Subject: Re: bug#36894: Stability issues in frameset sorting

From: Juri Linkov <[email-masked]>
Date: Fri, 09 Aug 2019 21:09:17 +0300

>> Currently the desktop doesn't restore focus to the previously selected frame.
>
> Is this new in Emacs 27?  Because I see no such problem in Emacs 26,
> where I use desktop all the time with multi-frame arrangements.

I tried to load in Emacs 26 the desktop created in Emacs 27,
but it crashed with this backtrace:

  emacs_backtrace at emacs/src/sysdep.c:2413
  terminate_due_to_signal at emacs/src/emacs.c:383
  handle_fatal_signal at emacs/src/sysdep.c:1769
  deliver_thread_signal at emacs/src/sysdep.c:1733
  stack_overflow at emacs/src/sysdep.c:1819
   (inlined by) handle_sigsegv at emacs/src/sysdep.c:1862
  ?? ??:0
  ?? ??:0
  xftfont_encode_char at emacs/src/xftfont.c:557
  get_char_glyph_code at emacs/src/xdisp.c:25797
   (inlined by) x_produce_glyphs at emacs/src/xdisp.c:28169
  produce_special_glyphs at emacs/src/xdisp.c:27805 (discriminator 9)
  init_iterator at emacs/src/xdisp.c:2945
  resize_mini_window at emacs/src/xdisp.c:11278
  display_echo_area_1 at emacs/src/xdisp.c:11175
  with_echo_area_buffer at emacs/src/xdisp.c:10955
  display_echo_area at emacs/src/xdisp.c:11142
   (inlined by) echo_area_display at emacs/src/xdisp.c:11647
  message3_nolog at emacs/src/xdisp.c:10653
  message3 at emacs/src/xdisp.c:10582
  Fmessage at emacs/src/editfns.c:4029
  Ffuncall at emacs/src/eval.c:2773
  exec_byte_code at emacs/src/bytecode.c:630
  Ffuncall at emacs/src/eval.c:2798
  Fapply at emacs/src/eval.c:2395
  apply1 at emacs/src/eval.c:2610
  call_debugger at emacs/src/eval.c:345
  signal_or_quit at emacs/src/eval.c:1617
  Fsignal at emacs/src/eval.c:1518
  xsignal1 at emacs/src/lisp.h:3854
  verror at emacs/src/eval.c:1840
  error at emacs/src/eval.c:1852
  x_set_font at emacs/src/frame.c:4262
  x_set_font_backend at emacs/src/frame.c:4396
  x_set_frame_parameters at emacs/src/frame.c:3913
  Fmodify_frame_parameters at emacs/src/frame.c:3177
  funcall_subr at emacs/src/eval.c:2850
  Ffuncall at emacs/src/eval.c:2773
  exec_byte_code at emacs/src/bytecode.c:630
  Ffuncall at emacs/src/eval.c:2798
  exec_byte_code at emacs/src/bytecode.c:630
  Ffuncall at emacs/src/eval.c:2798
  exec_byte_code at emacs/src/bytecode.c:630
  Ffuncall at emacs/src/eval.c:2798

Then replaced (font-backend xfthb x) with (font-backend xft x)
in the desktop file, after that it didn't crash, but raised
the error: (wrong-number-of-arguments #<subr set-window-fringes> 5)

Then replaced (fringes 8 8 nil nil) with (fringes 8 8 nil)
but it messed the monitors - restored frames in opposite monitors.

Then created a completely new desktop in Emacs 26,
but it didn't restore focus in the frame where it previously was.

And I don't see how Emacs 26 could restore focus,
because there is no 'last-focus-update' frame parameter in Emacs 26.
'last-focus-update' was added recently in Emacs 27.
So this is a new feature that we could use now in the desktop
to restore focus in the same frame where it was before.

> I'm wary of changing the order of restoring frames from what Emacs
> does now, because this could have adverse effects on some settings
> that are related to restoring frame dimensions.

I agree, looking at frameset--minibufferless-last-p it's difficult
to understand its logic, so I added comments to it.

Before my previous fix, despite the bug, the sorting order
was almost like intended in most cases: it still sorted
all minibuffer-owning frames before minibufferless.

Now I added more comments to explain the logic:

  (pcase-let ((`(,hasmini1 . ,id-def1) (cdr (assq 'frameset--mini (car state1))))
	      (`(,hasmini2 . ,id-def2) (cdr (assq 'frameset--mini (car state2)))))
    ;; hasmini1 is t when 1st frame has its own minibuffer
    ;; hasmini2 is t when 2nd frame has its own minibuffer
    ;; id-def1 is t when 1st minibuffer-owning frame is the default-minibuffer-frame
    ;;         or frame-id of 1st frame if it's minibufferless
    ;; id-def2 is t when 2nd minibuffer-owning frame is the default-minibuffer-frame
    ;;         or frame-id of 2nd frame if it's minibufferless
    (cond ;; Sort the minibuffer-owning default-minibuffer-frame first
	  ((eq id-def1 t) t)
	  ((eq id-def2 t) nil)
	  ;; Sort non-default minibuffer-owning frames before minibufferless
	  ((not (eq hasmini1 hasmini2)) (eq hasmini1 t)) ;; boolean xor
	  ;; Sort minibufferless frames with frame-id before some remaining
	  ((eq hasmini1 nil) (or id-def1 id-def2))
	  (t t)))

This shows that the intention was to restore minibuffer-owning frames before
minibufferless, and I agree that better not to change the sorting order,
because the last-selected frame might be the default-minibuffer-frame
(used by minibufferless frames) that should be created first.

So as a better solution we could restore the focus explicitly
after all frames are created.