88fb2634b9d865c1c2702e48c7adbd79bcf40e55
[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 (cl-defstruct (frameset (:type vector) :named
45 ;; Copier and predicate functions are defined below.
46 (:copier nil)
47 (:predicate nil))
48
49 "A frameset encapsulates a serializable view of a set of frames and windows.
50
51 It contains the following slots, which can be accessed with
52 \(frameset-SLOT fs) and set with (setf (frameset-SLOT fs) VALUE):
53
54 version A read-only version number, identifying the format
55 of the frameset struct. Currently its value is 1.
56 timestamp A read-only timestamp, the output of `current-time'.
57 app A symbol, or a list whose first element is a symbol, which
58 identifies the creator of the frameset and related info;
59 for example, desktop.el sets this slot to a list
60 `(desktop . ,desktop-file-version).
61 name A string, the name of the frameset instance.
62 description A string, a description for user consumption (to show in
63 menus, messages, etc).
64 properties A property list, to store both frameset-specific and
65 user-defined serializable data.
66 states A list of items (FRAME-PARAMETERS . WINDOW-STATE), in no
67 particular order. Each item represents a frame to be
68 restored. FRAME-PARAMETERS is a frame's parameter alist,
69 extracted with (frame-parameters FRAME) and filtered
70 through `frameset-filter-params'.
71 WINDOW-STATE is the output of `window-state-get' applied
72 to the root window of the frame.
73
74 To avoid collisions, it is recommended that applications wanting to add
75 private serializable data to `properties' either store all info under a
76 single, distinctive name, or use property names with a well-chosen prefix.
77
78 A frameset is intended to be used through the following simple API:
79
80 - `frameset-save', the type's constructor, captures all or a subset of the
81 live frames, and returns a serializable snapshot of them (a frameset).
82 - `frameset-restore' takes a frameset, and restores the frames and windows
83 it describes, as faithfully as possible.
84 - `frameset-p' is the predicate for the frameset type. It returns nil
85 for non-frameset objects, and the frameset version number (see below)
86 for frameset objects.
87 - `frameset-copy' returns a deep copy of a frameset.
88 - `frameset-prop' is a `setf'able accessor for the contents of the
89 `properties' slot.
90 - The `frameset-SLOT' accessors described above."
91
92 (version 1 :read-only t)
93 (timestamp (current-time) :read-only t)
94 (app nil)
95 (name nil)
96 (description nil)
97 (properties nil)
98 (states nil))
99
100 (defun frameset-copy (frameset)
101 "Return a deep copy of FRAMESET.
102 FRAMESET is copied with `copy-tree'."
103 (copy-tree frameset t))
104
105 ;;;###autoload
106 (defun frameset-p (object)
107 "If OBJECT is a frameset, return its version number.
108 Else return nil."
109 (and (vectorp object) ; a vector
110 (eq (aref object 0) 'frameset) ; tagged as `frameset'
111 (integerp (aref object 1)) ; version is an int
112 (consp (aref object 2)) ; timestamp is a non-null list
113 (stringp (or (aref object 4) "")) ; name is a string or null
114 (stringp (or (aref object 5) "")) ; description is a string or null
115 (listp (aref object 6)) ; properties is a list
116 (consp (aref object 7)) ; and states is non-null
117 (aref object 1))) ; return version
118
119 ;; A setf'able accessor to the frameset's properties
120 (defun frameset-prop (frameset property)
121 "Return the value for FRAMESET of PROPERTY.
122
123 Properties can be set with
124
125 (setf (frameset-prop FRAMESET PROP) NEW-VALUE)"
126 (plist-get (frameset-properties frameset) property))
127
128 (gv-define-setter frameset-prop (val fs prop)
129 (macroexp-let2 nil v val
130 `(progn
131 (setf (frameset-properties ,fs)
132 (plist-put (frameset-properties ,fs) ,prop ,v))
133 ,v)))
134
135 \f
136 ;; Filtering
137
138 ;; What's the deal with these "filter alists"?
139 ;;
140 ;; Let's say that Emacs' frame parameters were never designed as a tool to
141 ;; precisely record (or restore) a frame's state. They grew organically,
142 ;; and their uses and behaviors reflect their history. In using them to
143 ;; implement framesets, the unwary implementor, or the prospective package
144 ;; writer willing to use framesets in their code, might fall victim of some
145 ;; unexpected... oddities.
146 ;;
147 ;; You can find frame parameters that:
148 ;;
149 ;; - can be used to get and set some data from the frame's current state
150 ;; (`height', `width')
151 ;; - can be set at creation time, and setting them afterwards has no effect
152 ;; (`window-state', `minibuffer')
153 ;; - can be set at creation time, and setting them afterwards will fail with
154 ;; an error, *unless* you set it to the same value, a noop (`border-width')
155 ;; - act differently when passed at frame creation time, and when set
156 ;; afterwards (`height')
157 ;; - affect the value of other parameters (`name', `visibility')
158 ;; - can be ignored by window managers (most positional args, like `height',
159 ;; `width', `left' and `top', and others, like `auto-raise', `auto-lower')
160 ;; - can be set externally in X resources or Window registry (again, most
161 ;; positional parameters, and also `toolbar-lines', `menu-bar-lines' etc.)
162 ;, - can contain references to live objects (`buffer-list', `minibuffer') or
163 ;; code (`buffer-predicate')
164 ;; - are set automatically, and cannot be changed (`window-id', `parent-id'),
165 ;; but setting them produces no error
166 ;; - have a noticeable effect in some window managers, and are ignored in
167 ;; others (`menu-bar-lines')
168 ;; - can not be safely set in a tty session and then copied back to a GUI
169 ;; session (`font', `background-color', `foreground-color')
170 ;;
171 ;; etc etc.
172 ;;
173 ;; Which means that, in order to save a parameter alist to disk and read it
174 ;; back later to reconstruct a frame, some processing must be done. That's
175 ;; what `frameset-filter-params' and the `frameset-*-filter-alist' variables
176 ;; are for.
177 ;;
178 ;; First, a clarification: the word "filter" in these names refers to both
179 ;; common meanings of filter: to filter out (i.e., to remove), and to pass
180 ;; through a transformation function (think `filter-buffer-substring').
181 ;;
182 ;; `frameset-filter-params' takes a parameter alist PARAMETERS, a filtering
183 ;; alist FILTER-ALIST, and a flag SAVING to indicate whether we are filtering
184 ;; parameters with the intent of saving a frame or restoring it. It then
185 ;; accumulates an output list, FILTERED, by checking each parameter in
186 ;; PARAMETERS against FILTER-ALIST and obeying any rule found there. The
187 ;; absence of a rule just means the parameter/value pair (called CURRENT in
188 ;; filtering functions) is copied to FILTERED as is. Keyword values :save,
189 ;; :restore and :never tell the function to copy CURRENT to FILTERED in the
190 ;; respective situations, that is, when saving, restoring, or never at all.
191 ;; Values :save and :restore are not used in this package, because usually if
192 ;; you don't want to save a parameter, you don't want to restore it either.
193 ;; But they can be useful, for example, if you already have a saved frameset
194 ;; created with some intent, and want to reuse it for a different objective
195 ;; where the expected parameter list has different requirements.
196 ;;
197 ;; Finally, the value can also be a filtering function, or a filtering
198 ;; function plus some arguments. The function is called for each matching
199 ;; parameter, and receives CURRENT (the parameter/value pair being processed),
200 ;; FILTERED (the output alist so far), PARAMETERS (the full parameter alist),
201 ;; SAVING (the save/restore flag), plus any additional ARGS set along the
202 ;; function in the `frameset-*-filter-alist' entry. The filtering function
203 ;; then has the possibility to pass along CURRENT, or reject it altogether,
204 ;; or pass back a (NEW-PARAM . NEW-VALUE) pair, which does not even need to
205 ;; refer to the same parameter (so you can filter `width' and return `height'
206 ;; and vice versa, if you're feeling silly and want to mess with the user's
207 ;; mind). As a help in deciding what to do, the filtering function has
208 ;; access to PARAMETERS, but must not change it in any way. It also has
209 ;; access to FILTERED, which can be modified at will. This allows two or
210 ;; more filters to coordinate themselves, because in general there's no way
211 ;; to predict the order in which they will be run.
212 ;;
213 ;; So, which parameters are filtered by default, and why? Let's see.
214 ;;
215 ;; - `buffer-list', `buried-buffer-list', `buffer-predicate': They contain
216 ;; references to live objects, or in the case of `buffer-predicate', it
217 ;; could also contain an fbound symbol (a predicate function) that could
218 ;; not be defined in a later session.
219 ;;
220 ;; - `window-id', `outer-window-id', `parent-id': They are assigned
221 ;; automatically and cannot be set, so keeping them is harmless, but they
222 ;; add clutter. `window-system' is similar: it's assigned at frame
223 ;; creation, and does not serve any useful purpose later.
224 ;;
225 ;; - `left', `top': Only problematic when saving an iconified frame, because
226 ;; when the frame is iconified they are set to (- 32000), which doesn't
227 ;; really help in restoring the frame. Better to remove them and let the
228 ;; window manager choose a default position for the frame.
229 ;;
230 ;; - `background-color', `foreground-color': In tty frames they can be set
231 ;; to "unspecified-bg" and "unspecified-fg", which aren't understood on
232 ;; GUI sessions. They have to be filtered out when switching from tty to
233 ;; a graphical display.
234 ;;
235 ;; - `tty', `tty-type': These are tty-specific. When switching to a GUI
236 ;; display they do no harm, but they clutter the parameter list.
237 ;;
238 ;; - `minibuffer': It can contain a reference to a live window, which cannot
239 ;; be serialized. Because of Emacs' idiosyncratic treatment of this
240 ;; parameter, frames created with (minibuffer . t) have a parameter
241 ;; (minibuffer . #<window...>), while frames created with
242 ;; (minibuffer . #<window...>) have (minibuffer . nil), which is madness
243 ;; but helps to differentiate between minibufferless and "normal" frames.
244 ;; So, changing (minibuffer . #<window...>) to (minibuffer . t) allows
245 ;; Emacs to set up the new frame correctly. Nice, uh?
246 ;;
247 ;; - `name': If this parameter is directly set, `explicit-name' is
248 ;; automatically set to t, and then `name' no longer changes dynamically.
249 ;; So, in general, not saving `name' is the right thing to do, though
250 ;; surely there are applications that will want to override this filter.
251 ;;
252 ;; - `font', `fullscreen', `height' and `width': These parameters suffer
253 ;; from the fact that they are badly manged when going through a
254 ;; tty session, though not all in the same way. When saving a GUI frame
255 ;; and restoring it in a tty, the height and width of the new frame are
256 ;; those of the tty screen (let's say 80x25, for example); going back
257 ;; to a GUI session means getting frames of the tty screen size (so all
258 ;; your frames are 80 cols x 25 rows). For `fullscreen' there's a
259 ;; similar problem, because a tty frame cannot really be fullscreen or
260 ;; maximized, so the state is lost. The problem with `font' is a bit
261 ;; different, because a valid GUI font spec in `font' turns into
262 ;; (font . "tty") in a tty frame, and when read back into a GUI session
263 ;; it fails because `font's value is no longer a valid font spec.
264 ;;
265 ;; In most cases, the filtering functions just do the obvious thing: remove
266 ;; CURRENT when it is meaningless to keep it, or pass a modified copy if
267 ;; that helps (as in the case of `minibuffer').
268 ;;
269 ;; The exception are the parameters in the last set, which should survive
270 ;; the roundtrip though tty-land. The answer is to add "stashing
271 ;; parameters", working in pairs, to shelve the GUI-specific contents and
272 ;; restore it once we're back in pixel country. That's what functions
273 ;; `frameset-filter-shelve-param' and `frameset-unshelve-param' do.
274 ;;
275 ;; Basically, if you set `frameset-filter-shelve-param' as the filter for
276 ;; a parameter P, it will detect when it is restoring a GUI frame into a
277 ;; tty session, and save P's value in the custom parameter X:P, but only
278 ;; if X:P does not exist already (so it is not overwritten if you enter
279 ;; the tty session more than once). If you're not switching to a tty
280 ;; frame, the filter just passes CURRENT along.
281 ;;
282 ;; The parameter X:P, on the other hand, must have been setup to be
283 ;; filtered by `frameset-filter-unshelve-param', which unshelves the
284 ;; value: if we're entering a GUI session, returns P instead of CURRENT,
285 ;; while in other cases it just passes it along.
286 ;;
287 ;; The only additional trick is that `frameset-filter-shelve-param' does
288 ;; not set P if switching back to GUI and P already has a value, because
289 ;; it assumes that `frameset-filter-unshelve-param' did set it up. And
290 ;; `frameset-filter-unshelve-param', when unshelving P, must look into
291 ;; FILTERED to determine if P has already been set and if so, modify it;
292 ;; else just returns P.
293 ;;
294 ;; Currently, the value of X in X:P is `GUI', but you can use any prefix,
295 ;; by passing its symbol as argument in the filter:
296 ;;
297 ;; (my-parameter frameset-filter-shelve-param MYPREFIX)
298 ;;
299 ;; instead of
300 ;;
301 ;; (my-parameter . frameset-filter-shelve-param)
302 ;;
303 ;; Note that `frameset-filter-unshelve-param' does not need MYPREFIX
304 ;; because it is available from the parameter name in CURRENT. Also note
305 ;; that the colon between the prefix and the parameter name is hardcoded.
306 ;; The reason is that X:P is quite readable, and that the colon is a
307 ;; very unusual character in symbol names, other than in initial position
308 ;; in keywords (emacs -Q has only two such symbols, and one of them is a
309 ;; URL). So the probability of a collision with existing or future
310 ;; symbols is quite insignificant.
311 ;;
312 ;; Now, what about the filter alists? There are three of them, though
313 ;; only two sets of parameters:
314 ;;
315 ;; - `frameset-session-filter-alist' contains these filters that allow to
316 ;; save and restore framesets in-session, without the need to serialize
317 ;; the frameset or save it to disk (for example, to save a frameset in a
318 ;; register and restore it later). Filters in this list do not remove
319 ;; live objects, except in `minibuffer', which is dealt especially by
320 ;; `frameset-save' / `frameset-restore'.
321 ;;
322 ;; - `frameset-persistent-filter-alist' is the whole deal. It does all
323 ;; the filtering described above, and the result is ready to be saved on
324 ;; disk without loss of information. That's the format used by the
325 ;; desktop.el package, for example.
326 ;;
327 ;; IMPORTANT: These variables share structure and should never be modified.
328 ;;
329 ;; - `frameset-filter-alist': The value of this variable is the default
330 ;; value for the FILTERS arguments of `frameset-save' and
331 ;; `frameset-restore'. It is set to `frameset-persistent-filter-alist',
332 ;; though it can be changed by specific applications.
333 ;;
334 ;; How to use them?
335 ;;
336 ;; The simplest way is just do nothing. The default should work
337 ;; reasonably and sensibly enough. But, what if you really need a
338 ;; customized filter alist? Then you can create your own variable
339 ;;
340 ;; (defvar my-filter-alist
341 ;; '((my-param1 . :never)
342 ;; (my-param2 . :save)
343 ;; (my-param3 . :restore)
344 ;; (my-param4 . my-filtering-function-without-args)
345 ;; (my-param5 my-filtering-function-with arg1 arg2)
346 ;; ;;; many other parameters
347 ;; )
348 ;; "My customized parameter filter alist.")
349 ;;
350 ;; or, if you're only changing a few items,
351 ;;
352 ;; (defvar my-filter-alist
353 ;; (nconc '((my-param1 . :never)
354 ;; (my-param2 . my-filtering-function))
355 ;; frameset-filter-alist)
356 ;; "My brief customized parameter filter alist.")
357 ;;
358 ;; and pass it to the FILTER arg of the save/restore functions,
359 ;; ALWAYS taking care of not modifying the original lists; if you're
360 ;; going to do any modifying of my-filter-alist, please use
361 ;;
362 ;; (nconc '((my-param1 . :never) ...)
363 ;; (copy-sequence frameset-filter-alist))
364 ;;
365 ;; One thing you shouldn't forget is that they are alists, so searching
366 ;; in them is sequential. If you just want to change the default of
367 ;; `name' to allow it to be saved, you can set (name . nil) in your
368 ;; customized filter alist; it will take precedence over the latter
369 ;; setting. In case you decide that you *always* want to save `name',
370 ;; you can add it to `frameset-filter-alist':
371 ;;
372 ;; (push '(name . nil) frameset-filter-alist)
373 ;;
374 ;; In certain applications, having a parameter filtering function like
375 ;; `frameset-filter-params' can be useful, even if you're not using
376 ;; framesets. The interface of `frameset-filter-params' is generic
377 ;; and does not depend of global state, with one exception: it uses
378 ;; the internal variable `frameset--target-display' to decide if, and
379 ;; how, to modify the `display' parameter of FILTERED. But that
380 ;; should not represent any problem, because it's only meaningful
381 ;; when restoring, and customized uses of `frameset-filter-params'
382 ;; are likely to use their own filter alist and just call
383 ;;
384 ;; (setq my-filtered (frameset-filter-params my-params my-filters t))
385 ;;
386 ;; In case you want to use it with the standard filters, you can
387 ;; wrap the call to `frameset-filter-params' in a let form to bind
388 ;; `frameset--target-display' to nil or the desired value.
389 ;;
390
391 ;;;###autoload
392 (defvar frameset-session-filter-alist
393 '((name . :never)
394 (left . frameset-filter-iconified)
395 (minibuffer . frameset-filter-minibuffer)
396 (top . frameset-filter-iconified))
397 "Minimum set of parameters to filter for live (on-session) framesets.
398 See `frameset-filter-alist' for a full description.")
399
400 ;;;###autoload
401 (defvar frameset-persistent-filter-alist
402 (nconc
403 '((background-color . frameset-filter-sanitize-color)
404 (buffer-list . :never)
405 (buffer-predicate . :never)
406 (buried-buffer-list . :never)
407 (font . frameset-filter-shelve-param)
408 (foreground-color . frameset-filter-sanitize-color)
409 (fullscreen . frameset-filter-shelve-param)
410 (GUI:font . frameset-filter-unshelve-param)
411 (GUI:fullscreen . frameset-filter-unshelve-param)
412 (GUI:height . frameset-filter-unshelve-param)
413 (GUI:width . frameset-filter-unshelve-param)
414 (height . frameset-filter-shelve-param)
415 (outer-window-id . :never)
416 (parent-id . :never)
417 (tty . frameset-filter-tty-to-GUI)
418 (tty-type . frameset-filter-tty-to-GUI)
419 (width . frameset-filter-shelve-param)
420 (window-id . :never)
421 (window-system . :never))
422 frameset-session-filter-alist)
423 "Parameters to filter for persistent framesets.
424 See `frameset-filter-alist' for a full description.")
425
426 ;;;###autoload
427 (defvar frameset-filter-alist frameset-persistent-filter-alist
428 "Alist of frame parameters and filtering functions.
429
430 This alist is the default value of the FILTERS argument of
431 `frameset-save' and `frameset-restore' (which see).
432
433 On saving, PARAMETERS is the parameter alist of each frame processed,
434 and FILTERED is the parameter alist that gets saved to the frameset.
435
436 On restoring, PARAMETERS is the parameter alist extracted from the
437 frameset, and FILTERED is the resulting frame parameter alist used
438 to restore the frame.
439
440 Elements of `frameset-filter-alist' are conses (PARAM . ACTION),
441 where PARAM is a parameter name (a symbol identifying a frame
442 parameter), and ACTION can be:
443
444 nil The parameter is copied to FILTERED.
445 :never The parameter is never copied to FILTERED.
446 :save The parameter is copied only when saving the frame.
447 :restore The parameter is copied only when restoring the frame.
448 FILTER A filter function.
449
450 FILTER can be a symbol FILTER-FUN, or a list (FILTER-FUN ARGS...).
451 FILTER-FUN is invoked with
452
453 (apply FILTER-FUN CURRENT FILTERED PARAMETERS SAVING ARGS)
454
455 where
456
457 CURRENT A cons (PARAM . VALUE), where PARAM is the one being
458 filtered and VALUE is its current value.
459 FILTERED The resulting alist (so far).
460 PARAMETERS The complete alist of parameters being filtered,
461 SAVING Non-nil if filtering before saving state, nil if filtering
462 before restoring it.
463 ARGS Any additional arguments specified in the ACTION.
464
465 FILTER-FUN is allowed to modify items in FILTERED, but no other arguments.
466 It must return:
467 nil Skip CURRENT (do not add it to FILTERED).
468 t Add CURRENT to FILTERED as is.
469 (NEW-PARAM . NEW-VALUE) Add this to FILTERED instead of CURRENT.
470
471 Frame parameters not on this alist are passed intact, as if they were
472 defined with ACTION = nil.")
473
474
475 (defvar frameset--target-display nil
476 ;; Either (minibuffer . VALUE) or nil.
477 ;; This refers to the current frame config being processed inside
478 ;; `frameset-restore' and its auxiliary functions (like filtering).
479 ;; If nil, there is no need to change the display.
480 ;; If non-nil, display parameter to use when creating the frame.
481 "Internal use only.")
482
483 (defun frameset-switch-to-gui-p (parameters)
484 "True when switching to a graphic display.
485 Return non-nil if the parameter alist PARAMETERS describes a frame on a
486 text-only terminal, and the frame is being restored on a graphic display;
487 otherwise return nil. Only meaningful when called from a filtering
488 function in `frameset-filter-alist'."
489 (and frameset--target-display ; we're switching
490 (null (cdr (assq 'display parameters))) ; from a tty
491 (cdr frameset--target-display))) ; to a GUI display
492
493 (defun frameset-switch-to-tty-p (parameters)
494 "True when switching to a text-only terminal.
495 Return non-nil if the parameter alist PARAMETERS describes a frame on a
496 graphic display, and the frame is being restored on a text-only terminal;
497 otherwise return nil. Only meaningful when called from a filtering
498 function in `frameset-filter-alist'."
499 (and frameset--target-display ; we're switching
500 (cdr (assq 'display parameters)) ; from a GUI display
501 (null (cdr frameset--target-display)))) ; to a tty
502
503 (defun frameset-filter-tty-to-GUI (_current _filtered parameters saving)
504 "Remove CURRENT when switching from tty to a graphic display.
505
506 For the meaning of CURRENT, FILTERED, PARAMETERS and SAVING,
507 see `frameset-filter-alist'."
508 (or saving
509 (not (frameset-switch-to-gui-p parameters))))
510
511 (defun frameset-filter-sanitize-color (current _filtered parameters saving)
512 "When switching to a GUI frame, remove \"unspecified\" colors.
513 Useful as a filter function for tty-specific parameters.
514
515 For the meaning of CURRENT, FILTERED, PARAMETERS and SAVING,
516 see `frameset-filter-alist'."
517 (or saving
518 (not (frameset-switch-to-gui-p parameters))
519 (not (stringp (cdr current)))
520 (not (string-match-p "^unspecified-[fb]g$" (cdr current)))))
521
522 (defun frameset-filter-minibuffer (current _filtered _parameters saving)
523 "When saving, convert (minibuffer . #<window>) to (minibuffer . t).
524
525 For the meaning of CURRENT, FILTERED, PARAMETERS and SAVING,
526 see `frameset-filter-alist'."
527 (or (not saving)
528 (if (windowp (cdr current))
529 '(minibuffer . t)
530 t)))
531
532 (defun frameset-filter-shelve-param (current _filtered parameters saving
533 &optional prefix)
534 "When switching to a tty frame, save parameter P as PREFIX:P.
535 The parameter can be later restored with `frameset-filter-unshelve-param'.
536 PREFIX defaults to `GUI'.
537
538 For the meaning of CURRENT, FILTERED, PARAMETERS and SAVING,
539 see `frameset-filter-alist'."
540 (unless prefix (setq prefix 'GUI))
541 (cond (saving t)
542 ((frameset-switch-to-tty-p parameters)
543 (let ((prefix:p (intern (format "%s:%s" prefix (car current)))))
544 (if (assq prefix:p parameters)
545 nil
546 (cons prefix:p (cdr current)))))
547 ((frameset-switch-to-gui-p parameters)
548 (not (assq (intern (format "%s:%s" prefix (car current))) parameters)))
549 (t t)))
550
551 (defun frameset-filter-unshelve-param (current filtered parameters saving)
552 "When switching to a GUI frame, restore PREFIX:P parameter as P.
553 CURRENT must be of the form (PREFIX:P . value).
554
555 For the meaning of CURRENT, FILTERED, PARAMETERS and SAVING,
556 see `frameset-filter-alist'."
557 (or saving
558 (not (frameset-switch-to-gui-p parameters))
559 (let* ((prefix:p (symbol-name (car current)))
560 (p (intern (substring prefix:p
561 (1+ (string-match-p ":" prefix:p)))))
562 (val (cdr current))
563 (found (assq p filtered)))
564 (if (not found)
565 (cons p val)
566 (setcdr found val)
567 nil))))
568
569 (defun frameset-filter-iconified (_current _filtered parameters saving)
570 "Remove CURRENT when saving an iconified frame.
571 This is used for positional parameters `left' and `top', which are
572 meaningless in an iconified frame, so the frame is restored in a
573 default position.
574
575 For the meaning of CURRENT, FILTERED, PARAMETERS and SAVING,
576 see `frameset-filter-alist'."
577 (not (and saving (eq (cdr (assq 'visibility parameters)) 'icon))))
578
579 (defun frameset-filter-params (parameters filter-alist saving)
580 "Filter parameter alist PARAMETERS and return a filtered alist.
581 FILTER-ALIST is an alist of parameter filters, in the format of
582 `frameset-filter-alist' (which see).
583 SAVING is non-nil while filtering parameters to save a frameset,
584 nil while the filtering is done to restore it."
585 (let ((filtered nil))
586 (dolist (current parameters)
587 ;; When saving, the parameter alist is temporary, so modifying it
588 ;; is not a problem. When restoring, the parameter alist is part
589 ;; of a frameset, so we must copy parameters to avoid inadvertent
590 ;; modifications.
591 (pcase (cdr (assq (car current) filter-alist))
592 (`nil
593 (push (if saving current (copy-tree current)) filtered))
594 (:never
595 nil)
596 (:restore
597 (unless saving (push (copy-tree current) filtered)))
598 (:save
599 (when saving (push current filtered)))
600 ((or `(,fun . ,args) (and fun (pred fboundp)))
601 (let* ((this (apply fun current filtered parameters saving args))
602 (val (if (eq this t) current this)))
603 (when val
604 (push (if saving val (copy-tree val)) filtered))))
605 (other
606 (delay-warning 'frameset (format "Unknown filter %S" other) :error))))
607 ;; Set the display parameter after filtering, so that filter functions
608 ;; have access to its original value.
609 (when frameset--target-display
610 (let ((display (assq 'display filtered)))
611 (if display
612 (setcdr display (cdr frameset--target-display))
613 (push frameset--target-display filtered))))
614 filtered))
615
616 \f
617 ;; Frame ids
618
619 (defun frameset--set-id (frame)
620 "Set FRAME's id if not yet set.
621 Internal use only."
622 (unless (frame-parameter frame 'frameset--id)
623 (set-frame-parameter frame
624 'frameset--id
625 (mapconcat (lambda (n) (format "%04X" n))
626 (cl-loop repeat 4 collect (random 65536))
627 "-"))))
628 ;;;###autoload
629 (defun frameset-frame-id (frame)
630 "Return the frame id of FRAME, if it has one; else, return nil.
631 A frame id is a string that uniquely identifies a frame.
632 It is persistent across `frameset-save' / `frameset-restore'
633 invocations, and once assigned is never changed unless the same
634 frame is duplicated (via `frameset-restore'), in which case the
635 newest frame keeps the id and the old frame's is set to nil."
636 (frame-parameter frame 'frameset--id))
637
638 ;;;###autoload
639 (defun frameset-frame-id-equal-p (frame id)
640 "Return non-nil if FRAME's id matches ID."
641 (string= (frameset-frame-id frame) id))
642
643 ;;;###autoload
644 (defun frameset-frame-with-id (id &optional frame-list)
645 "Return the live frame with id ID, if exists; else nil.
646 If FRAME-LIST is a list of frames, check these frames only.
647 If nil, check all live frames."
648 (cl-find-if (lambda (f)
649 (and (frame-live-p f)
650 (frameset-frame-id-equal-p f id)))
651 (or frame-list (frame-list))))
652
653 \f
654 ;; Saving framesets
655
656 (defun frameset--record-minibuffer-relationships (frame-list)
657 "Process FRAME-LIST and record minibuffer relationships.
658 FRAME-LIST is a list of frames. Internal use only."
659 ;; Record frames with their own minibuffer
660 (dolist (frame (minibuffer-frame-list))
661 (when (memq frame frame-list)
662 (frameset--set-id frame)
663 ;; For minibuffer-owning frames, frameset--mini is a cons
664 ;; (t . DEFAULT?), where DEFAULT? is a boolean indicating whether
665 ;; the frame is the one pointed out by `default-minibuffer-frame'.
666 (set-frame-parameter frame
667 'frameset--mini
668 (cons t (eq frame default-minibuffer-frame)))))
669 ;; Now link minibufferless frames with their minibuffer frames
670 (dolist (frame frame-list)
671 (unless (frame-parameter frame 'frameset--mini)
672 (frameset--set-id frame)
673 (let* ((mb-frame (window-frame (minibuffer-window frame)))
674 (id (and mb-frame (frameset-frame-id mb-frame))))
675 (if (null id)
676 (error "Minibuffer frame %S for %S is not being saved" mb-frame frame)
677 ;; For minibufferless frames, frameset--mini is a cons
678 ;; (nil . FRAME-ID), where FRAME-ID is the frameset--id
679 ;; of the frame containing its minibuffer window.
680 (set-frame-parameter frame
681 'frameset--mini
682 (cons nil id)))))))
683
684 ;;;###autoload
685 (cl-defun frameset-save (frame-list
686 &key app name description
687 filters predicate properties)
688 "Return a frameset for FRAME-LIST, a list of frames.
689 Dead frames and non-frame objects are silently removed from the list.
690 If nil, FRAME-LIST defaults to the output of `frame-list' (all live frames).
691 APP, NAME and DESCRIPTION are optional data; see the docstring of the
692 `frameset' defstruct for details.
693 FILTERS is an alist of parameter filters; if nil, the value of the variable
694 `frameset-filter-alist' is used instead.
695 PREDICATE is a predicate function, which must return non-nil for frames that
696 should be saved; if PREDICATE is nil, all frames from FRAME-LIST are saved.
697 PROPERTIES is a user-defined property list to add to the frameset."
698 (let* ((list (or (copy-sequence frame-list) (frame-list)))
699 (frames (cl-delete-if-not #'frame-live-p
700 (if predicate
701 (cl-delete-if-not predicate list)
702 list))))
703 (frameset--record-minibuffer-relationships frames)
704 (make-frameset :app app
705 :name name
706 :description description
707 :properties properties
708 :states (mapcar
709 (lambda (frame)
710 (cons
711 (frameset-filter-params (frame-parameters frame)
712 (or filters
713 frameset-filter-alist)
714 t)
715 (window-state-get (frame-root-window frame) t)))
716 frames))))
717
718 \f
719 ;; Restoring framesets
720
721 (defvar frameset--reuse-list nil
722 "The list of frames potentially reusable.
723 Its value is only meaningful during execution of `frameset-restore'.
724 Internal use only.")
725
726 (defun frameset-compute-pos (value left/top right/bottom)
727 "Return an absolute positioning value for a frame.
728 VALUE is the value of a positional frame parameter (`left' or `top').
729 If VALUE is relative to the screen edges (like (+ -35) or (-200), it is
730 converted to absolute by adding it to the corresponding edge; if it is
731 an absolute position, it is returned unmodified.
732 LEFT/TOP and RIGHT/BOTTOM indicate the dimensions of the screen in
733 pixels along the relevant direction: either the position of the left
734 and right edges for a `left' positional parameter, or the position of
735 the top and bottom edges for a `top' parameter."
736 (pcase value
737 (`(+ ,val) (+ left/top val))
738 (`(- ,val) (+ right/bottom val))
739 (val val)))
740
741 (defun frameset-move-onscreen (frame force-onscreen)
742 "If FRAME is offscreen, move it back onscreen and, if necessary, resize it.
743 For the description of FORCE-ONSCREEN, see `frameset-restore'.
744 When forced onscreen, frames wider than the monitor's workarea are converted
745 to fullwidth, and frames taller than the workarea are converted to fullheight.
746 NOTE: This only works for non-iconified frames."
747 (pcase-let* ((`(,left ,top ,width ,height) (cl-cdadr (frame-monitor-attributes frame)))
748 (right (+ left width -1))
749 (bottom (+ top height -1))
750 (fr-left (frameset-compute-pos (frame-parameter frame 'left) left right))
751 (fr-top (frameset-compute-pos (frame-parameter frame 'top) top bottom))
752 (ch-width (frame-char-width frame))
753 (ch-height (frame-char-height frame))
754 (fr-width (max (frame-pixel-width frame) (* ch-width (frame-width frame))))
755 (fr-height (max (frame-pixel-height frame) (* ch-height (frame-height frame))))
756 (fr-right (+ fr-left fr-width -1))
757 (fr-bottom (+ fr-top fr-height -1)))
758 (when (pcase force-onscreen
759 ;; A predicate.
760 ((pred functionp)
761 (funcall force-onscreen
762 frame
763 (list fr-left fr-top fr-width fr-height)
764 (list left top width height)))
765 ;; Any corner is outside the screen.
766 (:all (or (< fr-bottom top) (> fr-bottom bottom)
767 (< fr-left left) (> fr-left right)
768 (< fr-right left) (> fr-right right)
769 (< fr-top top) (> fr-top bottom)))
770 ;; Displaced to the left, right, above or below the screen.
771 (`t (or (> fr-left right)
772 (< fr-right left)
773 (> fr-top bottom)
774 (< fr-bottom top)))
775 ;; Fully inside, no need to do anything.
776 (_ nil))
777 (let ((fullwidth (> fr-width width))
778 (fullheight (> fr-height height))
779 (params nil))
780 ;; Position frame horizontally.
781 (cond (fullwidth
782 (push `(left . ,left) params))
783 ((> fr-right right)
784 (push `(left . ,(+ left (- width fr-width))) params))
785 ((< fr-left left)
786 (push `(left . ,left) params)))
787 ;; Position frame vertically.
788 (cond (fullheight
789 (push `(top . ,top) params))
790 ((> fr-bottom bottom)
791 (push `(top . ,(+ top (- height fr-height))) params))
792 ((< fr-top top)
793 (push `(top . ,top) params)))
794 ;; Compute fullscreen state, if required.
795 (when (or fullwidth fullheight)
796 (push (cons 'fullscreen
797 (cond ((not fullwidth) 'fullheight)
798 ((not fullheight) 'fullwidth)
799 (t 'maximized)))
800 params))
801 ;; Finally, move the frame back onscreen.
802 (when params
803 (modify-frame-parameters frame params))))))
804
805 (defun frameset--find-frame-if (predicate display &rest args)
806 "Find a frame in `frameset--reuse-list' satisfying PREDICATE.
807 Look through available frames whose display property matches DISPLAY
808 and return the first one for which (PREDICATE frame ARGS) returns t.
809 If PREDICATE is nil, it is always satisfied. Internal use only."
810 (cl-find-if (lambda (frame)
811 (and (equal (frame-parameter frame 'display) display)
812 (or (null predicate)
813 (apply predicate frame args))))
814 frameset--reuse-list))
815
816 (defun frameset--reuse-frame (display parameters)
817 "Return an existing frame to reuse, or nil if none found.
818 DISPLAY is the display where the frame will be shown, and PARAMETERS
819 is the parameter alist of the frame being restored. Internal use only."
820 (let ((frame nil)
821 mini)
822 ;; There are no fancy heuristics there. We could implement some
823 ;; based on frame size and/or position, etc., but it is not clear
824 ;; that any "gain" (in the sense of reduced flickering, etc.) is
825 ;; worth the added complexity. In fact, the code below mainly
826 ;; tries to work nicely when M-x desktop-read is used after a
827 ;; desktop session has already been loaded. The other main use
828 ;; case, which is the initial desktop-read upon starting Emacs,
829 ;; will usually have only one frame, and should already work.
830 (cond ((null display)
831 ;; When the target is tty, every existing frame is reusable.
832 (setq frame (frameset--find-frame-if nil display)))
833 ((car (setq mini (cdr (assq 'frameset--mini parameters))))
834 ;; If the frame has its own minibuffer, let's see whether
835 ;; that frame has already been loaded (which can happen after
836 ;; M-x desktop-read).
837 (setq frame (frameset--find-frame-if
838 (lambda (f id)
839 (frameset-frame-id-equal-p f id))
840 display (cdr (assq 'frameset--id parameters))))
841 ;; If it has not been loaded, and it is not a minibuffer-only frame,
842 ;; let's look for an existing non-minibuffer-only frame to reuse.
843 (unless (or frame (eq (cdr (assq 'minibuffer parameters)) 'only))
844 (setq frame (frameset--find-frame-if
845 (lambda (f)
846 (let ((w (frame-parameter f 'minibuffer)))
847 (and (window-live-p w)
848 (window-minibuffer-p w)
849 (eq (window-frame w) f))))
850 display))))
851 (mini
852 ;; For minibufferless frames, check whether they already exist,
853 ;; and that they are linked to the right minibuffer frame.
854 (setq frame (frameset--find-frame-if
855 (lambda (f id mini-id)
856 (and (frameset-frame-id-equal-p f id)
857 (frameset-frame-id-equal-p (window-frame
858 (minibuffer-window f))
859 mini-id)))
860 display (cdr (assq 'frameset--id parameters)) (cdr mini))))
861 (t
862 ;; Default to just finding a frame in the same display.
863 (setq frame (frameset--find-frame-if nil display))))
864 ;; If found, remove from the list.
865 (when frame
866 (setq frameset--reuse-list (delq frame frameset--reuse-list)))
867 frame))
868
869 (defun frameset--initial-params (parameters)
870 "Return a list of PARAMETERS that must be set when creating the frame.
871 Setting position and size parameters as soon as possible helps reducing
872 flickering; other parameters, like `minibuffer' and `border-width', can
873 not be changed once the frame has been created. Internal use only."
874 (cl-loop for param in '(left top with height border-width minibuffer)
875 collect (assq param parameters)))
876
877 (defun frameset--restore-frame (parameters window-state filters force-onscreen)
878 "Set up and return a frame according to its saved state.
879 That means either reusing an existing frame or creating one anew.
880 PARAMETERS is the frame's parameter alist; WINDOW-STATE is its window state.
881 For the meaning of FILTERS and FORCE-ONSCREEN, see `frameset-restore'.
882 Internal use only."
883 (let* ((fullscreen (cdr (assq 'fullscreen parameters)))
884 (lines (assq 'tool-bar-lines parameters))
885 (filtered-cfg (frameset-filter-params parameters filters nil))
886 (display (cdr (assq 'display filtered-cfg))) ;; post-filtering
887 alt-cfg frame)
888
889 ;; This works around bug#14795 (or feature#14795, if not a bug :-)
890 (setq filtered-cfg (assq-delete-all 'tool-bar-lines filtered-cfg))
891 (push '(tool-bar-lines . 0) filtered-cfg)
892
893 (when fullscreen
894 ;; Currently Emacs has the limitation that it does not record the size
895 ;; and position of a frame before maximizing it, so we cannot save &
896 ;; restore that info. Instead, when restoring, we resort to creating
897 ;; invisible "fullscreen" frames of default size and then maximizing them
898 ;; (and making them visible) which at least is somewhat user-friendly
899 ;; when these frames are later de-maximized.
900 (let ((width (and (eq fullscreen 'fullheight) (cdr (assq 'width filtered-cfg))))
901 (height (and (eq fullscreen 'fullwidth) (cdr (assq 'height filtered-cfg))))
902 (visible (assq 'visibility filtered-cfg)))
903 (setq filtered-cfg (cl-delete-if (lambda (p)
904 (memq p '(visibility fullscreen width height)))
905 filtered-cfg :key #'car))
906 (when width
907 (setq filtered-cfg (append `((user-size . t) (width . ,width))
908 filtered-cfg)))
909 (when height
910 (setq filtered-cfg (append `((user-size . t) (height . ,height))
911 filtered-cfg)))
912 ;; These are parameters to apply after creating/setting the frame.
913 (push visible alt-cfg)
914 (push (cons 'fullscreen fullscreen) alt-cfg)))
915
916 ;; Time to find or create a frame an apply the big bunch of parameters.
917 ;; If a frame needs to be created and it falls partially or fully offscreen,
918 ;; sometimes it gets "pushed back" onscreen; however, moving it afterwards is
919 ;; allowed. So we create the frame as invisible and then reapply the full
920 ;; parameter alist (including position and size parameters).
921 (setq frame (or (and frameset--reuse-list
922 (frameset--reuse-frame display filtered-cfg))
923 (make-frame-on-display display
924 (cons '(visibility)
925 (frameset--initial-params filtered-cfg)))))
926 (modify-frame-parameters frame
927 (if (eq (frame-parameter frame 'fullscreen) fullscreen)
928 ;; Workaround for bug#14949
929 (assq-delete-all 'fullscreen filtered-cfg)
930 filtered-cfg))
931
932 ;; If requested, force frames to be onscreen.
933 (when (and force-onscreen
934 ;; FIXME: iconified frames should be checked too,
935 ;; but it is impossible without deiconifying them.
936 (not (eq (frame-parameter frame 'visibility) 'icon)))
937 (frameset-move-onscreen frame force-onscreen))
938
939 ;; Let's give the finishing touches (visibility, tool-bar, maximization).
940 (when lines (push lines alt-cfg))
941 (when alt-cfg (modify-frame-parameters frame alt-cfg))
942 ;; Now restore window state.
943 (window-state-put window-state (frame-root-window frame) 'safe)
944 frame))
945
946 (defun frameset--minibufferless-last-p (state1 state2)
947 "Predicate to sort frame states in an order suitable for creating frames.
948 It sorts minibuffer-owning frames before minibufferless ones.
949 Internal use only."
950 (pcase-let ((`(,hasmini1 ,id-def1) (assq 'frameset--mini (car state1)))
951 (`(,hasmini2 ,id-def2) (assq 'frameset--mini (car state2))))
952 (cond ((eq id-def1 t) t)
953 ((eq id-def2 t) nil)
954 ((not (eq hasmini1 hasmini2)) (eq hasmini1 t))
955 ((eq hasmini1 nil) (string< id-def1 id-def2))
956 (t t))))
957
958 (defun frameset-keep-original-display-p (force-display)
959 "True if saved frames' displays should be honored.
960 For the meaning of FORCE-DISPLAY, see `frameset-restore'."
961 (cond ((daemonp) t)
962 ((eq system-type 'windows-nt) nil) ;; Does ns support more than one display?
963 (t (not force-display))))
964
965 (defun frameset-minibufferless-first-p (frame1 _frame2)
966 "Predicate to sort minibufferless frames before other frames."
967 (not (frame-parameter frame1 'minibuffer)))
968
969 ;;;###autoload
970 (cl-defun frameset-restore (frameset
971 &key predicate filters reuse-frames
972 force-display force-onscreen)
973 "Restore a FRAMESET into the current display(s).
974
975 PREDICATE is a function called with two arguments, the parameter alist
976 and the window-state of the frame being restored, in that order (see
977 the docstring of the `frameset' defstruct for additional details).
978 If PREDICATE returns nil, the frame described by that parameter alist
979 and window-state is not restored.
980
981 FILTERS is an alist of parameter filters; if nil, the value of
982 `frameset-filter-alist' is used instead.
983
984 REUSE-FRAMES selects the policy to use to reuse frames when restoring:
985 t Reuse existing frames if possible, and delete those not reused.
986 nil Restore frameset in new frames and delete existing frames.
987 :keep Restore frameset in new frames and keep the existing ones.
988 LIST A list of frames to reuse; only these are reused (if possible).
989 Remaining frames in this list are deleted; other frames not
990 included on the list are left untouched.
991
992 FORCE-DISPLAY can be:
993 t Frames are restored in the current display.
994 nil Frames are restored, if possible, in their original displays.
995 :delete Frames in other displays are deleted instead of restored.
996 PRED A function called with two arguments, the parameter alist and
997 the window state (in that order). It must return t, nil or
998 `:delete', as above but affecting only the frame that will
999 be created from that parameter alist.
1000
1001 FORCE-ONSCREEN can be:
1002 t Force onscreen only those frames that are fully offscreen.
1003 nil Do not force any frame back onscreen.
1004 :all Force onscreen any frame fully or partially offscreen.
1005 PRED A function called with three arguments,
1006 - the live frame just restored,
1007 - a list (LEFT TOP WIDTH HEIGHT), describing the frame,
1008 - a list (LEFT TOP WIDTH HEIGHT), describing the workarea.
1009 It must return non-nil to force the frame onscreen, nil otherwise.
1010
1011 Note the timing and scope of the operations described above: REUSE-FRAMES
1012 affects existing frames; PREDICATE, FILTERS and FORCE-DISPLAY affect the frame
1013 being restored before that happens; and FORCE-ONSCREEN affects the frame once
1014 it has been restored.
1015
1016 All keyword parameters default to nil."
1017
1018 (cl-assert (frameset-p frameset))
1019
1020 (let (other-frames)
1021
1022 ;; frameset--reuse-list is a list of frames potentially reusable. Later we
1023 ;; will decide which ones can be reused, and how to deal with any leftover.
1024 (pcase reuse-frames
1025 ((or `nil `:keep)
1026 (setq frameset--reuse-list nil
1027 other-frames (frame-list)))
1028 ((pred consp)
1029 (setq frameset--reuse-list (copy-sequence reuse-frames)
1030 other-frames (cl-delete-if (lambda (frame)
1031 (memq frame frameset--reuse-list))
1032 (frame-list))))
1033 (_
1034 (setq frameset--reuse-list (frame-list)
1035 other-frames nil)))
1036
1037 ;; Sort saved states to guarantee that minibufferless frames will be created
1038 ;; after the frames that contain their minibuffer windows.
1039 (dolist (state (sort (copy-sequence (frameset-states frameset))
1040 #'frameset--minibufferless-last-p))
1041 (pcase-let ((`(,frame-cfg . ,window-cfg) state))
1042 (when (or (null predicate) (funcall predicate frame-cfg window-cfg))
1043 (condition-case-unless-debug err
1044 (let* ((d-mini (cdr (assq 'frameset--mini frame-cfg)))
1045 (mb-id (cdr d-mini))
1046 (default (and (booleanp mb-id) mb-id))
1047 (force-display (if (functionp force-display)
1048 (funcall force-display frame-cfg window-cfg)
1049 force-display))
1050 frame to-tty)
1051 ;; Only set target if forcing displays and the target display is different.
1052 (cond ((frameset-keep-original-display-p force-display)
1053 (setq frameset--target-display nil))
1054 ((eq (frame-parameter nil 'display) (cdr (assq 'display frame-cfg)))
1055 (setq frameset--target-display nil))
1056 (t
1057 (setq frameset--target-display (cons 'display
1058 (frame-parameter nil 'display))
1059 to-tty (null (cdr frameset--target-display)))))
1060 ;; Time to restore frames and set up their minibuffers as they were.
1061 ;; We only skip a frame (thus deleting it) if either:
1062 ;; - we're switching displays, and the user chose the option to delete, or
1063 ;; - we're switching to tty, and the frame to restore is minibuffer-only.
1064 (unless (and frameset--target-display
1065 (or (eq force-display :delete)
1066 (and to-tty
1067 (eq (cdr (assq 'minibuffer frame-cfg)) 'only))))
1068 ;; If keeping non-reusable frames, and the frameset--id of one of them
1069 ;; matches the id of a frame being restored (because, for example, the
1070 ;; frameset has already been read in the same session), remove the
1071 ;; frameset--id from the non-reusable frame, which is not useful anymore.
1072 (when (and other-frames
1073 (or (eq reuse-frames :keep) (consp reuse-frames)))
1074 (let ((dup (frameset-frame-with-id (cdr (assq 'frameset--id frame-cfg))
1075 other-frames)))
1076 (when dup
1077 (set-frame-parameter dup 'frameset--id nil))))
1078 ;; Restore minibuffers. Some of this stuff could be done in a filter
1079 ;; function, but it would be messy because restoring minibuffers affects
1080 ;; global state; it's best to do it here than add a bunch of global
1081 ;; variables to pass info back-and-forth to/from the filter function.
1082 (cond
1083 ((null d-mini)) ;; No frameset--mini. Process as normal frame.
1084 (to-tty) ;; Ignore minibuffer stuff and process as normal frame.
1085 ((car d-mini) ;; Frame has minibuffer (or it is minibuffer-only).
1086 (when (eq (cdr (assq 'minibuffer frame-cfg)) 'only)
1087 (setq frame-cfg (append '((tool-bar-lines . 0) (menu-bar-lines . 0))
1088 frame-cfg))))
1089 (t ;; Frame depends on other frame's minibuffer window.
1090 (let* ((mb-frame (or (frameset-frame-with-id mb-id)
1091 (error "Minibuffer frame %S not found" mb-id)))
1092 (mb-param (assq 'minibuffer frame-cfg))
1093 (mb-window (minibuffer-window mb-frame)))
1094 (unless (and (window-live-p mb-window)
1095 (window-minibuffer-p mb-window))
1096 (error "Not a minibuffer window %s" mb-window))
1097 (if mb-param
1098 (setcdr mb-param mb-window)
1099 (push (cons 'minibuffer mb-window) frame-cfg)))))
1100 ;; OK, we're ready at last to create (or reuse) a frame and
1101 ;; restore the window config.
1102 (setq frame (frameset--restore-frame frame-cfg window-cfg
1103 (or filters frameset-filter-alist)
1104 force-onscreen))
1105 ;; Set default-minibuffer if required.
1106 (when default (setq default-minibuffer-frame frame))))
1107 (error
1108 (delay-warning 'frameset (error-message-string err) :error))))))
1109
1110 ;; In case we try to delete the initial frame, we want to make sure that
1111 ;; other frames are already visible (discussed in thread for bug#14841).
1112 (sit-for 0 t)
1113
1114 ;; Delete remaining frames, but do not fail if some resist being deleted.
1115 (unless (eq reuse-frames :keep)
1116 (dolist (frame (sort (nconc (if (listp reuse-frames) nil other-frames)
1117 frameset--reuse-list)
1118 ;; Minibufferless frames must go first to avoid
1119 ;; errors when attempting to delete a frame whose
1120 ;; minibuffer window is used by another frame.
1121 #'frameset-minibufferless-first-p))
1122 (condition-case err
1123 (delete-frame frame)
1124 (error
1125 (delay-warning 'frameset (error-message-string err))))))
1126 (setq frameset--reuse-list nil
1127 frameset--target-display nil)
1128
1129 ;; Make sure there's at least one visible frame.
1130 (unless (or (daemonp) (visible-frame-list))
1131 (make-frame-visible (car (frame-list))))))
1132
1133 (provide 'frameset)
1134
1135 ;;; frameset.el ends here