lisp/frameset.el (frameset-prop): New function and setter.
[bpt/emacs.git] / lisp / frameset.el
1 ;;; frameset.el --- save and restore frame and window setup -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
4
5 ;; Author: Juanma Barranquero <lekktu@gmail.com>
6 ;; Keywords: convenience
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
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
28 ;; future.
29 ;;
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.
38
39 ;;; Code:
40
41 (require 'cl-lib)
42
43 \f
44 ;; Framesets have two fields:
45 ;; - properties: a property list to store both frameset-specific and
46 ;; user-defined serializable data. Currently defined properties
47 ;; include:
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
61 ;; restored.
62
63 (cl-defstruct (frameset (:type list) :named
64 (:copier nil)
65 (:predicate nil))
66 properties ;; property list
67 states) ;; list of conses (frame-state . window-state)
68
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))
73
74 ;;;autoload
75 (defun frameset-p (frameset)
76 "If FRAMESET is a frameset, return its :version.
77 Else return nil."
78 (and (eq (car-safe frameset) 'frameset)
79 (plist-get (cl-second frameset) :version)))
80
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.
84
85 Properties other than :version can be set with
86
87 (setf (frameset-prop FRAMESET PROP) NEW-VALUE)"
88 (plist-get (frameset-properties frameset) prop))
89
90 (gv-define-setter frameset-prop (v fs prop)
91 `(progn
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))))
95
96 \f
97 ;; Filtering
98
99 (defvar frameset-filter-alist
100 '((background-color . frameset-filter-sanitize-color)
101 (buffer-list . t)
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.
117
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:
120
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.
125
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:
129
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.
135
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.
140
141 Frame parameters not on this list are passed intact.")
142
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.")
150
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
160
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
170
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."
174 (or saving
175 (not (frameset-switch-to-gui-p parameters))
176 (not (stringp (cdr current)))
177 (not (string-match-p "^unspecified-[fb]g$" (cdr current)))))
178
179 (defun frameset-filter-minibuffer (current _filtered _parameters saving)
180 "Convert (minibuffer . #<window>) parameter to (minibuffer . t)."
181 (or (not saving)
182 (if (windowp (cdr current))
183 '(minibuffer . t)
184 t)))
185
186 (defun frameset-filter-save-parm (current _filtered parameters saving
187 &optional prefix)
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))
192 (cond (saving t)
193 ((frameset-switch-to-tty-p parameters)
194 (let ((prefix:p (intern (format "%s:%s" prefix (car current)))))
195 (if (assq prefix:p parameters)
196 nil
197 (cons prefix:p (cdr current)))))
198 ((frameset-switch-to-gui-p parameters)
199 (not (assq (intern (format "%s:%s" prefix (car current))) parameters)))
200 (t t)))
201
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)."
205 (or saving
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)))))
210 (val (cdr current))
211 (found (assq p filtered)))
212 (if (not found)
213 (cons p val)
214 (setcdr found val)
215 nil))))
216
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
221 default position."
222 (not (and saving (eq (cdr (assq 'visibility parameters)) 'icon))))
223
224 (defun frameset-keep-original-display-p (force-display)
225 "True if saved frames' displays should be honored."
226 (cond ((daemonp) t)
227 ((eq system-type 'windows-nt) nil)
228 (t (null force-display))))
229
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))
239 (`nil
240 (push current filtered))
241 (`t
242 nil)
243 (:save
244 (unless saving (push current filtered)))
245 (:restore
246 (when saving (push current filtered)))
247 ((or `(,fun . ,args) (and fun (pred fboundp)))
248 (let ((this (apply fun current filtered parameters saving args)))
249 (when this
250 (push (if (eq this t) current this) filtered))))
251 (other
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)))
257 (if display
258 (setcdr display (cdr frameset--target-display))
259 (push frameset--target-display filtered))))
260 filtered))
261
262 \f
263 ;; Saving framesets
264
265 (defun frameset--set-id (frame)
266 "Set FRAME's `frameset-id' if not yet set.
267 Internal use only."
268 (unless (frame-parameter frame 'frameset-id)
269 (set-frame-parameter frame
270 'frameset-id
271 (mapconcat (lambda (n) (format "%04X" n))
272 (cl-loop repeat 4 collect (random 65536))
273 "-"))))
274
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
286 'frameset--mini
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))))
294 (if (null 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
300 'frameset--mini
301 (cons nil id)))))))
302
303 ;;;autoload
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)
314 (frame-list))))))
315 (frameset--process-minibuffer-frames frames)
316 (make-frameset :properties (append '(:version 1) properties)
317 :states (mapcar
318 (lambda (frame)
319 (cons
320 (frameset-filter-params (frame-parameters frame)
321 (or filters
322 frameset-filter-alist)
323 t)
324 (window-state-get (frame-root-window frame) t)))
325 frames))))
326
327 \f
328 ;; Restoring framesets
329
330 (defvar frameset--reuse-list nil
331 "Internal use only.")
332
333 (defun frameset--compute-pos (value left/top right/bottom)
334 (pcase value
335 (`(+ ,val) (+ left/top val))
336 (`(- ,val) (+ right/bottom val))
337 (val val)))
338
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)
364 (< fr-right left)
365 (> fr-top bottom)
366 (< fr-bottom top)))
367 ;; Fully inside, no need to do anything.
368 (_ nil))
369 (let ((fullwidth (> fr-width width))
370 (fullheight (> fr-height height))
371 (params nil))
372 ;; Position frame horizontally.
373 (cond (fullwidth
374 (push `(left . ,left) params))
375 ((> fr-right right)
376 (push `(left . ,(+ left (- width fr-width))) params))
377 ((< fr-left left)
378 (push `(left . ,left) params)))
379 ;; Position frame vertically.
380 (cond (fullheight
381 (push `(top . ,top) params))
382 ((> fr-bottom bottom)
383 (push `(top . ,(+ top (- height fr-height))) params))
384 ((< fr-top top)
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)
391 (t 'maximized)))
392 params))
393 ;; Finally, move the frame back onscreen.
394 (when params
395 (modify-frame-parameters frame params))))))
396
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)
404 (or (null predicate)
405 (apply predicate frame args))))
406 frameset--reuse-list))
407
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."
412 (let ((frame nil)
413 mini)
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
430 (lambda (f id)
431 (string= (frame-parameter f 'frameset-id) id))
432 display (cdr mini)))
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
437 (lambda (f)
438 (let ((w (frame-parameter f 'minibuffer)))
439 (and (window-live-p w)
440 (window-minibuffer-p w)
441 (eq (window-frame w) f))))
442 display))))
443 (mini
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))
450 'frameset-id)
451 mini-id)))
452 display (cdr (assq 'frameset-id frame-cfg)) (cdr mini))))
453 (t
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.
457 (when frame
458 (setq frameset--reuse-list (delq frame frameset--reuse-list)))
459 frame))
460
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
470 alt-cfg frame)
471
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)
475
476 (when fullscreen
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))
489 (when width
490 (setq filtered-cfg (append `((user-size . t) (width . ,width))
491 filtered-cfg)))
492 (when height
493 (setq filtered-cfg (append `((user-size . t) (height . ,height))
494 filtered-cfg)))
495 ;; These are parameters to apply after creating/setting the frame.
496 (push visible alt-cfg)
497 (push (cons 'fullscreen fullscreen) alt-cfg)))
498
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
507 (cons '(visibility)
508 (cl-loop
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)
515 filtered-cfg))
516
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))
523
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)
529 frame))
530
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)
537 ((eq id-def2 t) nil)
538 ((not (eq hasmini1 hasmini2)) (eq hasmini1 t))
539 ((eq hasmini1 nil) (string< id-def1 id-def2))
540 (t t))))
541
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)))
547
548 ;;;autoload
549 (cl-defun frameset-restore (frameset &key filters reuse-frames force-display force-onscreen)
550 "Restore a FRAMESET into the current display(s).
551
552 FILTERS is a list of parameter filters; defaults to `frameset-filter-alist'.
553
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
560 list will be kept.
561
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.
566
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.
571
572 All keywords default to nil."
573
574 (cl-assert (frameset-p frameset))
575
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))))
579 other-frames)
580
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.
583 (pcase reuse-frames
584 ((or `nil `keep)
585 (setq frameset--reuse-list nil
586 other-frames (frame-list)))
587 ((pred consp)
588 (setq frameset--reuse-list (copy-sequence reuse-frames)
589 other-frames (cl-delete-if (lambda (frame)
590 (memq frame frameset--reuse-list))
591 (frame-list))))
592 (_
593 (setq frameset--reuse-list (frame-list)
594 other-frames nil)))
595
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))
619 other-frames
620 :key (lambda (frame)
621 (frame-parameter frame 'frameset-frame-id))
622 :test #'string=)))
623 (when dup
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
630 (or delete-saved
631 (and to-tty
632 (eq (cdr (assq 'minibuffer frame-cfg)) 'only))))
633
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.
638 (cond
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))
644 frame-cfg))))
645 (t ;; Frame depends on other frame's minibuffer window.
646 (let* ((mb-frame (or (cl-find-if
647 (lambda (f)
648 (string= (frame-parameter f 'frameset-id)
649 mb-id))
650 (frame-list))
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))
657 (if mb-param
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)
664 force-onscreen))
665 ;; Set default-minibuffer if required.
666 (when default (setq default-minibuffer-frame frame)))
667 (error
668 (delay-warning 'frameset (error-message-string err) :error))))
669
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).
672 (sit-for 0 t)
673
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))
679 (condition-case err
680 (delete-frame frame)
681 (error
682 (delay-warning 'frameset (error-message-string err))))))
683 (setq frameset--reuse-list nil)
684
685 ;; Make sure there's at least one visible frame.
686 (unless (or (daemonp) (visible-frame-list))
687 (make-frame-visible (car (frame-list))))))
688
689 (provide 'frameset)
690
691 ;;; frameset.el ends here