1 ;;; frameset.el --- save and restore frame and window setup -*- lexical-binding: t -*-
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
5 ;; Author: Juanma Barranquero <lekktu@gmail.com>
6 ;; Keywords: convenience
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This file provides a set of operations to save a frameset (the state
26 ;; of all or a subset of the existing frames and windows), both
27 ;; in-session and persistently, and restore it at some point in the
30 ;; It should be noted that restoring the frames' windows depends on
31 ;; the buffers they are displaying, but this package does not provide
32 ;; any way to save and restore sets of buffers (see desktop.el for
33 ;; that). So, it's up to the user of frameset.el to make sure that
34 ;; any relevant buffer is loaded before trying to restore a frameset.
35 ;; When a window is restored and a buffer is missing, the window will
36 ;; be deleted unless it is the last one in the frame, in which case
37 ;; some previous buffer will be shown instead.
44 ;; Framesets have two fields:
45 ;; - properties: a property list to store both frameset-specific and
46 ;; user-defined serializable data. Currently defined properties
48 ;; :version ID - Identifies the version of the frameset struct;
49 ;; this is the only property always present and
50 ;; must not be modified.
51 ;; :app APPINFO - Freeform. Can be used by applications and
52 ;; packages to indicate the intended (but by no
53 ;; means exclusive) use of the frameset. For
54 ;; example, currently desktop.el sets :app to
55 ;; `(desktop . ,desktop-file-version).
56 ;; :name NAME - The name of the frameset instance; a string.
57 ;; :desc TEXT - A description for user consumption (to choose
58 ;; among framesets, etc.); a string.
59 ;; - states: an alist of items (FRAME-PARAMETERS . WINDOW-STATE) in
60 ;; no particular order. Each item represents a frame to be
63 (cl-defstruct (frameset (:type list
) :named
66 properties
;; property list
67 states
) ;; list of conses (frame-state . window-state)
69 (defun copy-frameset (frameset)
70 "Return a copy of FRAMESET.
71 This is a deep copy done with `copy-tree'."
72 (copy-tree frameset t
))
75 (defun frameset-p (frameset)
76 "If FRAMESET is a frameset, return its :version.
78 (and (eq (car-safe frameset
) 'frameset
)
79 (plist-get (cl-second frameset
) :version
)))
81 ;; A setf'able accessor to the frameset's properties
82 (defun frameset-prop (frameset prop
)
83 "Return the value of the PROP property of FRAMESET.
85 Properties other than :version can be set with
87 (setf (frameset-prop FRAMESET PROP) NEW-VALUE)"
88 (plist-get (frameset-properties frameset
) prop
))
90 (gv-define-setter frameset-prop
(v fs prop
)
92 (cl-assert (not (eq ,prop
:version
)) t
":version can not be set")
93 (setf (frameset-properties ,fs
)
94 (plist-put (frameset-properties ,fs
) ,prop
,v
))))
99 (defvar frameset-filter-alist
100 '((background-color . frameset-filter-sanitize-color
)
102 (buffer-predicate . t
)
103 (buried-buffer-list . t
)
104 (font . frameset-filter-save-parm
)
105 (foreground-color . frameset-filter-sanitize-color
)
106 (fullscreen . frameset-filter-save-parm
)
107 (GUI:font . frameset-filter-restore-parm
)
108 (GUI:fullscreen . frameset-filter-restore-parm
)
109 (GUI:height . frameset-filter-restore-parm
)
110 (GUI:width . frameset-filter-restore-parm
)
111 (height . frameset-filter-save-parm
)
112 (left . frameset-filter-iconified
)
113 (minibuffer . frameset-filter-minibuffer
)
114 (top . frameset-filter-iconified
)
115 (width . frameset-filter-save-parm
))
116 "Alist of frame parameters and filtering functions.
118 Each element is a cons (PARAM . ACTION), where PARAM is a parameter
119 name (a symbol identifying a frame parameter), and ACTION can be:
121 t The parameter is always removed from the parameter list.
122 :save The parameter is removed when saving the frame.
123 :restore The parameter is removed when restoring the frame.
124 FILTER A filter function.
126 FILTER can be a symbol FILTER-FUN, or a list (FILTER-FUN ARGS...).
127 It will be called with four arguments CURRENT, FILTERED, PARAMETERS
128 and SAVING, plus any additional ARGS:
130 CURRENT A cons (PARAM . VALUE), where PARAM is the one being
131 filtered and VALUE is its current value.
132 FILTERED The alist of parameters filtered so far.
133 PARAMETERS The complete alist of parameters being filtered,
134 SAVING Non-nil if filtering before saving state, nil otherwise.
136 The FILTER-FUN function must return:
137 nil CURRENT is removed from the list.
138 t CURRENT is left as is.
139 (PARAM' . VALUE') Replace CURRENT with this.
141 Frame parameters not on this list are passed intact.")
143 (defvar frameset--target-display nil
144 ;; Either (minibuffer . VALUE) or nil.
145 ;; This refers to the current frame config being processed inside
146 ;; `frame--restore-frames' and its auxiliary functions (like filtering).
147 ;; If nil, there is no need to change the display.
148 ;; If non-nil, display parameter to use when creating the frame.
149 "Internal use only.")
151 (defun frameset-switch-to-gui-p (parameters)
152 "True when switching to a graphic display.
153 Return t if PARAMETERS describes a text-only terminal and
154 the target is a graphic display; otherwise return nil.
155 Only meaningful when called from a filtering function in
156 `frameset-filter-alist'."
157 (and frameset--target-display
; we're switching
158 (null (cdr (assq 'display parameters
))) ; from a tty
159 (cdr frameset--target-display
))) ; to a GUI display
161 (defun frameset-switch-to-tty-p (parameters)
162 "True when switching to a text-only terminal.
163 Return t if PARAMETERS describes a graphic display and
164 the target is a text-only terminal; otherwise return nil.
165 Only meaningful when called from a filtering function in
166 `frameset-filter-alist'."
167 (and frameset--target-display
; we're switching
168 (cdr (assq 'display parameters
)) ; from a GUI display
169 (null (cdr frameset--target-display
)))) ; to a tty
171 (defun frameset-filter-sanitize-color (current _filtered parameters saving
)
172 "When switching to a GUI frame, remove \"unspecified\" colors.
173 Useful as a filter function for tty-specific parameters."
175 (not (frameset-switch-to-gui-p parameters
))
176 (not (stringp (cdr current
)))
177 (not (string-match-p "^unspecified-[fb]g$" (cdr current
)))))
179 (defun frameset-filter-minibuffer (current _filtered _parameters saving
)
180 "Convert (minibuffer . #<window>) parameter to (minibuffer . t)."
182 (if (windowp (cdr current
))
186 (defun frameset-filter-save-parm (current _filtered parameters saving
188 "When switching to a tty frame, save parameter P as PREFIX:P.
189 The parameter can be later restored with `frameset-filter-restore-parm'.
190 PREFIX defaults to `GUI'."
191 (unless prefix
(setq prefix
'GUI
))
193 ((frameset-switch-to-tty-p parameters
)
194 (let ((prefix:p
(intern (format "%s:%s" prefix
(car current
)))))
195 (if (assq prefix
:p parameters
)
197 (cons prefix
:p
(cdr current
)))))
198 ((frameset-switch-to-gui-p parameters
)
199 (not (assq (intern (format "%s:%s" prefix
(car current
))) parameters
)))
202 (defun frameset-filter-restore-parm (current filtered parameters saving
)
203 "When switching to a GUI frame, restore PREFIX:P parameter as P.
204 CURRENT must be of the form (PREFIX:P . value)."
206 (not (frameset-switch-to-gui-p parameters
))
207 (let* ((prefix:p
(symbol-name (car current
)))
208 (p (intern (substring prefix
:p
209 (1+ (string-match-p ":" prefix
:p
)))))
211 (found (assq p filtered
)))
217 (defun frameset-filter-iconified (_current _filtered parameters saving
)
218 "Remove CURRENT when saving an iconified frame.
219 This is used for positions parameters `left' and `top', which are
220 meaningless in an iconified frame, so the frame is restored in a
222 (not (and saving
(eq (cdr (assq 'visibility parameters
)) 'icon
))))
224 (defun frameset-keep-original-display-p (force-display)
225 "True if saved frames' displays should be honored."
227 ((eq system-type
'windows-nt
) nil
)
228 (t (null force-display
))))
230 (defun frameset-filter-params (parameters filter-alist saving
)
231 "Filter parameter list PARAMETERS and return a filtered list.
232 FILTER-ALIST is an alist of parameter filters, in the format of
233 `frameset-filter-alist' (which see).
234 SAVING is non-nil while filtering parameters to save a frameset,
235 nil while the filtering is done to restore it."
236 (let ((filtered nil
))
237 (dolist (current parameters
)
238 (pcase (cdr (assq (car current
) filter-alist
))
240 (push current filtered
))
244 (unless saving
(push current filtered
)))
246 (when saving
(push current filtered
)))
247 ((or `(,fun .
,args
) (and fun
(pred fboundp
)))
248 (let ((this (apply fun current filtered parameters saving args
)))
250 (push (if (eq this t
) current this
) filtered
))))
252 (delay-warning 'frameset
(format "Unknown filter %S" other
) :error
))))
253 ;; Set the display parameter after filtering, so that filter functions
254 ;; have access to its original value.
255 (when frameset--target-display
256 (let ((display (assq 'display filtered
)))
258 (setcdr display
(cdr frameset--target-display
))
259 (push frameset--target-display filtered
))))
265 (defun frameset--set-id (frame)
266 "Set FRAME's `frameset-id' if not yet set.
268 (unless (frame-parameter frame
'frameset-id
)
269 (set-frame-parameter frame
271 (mapconcat (lambda (n) (format "%04X" n
))
272 (cl-loop repeat
4 collect
(random 65536))
275 (defun frameset--process-minibuffer-frames (frame-list)
276 "Process FRAME-LIST and record minibuffer relationships.
277 FRAME-LIST is a list of frames."
278 ;; Record frames with their own minibuffer
279 (dolist (frame (minibuffer-frame-list))
280 (when (memq frame frame-list
)
281 (frameset--set-id frame
)
282 ;; For minibuffer-owning frames, frameset--mini is a cons
283 ;; (t . DEFAULT?), where DEFAULT? is a boolean indicating whether
284 ;; the frame is the one pointed out by `default-minibuffer-frame'.
285 (set-frame-parameter frame
287 (cons t
(eq frame default-minibuffer-frame
)))))
288 ;; Now link minibufferless frames with their minibuffer frames
289 (dolist (frame frame-list
)
290 (unless (frame-parameter frame
'frameset--mini
)
291 (frameset--set-id frame
)
292 (let* ((mb-frame (window-frame (minibuffer-window frame
)))
293 (id (and mb-frame
(frame-parameter mb-frame
'frameset-id
))))
295 (error "Minibuffer frame %S for %S is excluded" mb-frame frame
)
296 ;; For minibufferless frames, frameset--mini is a cons
297 ;; (nil . FRAME-ID), where FRAME-ID is the frameset-id of
298 ;; the frame containing its minibuffer window.
299 (set-frame-parameter frame
304 (cl-defun frameset-save (frame-list &key filters predicate properties
)
305 "Return the frameset of FRAME-LIST, a list of frames.
306 If nil, FRAME-LIST defaults to all live frames.
307 FILTERS is an alist of parameter filters; defaults to `frameset-filter-alist'.
308 PREDICATE is a predicate function, which must return non-nil for frames that
309 should be saved; it defaults to saving all frames from FRAME-LIST.
310 PROPERTIES is a user-defined property list to add to the frameset."
311 (let ((frames (cl-delete-if-not #'frame-live-p
312 (cl-delete-if-not (or predicate
#'framep
)
313 (or (copy-sequence frame-list
)
315 (frameset--process-minibuffer-frames frames
)
316 (make-frameset :properties
(append '(:version
1) properties
)
320 (frameset-filter-params (frame-parameters frame
)
322 frameset-filter-alist
)
324 (window-state-get (frame-root-window frame
) t
)))
328 ;; Restoring framesets
330 (defvar frameset--reuse-list nil
331 "Internal use only.")
333 (defun frameset--compute-pos (value left
/top right
/bottom
)
335 (`(+ ,val
) (+ left
/top val
))
336 (`(- ,val
) (+ right
/bottom val
))
339 (defun frameset--move-onscreen (frame force-onscreen
)
340 "If FRAME is offscreen, move it back onscreen and, if necessary, resize it.
341 For the description of FORCE-ONSCREEN, see `frameset-restore'.
342 When forced onscreen, frames wider than the monitor's workarea are converted
343 to fullwidth, and frames taller than the workarea are converted to fullheight.
344 NOTE: This only works for non-iconified frames. Internal use only."
345 (pcase-let* ((`(,left
,top
,width
,height
) (cl-cdadr (frame-monitor-attributes frame
)))
346 (right (+ left width -
1))
347 (bottom (+ top height -
1))
348 (fr-left (frameset--compute-pos (frame-parameter frame
'left
) left right
))
349 (fr-top (frameset--compute-pos (frame-parameter frame
'top
) top bottom
))
350 (ch-width (frame-char-width frame
))
351 (ch-height (frame-char-height frame
))
352 (fr-width (max (frame-pixel-width frame
) (* ch-width
(frame-width frame
))))
353 (fr-height (max (frame-pixel-height frame
) (* ch-height
(frame-height frame
))))
354 (fr-right (+ fr-left fr-width -
1))
355 (fr-bottom (+ fr-top fr-height -
1)))
356 (when (pcase force-onscreen
357 ;; Any corner is outside the screen.
358 (`all
(or (< fr-bottom top
) (> fr-bottom bottom
)
359 (< fr-left left
) (> fr-left right
)
360 (< fr-right left
) (> fr-right right
)
361 (< fr-top top
) (> fr-top bottom
)))
362 ;; Displaced to the left, right, above or below the screen.
363 (`t
(or (> fr-left right
)
367 ;; Fully inside, no need to do anything.
369 (let ((fullwidth (> fr-width width
))
370 (fullheight (> fr-height height
))
372 ;; Position frame horizontally.
374 (push `(left .
,left
) params
))
376 (push `(left .
,(+ left
(- width fr-width
))) params
))
378 (push `(left .
,left
) params
)))
379 ;; Position frame vertically.
381 (push `(top .
,top
) params
))
382 ((> fr-bottom bottom
)
383 (push `(top .
,(+ top
(- height fr-height
))) params
))
385 (push `(top .
,top
) params
)))
386 ;; Compute fullscreen state, if required.
387 (when (or fullwidth fullheight
)
388 (push (cons 'fullscreen
389 (cond ((not fullwidth
) 'fullheight
)
390 ((not fullheight
) 'fullwidth
)
393 ;; Finally, move the frame back onscreen.
395 (modify-frame-parameters frame params
))))))
397 (defun frameset--find-frame (predicate display
&rest args
)
398 "Find a frame in `frameset--reuse-list' satisfying PREDICATE.
399 Look through available frames whose display property matches DISPLAY
400 and return the first one for which (PREDICATE frame ARGS) returns t.
401 If PREDICATE is nil, it is always satisfied. Internal use only."
402 (cl-find-if (lambda (frame)
403 (and (equal (frame-parameter frame
'display
) display
)
405 (apply predicate frame args
))))
406 frameset--reuse-list
))
408 (defun frameset--reuse-frame (display frame-cfg
)
409 "Look for an existing frame to reuse.
410 DISPLAY is the display where the frame will be shown, and FRAME-CFG
411 is the parameter list of the frame being restored. Internal use only."
414 ;; There are no fancy heuristics there. We could implement some
415 ;; based on frame size and/or position, etc., but it is not clear
416 ;; that any "gain" (in the sense of reduced flickering, etc.) is
417 ;; worth the added complexity. In fact, the code below mainly
418 ;; tries to work nicely when M-x desktop-read is used after a
419 ;; desktop session has already been loaded. The other main use
420 ;; case, which is the initial desktop-read upon starting Emacs,
421 ;; will usually have only one frame, and should already work.
422 (cond ((null display
)
423 ;; When the target is tty, every existing frame is reusable.
424 (setq frame
(frameset--find-frame nil display
)))
425 ((car (setq mini
(cdr (assq 'frameset--mini frame-cfg
))))
426 ;; If the frame has its own minibuffer, let's see whether
427 ;; that frame has already been loaded (which can happen after
428 ;; M-x desktop-read).
429 (setq frame
(frameset--find-frame
431 (string= (frame-parameter f
'frameset-id
) id
))
433 ;; If it has not been loaded, and it is not a minibuffer-only frame,
434 ;; let's look for an existing non-minibuffer-only frame to reuse.
435 (unless (or frame
(eq (cdr (assq 'minibuffer frame-cfg
)) 'only
))
436 (setq frame
(frameset--find-frame
438 (let ((w (frame-parameter f
'minibuffer
)))
439 (and (window-live-p w
)
440 (window-minibuffer-p w
)
441 (eq (window-frame w
) f
))))
444 ;; For minibufferless frames, check whether they already exist,
445 ;; and that they are linked to the right minibuffer frame.
446 (setq frame
(frameset--find-frame
447 (lambda (f id mini-id
)
448 (and (string= (frame-parameter f
'frameset-id
) id
)
449 (string= (frame-parameter (window-frame (minibuffer-window f
))
452 display
(cdr (assq 'frameset-id frame-cfg
)) (cdr mini
))))
454 ;; Default to just finding a frame in the same display.
455 (setq frame
(frameset--find-frame nil display
))))
456 ;; If found, remove from the list.
458 (setq frameset--reuse-list
(delq frame frameset--reuse-list
)))
461 (defun frameset--get-frame (frame-cfg window-cfg filters force-onscreen
)
462 "Set up and return a frame according to its saved state.
463 That means either reusing an existing frame or creating one anew.
464 FRAME-CFG is the frame's parameter list; WINDOW-CFG is its window state.
465 For the meaning of FORCE-ONSCREEN, see `frameset-restore'."
466 (let* ((fullscreen (cdr (assq 'fullscreen frame-cfg
)))
467 (lines (assq 'tool-bar-lines frame-cfg
))
468 (filtered-cfg (frameset-filter-params frame-cfg filters nil
))
469 (display (cdr (assq 'display filtered-cfg
))) ;; post-filtering
472 ;; This works around bug#14795 (or feature#14795, if not a bug :-)
473 (setq filtered-cfg
(assq-delete-all 'tool-bar-lines filtered-cfg
))
474 (push '(tool-bar-lines .
0) filtered-cfg
)
477 ;; Currently Emacs has the limitation that it does not record the size
478 ;; and position of a frame before maximizing it, so we cannot save &
479 ;; restore that info. Instead, when restoring, we resort to creating
480 ;; invisible "fullscreen" frames of default size and then maximizing them
481 ;; (and making them visible) which at least is somewhat user-friendly
482 ;; when these frames are later de-maximized.
483 (let ((width (and (eq fullscreen
'fullheight
) (cdr (assq 'width filtered-cfg
))))
484 (height (and (eq fullscreen
'fullwidth
) (cdr (assq 'height filtered-cfg
))))
485 (visible (assq 'visibility filtered-cfg
)))
486 (setq filtered-cfg
(cl-delete-if (lambda (p)
487 (memq p
'(visibility fullscreen width height
)))
488 filtered-cfg
:key
#'car
))
490 (setq filtered-cfg
(append `((user-size . t
) (width .
,width
))
493 (setq filtered-cfg
(append `((user-size . t
) (height .
,height
))
495 ;; These are parameters to apply after creating/setting the frame.
496 (push visible alt-cfg
)
497 (push (cons 'fullscreen fullscreen
) alt-cfg
)))
499 ;; Time to find or create a frame an apply the big bunch of parameters.
500 ;; If a frame needs to be created and it falls partially or fully offscreen,
501 ;; sometimes it gets "pushed back" onscreen; however, moving it afterwards is
502 ;; allowed. So we create the frame as invisible and then reapply the full
503 ;; parameter list (including position and size parameters).
504 (setq frame
(or (and frameset--reuse-list
505 (frameset--reuse-frame display filtered-cfg
))
506 (make-frame-on-display display
509 for param in
'(left top width height minibuffer
)
510 collect
(assq param filtered-cfg
))))))
511 (modify-frame-parameters frame
512 (if (eq (frame-parameter frame
'fullscreen
) fullscreen
)
513 ;; Workaround for bug#14949
514 (assq-delete-all 'fullscreen filtered-cfg
)
517 ;; If requested, force frames to be onscreen.
518 (when (and force-onscreen
519 ;; FIXME: iconified frames should be checked too,
520 ;; but it is impossible without deiconifying them.
521 (not (eq (frame-parameter frame
'visibility
) 'icon
)))
522 (frameset--move-onscreen frame force-onscreen
))
524 ;; Let's give the finishing touches (visibility, tool-bar, maximization).
525 (when lines
(push lines alt-cfg
))
526 (when alt-cfg
(modify-frame-parameters frame alt-cfg
))
527 ;; Now restore window state.
528 (window-state-put window-cfg
(frame-root-window frame
) 'safe
)
531 (defun frameset--sort-states (state1 state2
)
532 "Predicate to sort frame states in a suitable order to be created.
533 It sorts minibuffer-owning frames before minibufferless ones."
534 (pcase-let ((`(,hasmini1
,id-def1
) (assq 'frameset--mini
(car state1
)))
535 (`(,hasmini2
,id-def2
) (assq 'frameset--mini
(car state2
))))
536 (cond ((eq id-def1 t
) t
)
538 ((not (eq hasmini1 hasmini2
)) (eq hasmini1 t
))
539 ((eq hasmini1 nil
) (string< id-def1 id-def2
))
542 (defun frameset-sort-frames-for-deletion (frame1 _frame2
)
543 "Predicate to sort live frames for deletion.
544 Minibufferless frames must go first to avoid errors when attempting
545 to delete a frame whose minibuffer window is used by another frame."
546 (not (frame-parameter frame1
'minibuffer
)))
549 (cl-defun frameset-restore (frameset &key filters reuse-frames force-display force-onscreen
)
550 "Restore a FRAMESET into the current display(s).
552 FILTERS is a list of parameter filters; defaults to `frameset-filter-alist'.
554 REUSE-FRAMES describes how to reuse existing frames while restoring a frameset:
555 t Reuse any existing frame if possible; delete leftover frames.
556 nil Restore frameset in new frames and delete existing frames.
557 keep Restore frameset in new frames and keep the existing ones.
558 LIST A list of frames to reuse; only these will be reused, if possible,
559 and any leftover one will be deleted; other frames not on this
562 FORCE-DISPLAY can be:
563 t Frames will be restored in the current display.
564 nil Frames will be restored, if possible, in their original displays.
565 delete Frames in other displays will be deleted instead of restored.
567 FORCE-ONSCREEN can be:
568 all Force onscreen any frame fully or partially offscreen.
569 t Force onscreen only those frames that are fully offscreen.
570 nil Do not force any frame back onscreen.
572 All keywords default to nil."
574 (cl-assert (frameset-p frameset
))
576 (let* ((delete-saved (eq force-display
'delete
))
577 (forcing (not (frameset-keep-original-display-p force-display
)))
578 (target (and forcing
(cons 'display
(frame-parameter nil
'display
))))
581 ;; frameset--reuse-list is a list of frames potentially reusable. Later we
582 ;; will decide which ones can be reused, and how to deal with any leftover.
585 (setq frameset--reuse-list nil
586 other-frames
(frame-list)))
588 (setq frameset--reuse-list
(copy-sequence reuse-frames
)
589 other-frames
(cl-delete-if (lambda (frame)
590 (memq frame frameset--reuse-list
))
593 (setq frameset--reuse-list
(frame-list)
596 ;; Sort saved states to guarantee that minibufferless frames will be created
597 ;; after the frames that contain their minibuffer windows.
598 (dolist (state (sort (copy-sequence (frameset-states frameset
))
599 #'frameset--sort-states
))
600 (condition-case-unless-debug err
601 (pcase-let* ((`(,frame-cfg .
,window-cfg
) state
)
602 ((and d-mini
`(,hasmini .
,mb-id
))
603 (cdr (assq 'frameset--mini frame-cfg
)))
604 (default (and (booleanp mb-id
) mb-id
))
605 (frame nil
) (to-tty nil
))
606 ;; Only set target if forcing displays and the target display is different.
607 (if (or (not forcing
)
608 (equal target
(or (assq 'display frame-cfg
) '(display . nil
))))
609 (setq frameset--target-display nil
)
610 (setq frameset--target-display target
611 to-tty
(null (cdr target
))))
612 ;; If keeping non-reusable frames, and the frame-id of one of them
613 ;; matches the frame-id of a frame being restored (because, for example,
614 ;; the frameset has already been read in the same session), remove the
615 ;; frame-id from the non-reusable frame, which is not useful anymore.
616 (when (and other-frames
617 (or (eq reuse-frames
'keep
) (consp reuse-frames
)))
618 (let ((dup (cl-find (cdr (assq 'frameset-frame-id frame-cfg
))
621 (frame-parameter frame
'frameset-frame-id
))
624 (set-frame-parameter dup
'frameset-frame-id nil
))))
625 ;; Time to restore frames and set up their minibuffers as they were.
626 ;; We only skip a frame (thus deleting it) if either:
627 ;; - we're switching displays, and the user chose the option to delete, or
628 ;; - we're switching to tty, and the frame to restore is minibuffer-only.
629 (unless (and frameset--target-display
632 (eq (cdr (assq 'minibuffer frame-cfg
)) 'only
))))
634 ;; Restore minibuffers. Some of this stuff could be done in a filter
635 ;; function, but it would be messy because restoring minibuffers affects
636 ;; global state; it's best to do it here than add a bunch of global
637 ;; variables to pass info back-and-forth to/from the filter function.
639 ((null d-mini
)) ;; No frameset--mini. Process as normal frame.
640 (to-tty) ;; Ignore minibuffer stuff and process as normal frame.
641 (hasmini ;; Frame has minibuffer (or it is minibuffer-only).
642 (when (eq (cdr (assq 'minibuffer frame-cfg
)) 'only
)
643 (setq frame-cfg
(append '((tool-bar-lines .
0) (menu-bar-lines .
0))
645 (t ;; Frame depends on other frame's minibuffer window.
646 (let* ((mb-frame (or (cl-find-if
648 (string= (frame-parameter f
'frameset-id
)
651 (error "Minibuffer frame %S not found" mb-id
)))
652 (mb-param (assq 'minibuffer frame-cfg
))
653 (mb-window (minibuffer-window mb-frame
)))
654 (unless (and (window-live-p mb-window
)
655 (window-minibuffer-p mb-window
))
656 (error "Not a minibuffer window %s" mb-window
))
658 (setcdr mb-param mb-window
)
659 (push (cons 'minibuffer mb-window
) frame-cfg
))))))
660 ;; OK, we're ready at last to create (or reuse) a frame and
661 ;; restore the window config.
662 (setq frame
(frameset--get-frame frame-cfg window-cfg
663 (or filters frameset-filter-alist
)
665 ;; Set default-minibuffer if required.
666 (when default
(setq default-minibuffer-frame frame
)))
668 (delay-warning 'frameset
(error-message-string err
) :error
))))
670 ;; In case we try to delete the initial frame, we want to make sure that
671 ;; other frames are already visible (discussed in thread for bug#14841).
674 ;; Delete remaining frames, but do not fail if some resist being deleted.
675 (unless (eq reuse-frames
'keep
)
676 (dolist (frame (sort (nconc (if (listp reuse-frames
) nil other-frames
)
677 frameset--reuse-list
)
678 #'frameset-sort-frames-for-deletion
))
682 (delay-warning 'frameset
(error-message-string err
))))))
683 (setq frameset--reuse-list nil
)
685 ;; Make sure there's at least one visible frame.
686 (unless (or (daemonp) (visible-frame-list))
687 (make-frame-visible (car (frame-list))))))
691 ;;; frameset.el ends here