1 ;;; frameset.el --- save and restore frame and window setup -*- lexical-binding: t -*-
3 ;; Copyright (C) 2013-2018 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 <https://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 (cl-defstruct (frameset (:type vector
) :named
45 (:constructor frameset--make
)
46 ;; Copier is defined below.
49 "A frameset encapsulates a serializable view of a set of frames and windows.
51 It contains the following slots, which can be accessed with
52 \(frameset-SLOT fs) and set with (setf (frameset-SLOT fs) VALUE):
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.
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.
78 A frameset is intended to be used through the following simple API:
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.
85 - `frameset-valid-p' checks a frameset's validity.
86 - `frameset-copy' returns a deep copy of a frameset.
87 - `frameset-prop' is a `setf'able accessor for the contents of the
89 - The `frameset-SLOT' accessors described above."
91 (version 1 :read-only t
)
92 (timestamp (current-time) :read-only t
)
99 ;; Add nicer docstrings for built-in predicate and accessors.
100 (put 'frameset-p
'function-documentation
101 "Return non-nil if OBJECT is a frameset, nil otherwise.\n\n(fn OBJECT)")
102 (put 'frameset-version
'function-documentation
103 "Return the version number of FRAMESET.\n
104 It is an integer that identifies the format of the frameset struct.
105 This slot cannot be modified.\n\n(fn FRAMESET)")
106 (put 'frameset-timestamp
'function-documentation
107 "Return the creation timestamp of FRAMESET.\n
108 The value is in the format returned by `current-time'.
109 This slot cannot be modified.\n\n(fn FRAMESET)")
110 (put 'frameset-app
'function-documentation
111 "Return the application identifier for FRAMESET.\n
112 The value is either a symbol, like `my-app', or a list
113 \(my-app ADDITIONAL-DATA...).\n\n(fn FRAMESET)")
114 (put 'frameset-name
'function-documentation
115 "Return the name of FRAMESET (a string).\n\n(fn FRAMESET)")
116 (put 'frameset-description
'function-documentation
117 "Return the description of FRAMESET (a string).\n\n(fn FRAMESET)")
118 (put 'frameset-properties
'function-documentation
119 "Return the property list of FRAMESET.\n
120 This list is useful to store both frameset-specific and user-defined
121 serializable data. The simplest way to access and modify it is
122 through `frameset-prop' (which see).\n\n(fn FRAMESET)")
123 (put 'frameset-states
'function-documentation
124 "Return the list of frame states of FRAMESET.\n
125 A frame state is a pair (FRAME-PARAMETERS . WINDOW-STATE), where
126 FRAME-PARAMETERS is a frame's parameter alist, extracted with
127 \(frame-parameters FRAME) and filtered through `frameset-filter-params',
128 and WINDOW-STATE is the output of `window-state-get' applied to the
129 root window of the frame.\n
130 IMPORTANT: Modifying this slot may cause frameset functions to fail,
131 unless the type constraints defined above are respected.\n\n(fn FRAMESET)")
133 ;; We autoloaded this for use in register.el, but now that we use registerv
134 ;; objects, this autoload is not useful any more.
135 ;; ;;;###autoload (autoload 'frameset-p "frameset"
136 ;; ;;;###autoload "Return non-nil if OBJECT is a frameset, nil otherwise." nil)
138 (defun frameset-copy (frameset)
139 "Return a deep copy of FRAMESET.
140 FRAMESET is copied with `copy-tree'."
141 (copy-tree frameset t
))
143 (defun frameset-valid-p (object)
144 "Return non-nil if OBJECT is a valid frameset, nil otherwise."
145 (and (frameset-p object
)
146 (integerp (frameset-version object
))
147 (consp (frameset-timestamp object
))
148 (let ((app (frameset-app object
)))
149 (or (null app
) ; APP is nil
150 (symbolp app
) ; or a symbol
151 (and (consp app
) ; or a list
152 (symbolp (car app
))))) ; starting with a symbol
153 (stringp (or (frameset-name object
) ""))
154 (stringp (or (frameset-description object
) ""))
155 (listp (frameset-properties object
))
156 (let ((states (frameset-states object
)))
158 (cl-every #'consp
(frameset-states object
))))
159 (frameset-version object
))) ; And VERSION is non-nil.
161 (defun frameset--prop-setter (frameset property value
)
162 "Setter function for `frameset-prop'. Internal use only."
163 (setf (frameset-properties frameset
)
164 (plist-put (frameset-properties frameset
) property value
))
167 ;; A setf'able accessor to the frameset's properties
168 (defun frameset-prop (frameset property
)
169 "Return the value for FRAMESET of PROPERTY.
171 Properties can be set with
173 (setf (frameset-prop FRAMESET PROPERTY) NEW-VALUE)"
174 (declare (gv-setter frameset--prop-setter
))
175 (plist-get (frameset-properties frameset
) property
))
180 ;; What's the deal with these "filter alists"?
182 ;; Let's say that Emacs' frame parameters were never designed as a tool to
183 ;; precisely record (or restore) a frame's state. They grew organically,
184 ;; and their uses and behaviors reflect their history. In using them to
185 ;; implement framesets, the unwary implementer, or the prospective package
186 ;; writer willing to use framesets in their code, might fall victim of some
187 ;; unexpected... oddities.
189 ;; You can find frame parameters that:
191 ;; - can be used to get and set some data from the frame's current state
192 ;; (`height', `width')
193 ;; - can be set at creation time, and setting them afterwards has no effect
194 ;; (`window-state', `minibuffer')
195 ;; - can be set at creation time, and setting them afterwards will fail with
196 ;; an error, *unless* you set it to the same value, a noop (`border-width')
197 ;; - act differently when passed at frame creation time, and when set
198 ;; afterwards (`height')
199 ;; - affect the value of other parameters (`name', `visibility')
200 ;; - can be ignored by window managers (most positional args, like `height',
201 ;; `width', `left' and `top', and others, like `auto-raise', `auto-lower')
202 ;; - can be set externally in X resources or Window registry (again, most
203 ;; positional parameters, and also `toolbar-lines', `menu-bar-lines' etc.)
204 ;, - can contain references to live objects (`buffer-list', `minibuffer') or
205 ;; code (`buffer-predicate')
206 ;; - are set automatically, and cannot be changed (`window-id', `parent-id'),
207 ;; but setting them produces no error
208 ;; - have a noticeable effect in some window managers, and are ignored in
209 ;; others (`menu-bar-lines')
210 ;; - can not be safely set in a tty session and then copied back to a GUI
211 ;; session (`font', `background-color', `foreground-color')
215 ;; Which means that, in order to save a parameter alist to disk and read it
216 ;; back later to reconstruct a frame, some processing must be done. That's
217 ;; what `frameset-filter-params' and the `frameset-*-filter-alist' variables
220 ;; First, a clarification. The word "filter" in these names refers to both
221 ;; common meanings of filter: to filter out (i.e., to remove), and to pass
222 ;; through a transformation function (think `filter-buffer-substring').
224 ;; `frameset-filter-params' takes a parameter alist PARAMETERS, a filtering
225 ;; alist FILTER-ALIST, and a flag SAVING to indicate whether we are filtering
226 ;; parameters with the intent of saving a frame or restoring it. It then
227 ;; accumulates an output alist, FILTERED, by checking each parameter in
228 ;; PARAMETERS against FILTER-ALIST and obeying any rule found there. The
229 ;; absence of a rule just means the parameter/value pair (called CURRENT in
230 ;; filtering functions) is copied to FILTERED as is. Keyword values :save,
231 ;; :restore and :never tell the function to copy CURRENT to FILTERED in the
232 ;; respective situations, that is, when saving, restoring, or never at all.
233 ;; Values :save and :restore can be useful, for example, if you already
234 ;; have a saved frameset created with some intent, and want to reuse it for
235 ;; a different objective where the expected parameter list has different
238 ;; Finally, the value can also be a filtering function, or a filtering
239 ;; function plus some arguments. The function is called for each matching
240 ;; parameter, and receives CURRENT (the parameter/value pair being processed),
241 ;; FILTERED (the output alist so far), PARAMETERS (the full parameter alist),
242 ;; SAVING (the save/restore flag), plus any additional ARGS set along the
243 ;; function in the `frameset-*-filter-alist' entry. The filtering function
244 ;; then has the possibility to pass along CURRENT, or reject it altogether,
245 ;; or pass back a (NEW-PARAM . NEW-VALUE) pair, which does not even need to
246 ;; refer to the same parameter (so you can filter `width' and return `height'
247 ;; and vice versa, if you're feeling silly and want to mess with the user's
248 ;; mind). As a help in deciding what to do, the filtering function has
249 ;; access to PARAMETERS, but must not change it in any way. It also has
250 ;; access to FILTERED, which can be modified at will. This allows two or
251 ;; more filters to coordinate themselves, because in general there's no way
252 ;; to predict the order in which they will be run.
254 ;; So, which parameters are filtered by default, and why? Let's see.
256 ;; - `buffer-list', `buried-buffer-list', `buffer-predicate': They contain
257 ;; references to live objects, or in the case of `buffer-predicate', it
258 ;; could also contain an fbound symbol (a predicate function) that could
259 ;; not be defined in a later session.
261 ;; - `window-id', `outer-window-id', `parent-id': They are assigned
262 ;; automatically and cannot be set, so keeping them is harmless, but they
263 ;; add clutter. `window-system' is similar: it's assigned at frame
264 ;; creation, and does not serve any useful purpose later.
266 ;; - `left', `top': Only problematic when saving an iconified frame, because
267 ;; when the frame is iconified they are set to (- 32000), which doesn't
268 ;; really help in restoring the frame. Better to remove them and let the
269 ;; window manager choose a default position for the frame.
271 ;; - `background-color', `foreground-color': In tty frames they can be set
272 ;; to "unspecified-bg" and "unspecified-fg", which aren't understood on
273 ;; GUI sessions. They have to be filtered out when switching from tty to
274 ;; a graphical display.
276 ;; - `tty', `tty-type': These are tty-specific. When switching to a GUI
277 ;; display they do no harm, but they clutter the parameter alist.
279 ;; - `minibuffer': It can contain a reference to a live window, which cannot
280 ;; be serialized. Because of Emacs' idiosyncratic treatment of this
281 ;; parameter, frames created with (minibuffer . t) have a parameter
282 ;; (minibuffer . #<window...>), while frames created with
283 ;; (minibuffer . #<window...>) have (minibuffer . nil), which is madness
284 ;; but helps to differentiate between minibufferless and "normal" frames.
285 ;; So, changing (minibuffer . #<window...>) to (minibuffer . t) allows
286 ;; Emacs to set up the new frame correctly. Nice, uh?
288 ;; - `name': If this parameter is directly set, `explicit-name' is
289 ;; automatically set to t, and then `name' no longer changes dynamically.
290 ;; So, in general, not saving `name' is the right thing to do, though
291 ;; surely there are applications that will want to override this filter.
293 ;; - `frameset--text-pixel-height', `frameset--text-pixel-width': These are used to
294 ;; save the pixel width and height of a frame. They are necessary
295 ;; during restore, but should not be set on the actual frame after
296 ;; restoring, so `:save' is used to ensure they are only saved.
298 ;; - `font', `fullscreen', `height' and `width': These parameters suffer
299 ;; from the fact that they are badly mangled when going through a
300 ;; tty session, though not all in the same way. When saving a GUI frame
301 ;; and restoring it in a tty, the height and width of the new frame are
302 ;; those of the tty screen (let's say 80x25, for example); going back
303 ;; to a GUI session means getting frames of the tty screen size (so all
304 ;; your frames are 80 cols x 25 rows). For `fullscreen' there's a
305 ;; similar problem, because a tty frame cannot really be fullscreen or
306 ;; maximized, so the state is lost. The problem with `font' is a bit
307 ;; different, because a valid GUI font spec in `font' turns into
308 ;; (font . "tty") in a tty frame, and when read back into a GUI session
309 ;; it fails because `font's value is no longer a valid font spec.
311 ;; In most cases, the filtering functions just do the obvious thing: remove
312 ;; CURRENT when it is meaningless to keep it, or pass a modified copy if
313 ;; that helps (as in the case of `minibuffer').
315 ;; The exception are the parameters in the last set, which should survive
316 ;; the roundtrip though tty-land. The answer is to add "stashing
317 ;; parameters", working in pairs, to shelve the GUI-specific contents and
318 ;; restore it once we're back in pixel country. That's what functions
319 ;; `frameset-filter-shelve-param' and `frameset-filter-unshelve-param' do.
321 ;; Basically, if you set `frameset-filter-shelve-param' as the filter for
322 ;; a parameter P, it will detect when it is restoring a GUI frame into a
323 ;; tty session, and save P's value in the custom parameter X:P, but only
324 ;; if X:P does not exist already (so it is not overwritten if you enter
325 ;; the tty session more than once). If you're not switching to a tty
326 ;; frame, the filter just passes CURRENT along.
328 ;; The parameter X:P, on the other hand, must have been setup to be
329 ;; filtered by `frameset-filter-unshelve-param', which unshelves the
330 ;; value: if we're entering a GUI session, returns P instead of CURRENT,
331 ;; while in other cases it just passes it along.
333 ;; The only additional trick is that `frameset-filter-shelve-param' does
334 ;; not set P if switching back to GUI and P already has a value, because
335 ;; it assumes that `frameset-filter-unshelve-param' did set it up. And
336 ;; `frameset-filter-unshelve-param', when unshelving P, must look into
337 ;; FILTERED to determine if P has already been set and if so, modify it;
338 ;; else just returns P.
340 ;; Currently, the value of X in X:P is `GUI', but you can use any prefix,
341 ;; by passing its symbol as argument in the filter:
343 ;; (my-parameter frameset-filter-shelve-param MYPREFIX)
347 ;; (my-parameter . frameset-filter-shelve-param)
349 ;; Note that `frameset-filter-unshelve-param' does not need MYPREFIX
350 ;; because it is available from the parameter name in CURRENT. Also note
351 ;; that the colon between the prefix and the parameter name is hardcoded.
352 ;; The reason is that X:P is quite readable, and that the colon is a
353 ;; very unusual character in symbol names, other than in initial position
354 ;; in keywords (emacs -Q has only two such symbols, and one of them is a
355 ;; URL). So the probability of a collision with existing or future
356 ;; symbols is quite insignificant.
358 ;; Now, what about the filter alist variables? There are three of them,
359 ;; though only two sets of parameters:
361 ;; - `frameset-session-filter-alist' contains these filters that allow
362 ;; saving and restoring framesets in-session, without the need to
363 ;; serialize the frameset or save it to disk (for example, to save a
364 ;; frameset in a register and restore it later). Filters in this
365 ;; list do not remove live objects, except in `minibuffer', which is
366 ;; dealt especially by `frameset-save' / `frameset-restore'.
368 ;; - `frameset-persistent-filter-alist' is the whole deal. It does all
369 ;; the filtering described above, and the result is ready to be saved on
370 ;; disk without loss of information. That's the format used by the
371 ;; desktop.el package, for example.
373 ;; IMPORTANT: These variables share structure and should NEVER be modified.
375 ;; - `frameset-filter-alist': The value of this variable is the default
376 ;; value for the FILTERS arguments of `frameset-save' and
377 ;; `frameset-restore'. It is set to `frameset-persistent-filter-alist',
378 ;; though it can be changed by specific applications.
382 ;; The simplest way is just do nothing. The default should work
383 ;; reasonably and sensibly enough. But, what if you really need a
384 ;; customized filter alist? Then you can create your own variable
386 ;; (defvar my-filter-alist
387 ;; '((my-param1 . :never)
388 ;; (my-param2 . :save)
389 ;; (my-param3 . :restore)
390 ;; (my-param4 . my-filtering-function-without-args)
391 ;; (my-param5 my-filtering-function-with arg1 arg2)
392 ;; ;;; many other parameters
394 ;; "My customized parameter filter alist.")
396 ;; or, if you're only changing a few items,
398 ;; (defvar my-filter-alist
399 ;; (nconc '((my-param1 . :never)
400 ;; (my-param2 . my-filtering-function))
401 ;; frameset-filter-alist)
402 ;; "My brief customized parameter filter alist.")
404 ;; and pass it to the FILTER arg of the save/restore functions,
405 ;; ALWAYS taking care of not modifying the original lists; if you're
406 ;; going to do any modifying of my-filter-alist, please use
408 ;; (nconc '((my-param1 . :never) ...)
409 ;; (copy-sequence frameset-filter-alist))
411 ;; One thing you shouldn't forget is that they are alists, so searching
412 ;; in them is sequential. If you just want to change the default of
413 ;; `name' to allow it to be saved, you can set (name . nil) in your
414 ;; customized filter alist; it will take precedence over the latter
415 ;; setting. In case you decide that you *always* want to save `name',
416 ;; you can add it to `frameset-filter-alist':
418 ;; (push '(name . nil) frameset-filter-alist)
420 ;; In certain applications, having a parameter filtering function like
421 ;; `frameset-filter-params' can be useful, even if you're not using
422 ;; framesets. The interface of `frameset-filter-params' is generic
423 ;; and does not depend of global state, with one exception: it uses
424 ;; the dynamically bound variable `frameset--target-display' to decide
425 ;; if, and how, to modify the `display' parameter of FILTERED. That
426 ;; should not represent a problem, because it's only meaningful when
427 ;; restoring, and customized uses of `frameset-filter-params' are
428 ;; likely to use their own filter alist and just call
430 ;; (setq my-filtered (frameset-filter-params my-params my-filters t))
432 ;; In case you want to use it with the standard filters, you can
433 ;; wrap the call to `frameset-filter-params' in a let form to bind
434 ;; `frameset--target-display' to nil or the desired value.
438 (defvar frameset-session-filter-alist
440 (left . frameset-filter-iconified
)
441 (minibuffer . frameset-filter-minibuffer
)
442 (top . frameset-filter-iconified
))
443 "Minimum set of parameters to filter for live (on-session) framesets.
444 DO NOT MODIFY. See `frameset-filter-alist' for a full description.")
447 (defvar frameset-persistent-filter-alist
449 '((background-color . frameset-filter-sanitize-color
)
450 (buffer-list .
:never
)
451 (buffer-predicate .
:never
)
452 (buried-buffer-list .
:never
)
453 ;; Don't save the 'client' parameter to avoid that a subsequent
454 ;; `save-buffers-kill-terminal' in a non-client session barks at
455 ;; the user (Bug#29067).
457 (delete-before .
:never
)
458 (font . frameset-filter-font-param
)
459 (foreground-color . frameset-filter-sanitize-color
)
460 (frameset--text-pixel-height .
:save
)
461 (frameset--text-pixel-width .
:save
)
462 (fullscreen . frameset-filter-shelve-param
)
463 (GUI:font . frameset-filter-unshelve-param
)
464 (GUI:fullscreen . frameset-filter-unshelve-param
)
465 (GUI:height . frameset-filter-unshelve-param
)
466 (GUI:width . frameset-filter-unshelve-param
)
467 (height . frameset-filter-shelve-param
)
468 (outer-window-id .
:never
)
469 (parent-frame .
:never
)
471 (mouse-wheel-frame .
:never
)
472 (tty . frameset-filter-tty-to-GUI
)
473 (tty-type . frameset-filter-tty-to-GUI
)
474 (width . frameset-filter-shelve-param
)
476 (window-system .
:never
))
477 frameset-session-filter-alist
)
478 "Parameters to filter for persistent framesets.
479 DO NOT MODIFY. See `frameset-filter-alist' for a full description.")
482 (defvar frameset-filter-alist frameset-persistent-filter-alist
483 "Alist of frame parameters and filtering functions.
485 This alist is the default value of the FILTERS argument of
486 `frameset-save' and `frameset-restore' (which see).
488 Initially, `frameset-filter-alist' is set to, and shares the value of,
489 `frameset-persistent-filter-alist'. You can override any item in
490 this alist by `push'ing a new item onto it. If, for some reason, you
491 intend to modify existing values, do
493 (setq frameset-filter-alist (copy-tree frameset-filter-alist))
495 before changing anything.
497 On saving, PARAMETERS is the parameter alist of each frame processed,
498 and FILTERED is the parameter alist that gets saved to the frameset.
500 On restoring, PARAMETERS is the parameter alist extracted from the
501 frameset, and FILTERED is the resulting frame parameter alist used
502 to restore the frame.
504 Elements of `frameset-filter-alist' are conses (PARAM . ACTION),
505 where PARAM is a parameter name (a symbol identifying a frame
506 parameter), and ACTION can be:
508 nil The parameter is copied to FILTERED.
509 :never The parameter is never copied to FILTERED.
510 :save The parameter is copied only when saving the frame.
511 :restore The parameter is copied only when restoring the frame.
512 FILTER A filter function.
514 FILTER can be a symbol FILTER-FUN, or a list (FILTER-FUN ARGS...).
515 FILTER-FUN is invoked with
517 (apply FILTER-FUN CURRENT FILTERED PARAMETERS SAVING ARGS)
521 CURRENT A cons (PARAM . VALUE), where PARAM is the one being
522 filtered and VALUE is its current value.
523 FILTERED The resulting alist (so far).
524 PARAMETERS The complete alist of parameters being filtered,
525 SAVING Non-nil if filtering before saving state, nil if filtering
527 ARGS Any additional arguments specified in the ACTION.
529 FILTER-FUN is allowed to modify items in FILTERED, but no other arguments.
531 nil Skip CURRENT (do not add it to FILTERED).
532 t Add CURRENT to FILTERED as is.
533 (NEW-PARAM . NEW-VALUE) Add this to FILTERED instead of CURRENT.
535 Frame parameters not on this alist are passed intact, as if they were
536 defined with ACTION = nil.")
538 ;; Dynamically bound in `frameset-save', `frameset-restore'.
539 (defvar frameset--target-display
)
540 ;; Either (display . VALUE) or nil.
541 ;; This refers to the current frame config being processed with
542 ;; `frameset-filter-params' and its auxiliary filtering functions.
543 ;; If nil, there is no need to change the display.
544 ;; If non-nil, display parameter to use when creating the frame.
546 (defun frameset-switch-to-gui-p (parameters)
547 "True when switching to a graphic display.
548 Return non-nil if the parameter alist PARAMETERS describes a frame on a
549 text-only terminal, and the frame is being restored on a graphic display;
550 otherwise return nil. Only meaningful when called from a filtering
551 function in `frameset-filter-alist'."
552 (and frameset--target-display
; we're switching
553 (null (cdr (assq 'display parameters
))) ; from a tty
554 (cdr frameset--target-display
))) ; to a GUI display
556 (defun frameset-switch-to-tty-p (parameters)
557 "True when switching to a text-only terminal.
558 Return non-nil if the parameter alist PARAMETERS describes a frame on a
559 graphic display, and the frame is being restored on a text-only terminal;
560 otherwise return nil. Only meaningful when called from a filtering
561 function in `frameset-filter-alist'."
562 (and frameset--target-display
; we're switching
563 (cdr (assq 'display parameters
)) ; from a GUI display
564 (null (cdr frameset--target-display
)))) ; to a tty
566 (defun frameset-filter-tty-to-GUI (_current _filtered parameters saving
)
567 "Remove CURRENT when switching from tty to a graphic display.
569 For the meaning of CURRENT, FILTERED, PARAMETERS and SAVING,
570 see `frameset-filter-alist'."
572 (not (frameset-switch-to-gui-p parameters
))))
574 (defun frameset-filter-sanitize-color (current _filtered parameters saving
)
575 "When switching to a GUI frame, remove \"unspecified\" colors.
576 Useful as a filter function for tty-specific parameters.
578 For the meaning of CURRENT, FILTERED, PARAMETERS and SAVING,
579 see `frameset-filter-alist'."
581 (not (frameset-switch-to-gui-p parameters
))
582 (not (stringp (cdr current
)))
583 (not (string-match-p "^unspecified-[fb]g$" (cdr current
)))))
585 (defun frameset-filter-minibuffer (current filtered _parameters saving
)
586 "Force the minibuffer parameter to have a sensible value.
588 When saving, convert (minibuffer . #<window>) to (minibuffer . nil).
589 When restoring, if there are two copies, keep the one pointing to
592 For the meaning of CURRENT, FILTERED, PARAMETERS and SAVING,
593 see `frameset-filter-alist'."
594 (let ((value (cdr current
)) mini
)
596 ;; "Fix semantics of 'minibuffer' frame parameter" change:
597 ;; When the cdr of the parameter is a minibuffer window, save
598 ;; (minibuffer . nil) instead of (minibuffer . t).
602 ((setq mini
(assq 'minibuffer filtered
))
603 (when (windowp value
) (setcdr mini value
))
607 (defun frameset-filter-shelve-param (current _filtered parameters saving
609 "When switching to a tty frame, save parameter P as PREFIX:P.
610 The parameter can be later restored with `frameset-filter-unshelve-param'.
611 PREFIX defaults to `GUI'.
613 For the meaning of CURRENT, FILTERED, PARAMETERS and SAVING,
614 see `frameset-filter-alist'."
615 (unless prefix
(setq prefix
'GUI
))
617 ((frameset-switch-to-tty-p parameters
)
618 (let ((prefix:p
(intern (format "%s:%s" prefix
(car current
)))))
619 (if (assq prefix
:p parameters
)
621 (cons prefix
:p
(cdr current
)))))
622 ((frameset-switch-to-gui-p parameters
)
623 (not (assq (intern (format "%s:%s" prefix
(car current
))) parameters
)))
626 (defun frameset-filter-unshelve-param (current filtered parameters saving
)
627 "When switching to a GUI frame, restore PREFIX:P parameter as P.
628 CURRENT must be of the form (PREFIX:P . value).
630 For the meaning of CURRENT, FILTERED, PARAMETERS and SAVING,
631 see `frameset-filter-alist'."
633 (not (frameset-switch-to-gui-p parameters
))
634 (let* ((prefix:p
(symbol-name (car current
)))
635 (p (intern (substring prefix
:p
636 (1+ (string-match-p ":" prefix
:p
)))))
638 (found (assq p filtered
)))
644 (defun frameset-filter-font-param (current filtered parameters saving
646 "When switching from a tty frame to a GUI frame, remove the FONT param.
648 When switching from a GUI frame to a tty frame, behave
649 as `frameset-filter-shelve-param' does."
651 (if (frameset-switch-to-tty-p parameters
)
652 (frameset-filter-shelve-param current filtered parameters saving
655 (defun frameset-filter-iconified (_current _filtered parameters saving
)
656 "Remove CURRENT when saving an iconified frame.
657 This is used for positional parameters `left' and `top', which are
658 meaningless in an iconified frame, so the frame is restored in a
661 For the meaning of CURRENT, FILTERED, PARAMETERS and SAVING,
662 see `frameset-filter-alist'."
663 (not (and saving
(eq (cdr (assq 'visibility parameters
)) 'icon
))))
665 (defun frameset-filter-params (parameters filter-alist saving
)
666 "Filter parameter alist PARAMETERS and return a filtered alist.
667 FILTER-ALIST is an alist of parameter filters, in the format of
668 `frameset-filter-alist' (which see).
669 SAVING is non-nil while filtering parameters to save a frameset,
670 nil while the filtering is done to restore it."
671 (let ((filtered nil
))
672 (dolist (current parameters
)
673 ;; When saving, the parameter alist is temporary, so modifying it
674 ;; is not a problem. When restoring, the parameter alist is part
675 ;; of a frameset, so we must copy parameters to avoid inadvertent
677 (pcase (cdr (assq (car current
) filter-alist
))
679 (push (if saving current
(copy-tree current
)) filtered
))
683 (unless saving
(push (copy-tree current
) filtered
)))
685 (when saving
(push current filtered
)))
686 ((or `(,fun .
,args
) (and fun
(pred fboundp
)))
687 (let* ((this (apply fun current filtered parameters saving args
))
688 (val (if (eq this t
) current this
)))
690 (push (if saving val
(copy-tree val
)) filtered
))))
692 (delay-warning 'frameset
(format "Unknown filter %S" other
) :error
))))
693 ;; Set the display parameter after filtering, so that filter functions
694 ;; have access to its original value.
695 (when frameset--target-display
696 (setf (alist-get 'display filtered
) (cdr frameset--target-display
)))
702 (defun frameset--set-id (frame)
703 "Set FRAME's id if not yet set.
705 (unless (frame-parameter frame
'frameset--id
)
706 (set-frame-parameter frame
708 (mapconcat (lambda (n) (format "%04X" n
))
709 (cl-loop repeat
4 collect
(random 65536))
712 (defun frameset-cfg-id (frame-cfg)
713 "Return the frame id for frame configuration FRAME-CFG."
714 (cdr (assq 'frameset--id frame-cfg
)))
717 (defun frameset-frame-id (frame)
718 "Return the frame id of FRAME, if it has one; else, return nil.
719 A frame id is a string that uniquely identifies a frame.
720 It is persistent across `frameset-save' / `frameset-restore'
721 invocations, and once assigned is never changed unless the same
722 frame is duplicated (via `frameset-restore'), in which case the
723 newest frame keeps the id and the old frame's is set to nil."
724 (frame-parameter frame
'frameset--id
))
727 (defun frameset-frame-id-equal-p (frame id
)
728 "Return non-nil if FRAME's id matches ID."
729 (string= (frameset-frame-id frame
) id
))
732 (defun frameset-frame-with-id (id &optional frame-list
)
733 "Return the live frame with id ID, if exists; else nil.
734 If FRAME-LIST is a list of frames, check these frames only.
735 If nil, check all live frames."
736 (cl-find-if (lambda (f)
737 (and (frame-live-p f
)
738 (frameset-frame-id-equal-p f id
)))
739 (or frame-list
(frame-list))))
744 (defun frameset--record-relationships (frame-list)
745 "Process FRAME-LIST and record relationships.
746 FRAME-LIST is a list of frames.
748 The relationships recorded for each frame are
750 - `minibuffer' via `frameset--mini'
751 - `delete-before' via `frameset--delete-before'
752 - `parent-frame' via `frameset--parent-frame'
753 - `mouse-wheel-frame' via `frameset--mouse-wheel-frame'
754 - `text-pixel-width' via `frameset--text-pixel-width'
755 - `text-pixel-height' via `frameset--text-pixel-height'
758 ;; Record frames with their own minibuffer
759 (dolist (frame (minibuffer-frame-list))
760 (when (memq frame frame-list
)
761 (frameset--set-id frame
)
762 ;; For minibuffer-owning frames, frameset--mini is a cons
763 ;; (t . DEFAULT?), where DEFAULT? is a boolean indicating whether
764 ;; the frame is the one pointed out by `default-minibuffer-frame'.
765 (set-frame-parameter frame
767 (cons t
(eq frame default-minibuffer-frame
)))))
768 ;; Now link minibufferless frames with their minibuffer frames and
769 ;; store `parent-frame', `delete-before' and `mouse-wheel-frame'
770 ;; relationships in a similar way.
771 (dolist (frame frame-list
)
772 (let ((parent-frame (frame-parent frame
))
773 (delete-before (frame-parameter frame
'delete-before
))
774 (mouse-wheel-frame (frame-parameter frame
'mouse-wheel-frame
))
775 (nomini (not (frame-parameter frame
'frameset--mini
))))
776 (when (or nomini parent-frame delete-before mouse-wheel-frame
)
778 (frameset--set-id frame
))
781 frame
'frameset--parent-frame
(frameset-frame-id parent-frame
)))
784 frame
'frameset--delete-before
(frameset-frame-id delete-before
)))
785 (when mouse-wheel-frame
787 frame
'frameset--mouse-wheel-frame
788 (frameset-frame-id mouse-wheel-frame
)))
790 (let ((mb-frame (window-frame (minibuffer-window frame
))))
791 ;; For minibufferless frames, frameset--mini is a cons
792 ;; (nil . FRAME-ID), where FRAME-ID is the frameset--id of
793 ;; the frame containing its minibuffer window.
794 ;; FRAME-ID can be set to nil, if FRAME-LIST doesn't contain
795 ;; the minibuffer frame of a minibufferless frame; we allow
796 ;; it without trying to second-guess the user.
802 (frameset-frame-id mb-frame
)))))))))
803 ;; Now store text-pixel width and height if it differs from the calculated
804 ;; width and height and the frame is not fullscreen.
805 (dolist (frame frame-list
)
806 (unless (frame-parameter frame
'fullscreen
)
807 (unless (eq (* (frame-parameter frame
'width
)
808 (frame-char-width frame
))
809 (frame-text-width frame
))
811 frame
'frameset--text-pixel-width
812 (frame-text-width frame
)))
813 (unless (eq (* (frame-parameter frame
'height
)
814 (frame-char-height frame
))
815 (frame-text-height frame
))
817 frame
'frameset--text-pixel-height
818 (frame-text-height frame
))))))
821 (cl-defun frameset-save (frame-list
822 &key app name description
823 filters predicate properties
)
824 "Return a frameset for FRAME-LIST, a list of frames.
825 Dead frames and non-frame objects are silently removed from the list.
826 If nil, FRAME-LIST defaults to the output of `frame-list' (all live frames).
827 APP, NAME and DESCRIPTION are optional data; see the docstring of the
828 `frameset' defstruct for details.
829 FILTERS is an alist of parameter filters; if nil, the value of the variable
830 `frameset-filter-alist' is used instead.
831 PREDICATE is a predicate function, which must return non-nil for frames that
832 should be saved; if PREDICATE is nil, all frames from FRAME-LIST are saved.
833 PROPERTIES is a user-defined property list to add to the frameset."
834 (let* ((list (or (copy-sequence frame-list
) (frame-list)))
835 (frameset--target-display nil
)
836 (frames (cl-delete-if-not #'frame-live-p
838 (cl-delete-if-not predicate list
)
841 (frameset--record-relationships frames
)
842 (setq fs
(frameset--make
845 :description description
846 :properties properties
850 (frameset-filter-params (frame-parameters frame
)
852 frameset-filter-alist
)
854 (window-state-get (frame-root-window frame
) t
)))
856 (cl-assert (frameset-valid-p fs
))
860 ;; Restoring framesets
862 ;; Dynamically bound in `frameset-restore'.
863 (defvar frameset--reuse-list
)
864 (defvar frameset--action-map
)
866 (defun frameset-compute-pos (value left
/top right
/bottom
)
867 "Return an absolute positioning value for a frame.
868 VALUE is the value of a positional frame parameter (`left' or `top').
869 If VALUE is relative to the screen edges (like (+ -35) or (-200), it is
870 converted to absolute by adding it to the corresponding edge; if it is
871 an absolute position, it is returned unmodified.
872 LEFT/TOP and RIGHT/BOTTOM indicate the dimensions of the screen in
873 pixels along the relevant direction: either the position of the left
874 and right edges for a `left' positional parameter, or the position of
875 the top and bottom edges for a `top' parameter."
877 (`(+ ,val
) (+ left
/top val
))
878 (`(- ,val
) (+ right
/bottom val
))
881 (defun frameset-move-onscreen (frame force-onscreen
)
882 "If FRAME is offscreen, move it back onscreen and, if necessary, resize it.
883 For the description of FORCE-ONSCREEN, see `frameset-restore'.
884 When forced onscreen, frames wider than the monitor's workarea are converted
885 to fullwidth, and frames taller than the workarea are converted to fullheight.
886 NOTE: This only works for non-iconified frames."
887 (pcase-let* ((`(,left
,top
,width
,height
) (cl-cdadr (frame-monitor-attributes frame
)))
888 (right (+ left width -
1))
889 (bottom (+ top height -
1))
890 (fr-left (frameset-compute-pos (frame-parameter frame
'left
) left right
))
891 (fr-top (frameset-compute-pos (frame-parameter frame
'top
) top bottom
))
892 (ch-width (frame-char-width frame
))
893 (ch-height (frame-char-height frame
))
894 (fr-width (max (frame-pixel-width frame
) (* ch-width
(frame-width frame
))))
895 (fr-height (max (frame-pixel-height frame
) (* ch-height
(frame-height frame
))))
896 (fr-right (+ fr-left fr-width -
1))
897 (fr-bottom (+ fr-top fr-height -
1)))
898 (when (pcase force-onscreen
901 (funcall force-onscreen
903 (list fr-left fr-top fr-width fr-height
)
904 (list left top width height
)))
905 ;; Any corner is outside the screen.
906 (:all
(or (< fr-bottom top
) (> fr-bottom bottom
)
907 (< fr-left left
) (> fr-left right
)
908 (< fr-right left
) (> fr-right right
)
909 (< fr-top top
) (> fr-top bottom
)))
910 ;; Displaced to the left, right, above or below the screen.
911 (`t
(or (> fr-left right
)
915 ;; Fully inside, no need to do anything.
917 (let ((fullwidth (> fr-width width
))
918 (fullheight (> fr-height height
))
920 ;; Position frame horizontally.
922 (push `(left .
,left
) params
))
924 (push `(left .
,(+ left
(- width fr-width
))) params
))
926 (push `(left .
,left
) params
)))
927 ;; Position frame vertically.
929 (push `(top .
,top
) params
))
930 ((> fr-bottom bottom
)
931 (push `(top .
,(+ top
(- height fr-height
))) params
))
933 (push `(top .
,top
) params
)))
934 ;; Compute fullscreen state, if required.
935 (when (or fullwidth fullheight
)
936 (push (cons 'fullscreen
937 (cond ((not fullwidth
) 'fullheight
)
938 ((not fullheight
) 'fullwidth
)
941 ;; Finally, move the frame back onscreen.
943 (modify-frame-parameters frame params
))))))
945 (defun frameset--find-frame-if (predicate display
&rest args
)
946 "Find a reusable frame satisfying PREDICATE.
947 Look through available frames whose display property matches DISPLAY
948 and return the first one for which (PREDICATE frame ARGS) returns t.
949 If PREDICATE is nil, it is always satisfied. Internal use only."
950 (cl-find-if (lambda (frame)
951 (and (equal (frame-parameter frame
'display
) display
)
953 (apply predicate frame args
))))
954 frameset--reuse-list
))
956 (defun frameset--reuse-frame (display parameters
)
957 "Return an existing frame to reuse, or nil if none found.
958 DISPLAY is the display where the frame will be shown, and PARAMETERS
959 is the parameter alist of the frame being restored. Internal use only."
962 ;; There are no fancy heuristics there. We could implement some
963 ;; based on frame size and/or position, etc., but it is not clear
964 ;; that any "gain" (in the sense of reduced flickering, etc.) is
965 ;; worth the added complexity. In fact, the code below mainly
966 ;; tries to work nicely when M-x desktop-read is used after a
967 ;; desktop session has already been loaded. The other main use
968 ;; case, which is the initial desktop-read upon starting Emacs,
969 ;; will usually have only one frame, and should already work.
970 (cond ((null display
)
971 ;; When the target is tty, every existing frame is reusable.
972 (setq frame
(frameset--find-frame-if nil display
)))
973 ((car (setq mini
(cdr (assq 'frameset--mini parameters
))))
974 ;; If the frame has its own minibuffer, let's see whether
975 ;; that frame has already been loaded (which can happen after
976 ;; M-x desktop-read).
977 (setq frame
(frameset--find-frame-if
979 (frameset-frame-id-equal-p f id
))
980 display
(frameset-cfg-id parameters
)))
981 ;; If it has not been loaded, and it is not a minibuffer-only frame,
982 ;; let's look for an existing non-minibuffer-only frame to reuse.
983 (unless (or frame
(eq (cdr (assq 'minibuffer parameters
)) 'only
))
984 ;; "Fix semantics of 'minibuffer' frame parameter" change:
985 ;; The 'minibuffer' frame parameter of a non-minibuffer-only
986 ;; frame is t instead of that frame's minibuffer window.
987 (setq frame
(frameset--find-frame-if
989 (eq (frame-parameter f
'minibuffer
) t
))
992 ;; For minibufferless frames, check whether they already exist,
993 ;; and that they are linked to the right minibuffer frame.
994 (setq frame
(frameset--find-frame-if
995 (lambda (f id mini-id
)
996 (and (frameset-frame-id-equal-p f id
)
997 (or (null mini-id
) ; minibuffer frame not saved
998 (frameset-frame-id-equal-p
999 (window-frame (minibuffer-window f
))
1001 display
(frameset-cfg-id parameters
) (cdr mini
))))
1003 ;; Default to just finding a frame in the same display.
1004 (setq frame
(frameset--find-frame-if nil display
))))
1005 ;; If found, remove from the list.
1007 (setq frameset--reuse-list
(delq frame frameset--reuse-list
)))
1010 (defun frameset--initial-params (parameters)
1011 "Return a list of PARAMETERS that must be set when creating the frame.
1012 Setting position and size parameters as soon as possible helps reducing
1013 flickering; other parameters, like `minibuffer' and `border-width', can
1014 not be changed once the frame has been created. Internal use only."
1015 (cl-loop for param in
'(left top width height border-width minibuffer
)
1016 when
(assq param parameters
) collect it
))
1018 (defun frameset--restore-frame (parameters window-state filters force-onscreen
)
1019 "Set up and return a frame according to its saved state.
1020 That means either reusing an existing frame or creating one anew.
1021 PARAMETERS is the frame's parameter alist; WINDOW-STATE is its window state.
1022 For the meaning of FILTERS and FORCE-ONSCREEN, see `frameset-restore'.
1024 (let* ((fullscreen (cdr (assq 'fullscreen parameters
)))
1025 (filtered-cfg (frameset-filter-params parameters filters nil
))
1026 (display (cdr (assq 'display filtered-cfg
))) ;; post-filtering
1029 ;; Use text-pixels for height and width, if available.
1030 (let ((text-pixel-width (cdr (assq 'frameset--text-pixel-width parameters
)))
1031 (text-pixel-height (cdr (assq 'frameset--text-pixel-height parameters
))))
1032 (when text-pixel-width
1033 (setf (alist-get 'width filtered-cfg
) (cons 'text-pixels text-pixel-width
)))
1034 (when text-pixel-height
1035 (setf (alist-get 'height filtered-cfg
) (cons 'text-pixels text-pixel-height
))))
1038 ;; Currently Emacs has the limitation that it does not record the size
1039 ;; and position of a frame before maximizing it, so we cannot save &
1040 ;; restore that info. Instead, when restoring, we resort to creating
1041 ;; invisible "fullscreen" frames of default size and then maximizing them
1042 ;; (and making them visible) which at least is somewhat user-friendly
1043 ;; when these frames are later de-maximized.
1044 (let ((width (and (eq fullscreen
'fullheight
) (cdr (assq 'width filtered-cfg
))))
1045 (height (and (eq fullscreen
'fullwidth
) (cdr (assq 'height filtered-cfg
))))
1046 (visible (assq 'visibility filtered-cfg
)))
1047 (setq filtered-cfg
(cl-delete-if (lambda (p)
1048 (memq p
'(visibility fullscreen width height
)))
1049 filtered-cfg
:key
#'car
))
1051 (setq filtered-cfg
(append `((user-size . t
) (width .
,width
))
1054 (setq filtered-cfg
(append `((user-size . t
) (height .
,height
))
1056 ;; These are parameters to apply after creating/setting the frame.
1057 (push visible alt-cfg
)
1058 (push (cons 'fullscreen fullscreen
) alt-cfg
)))
1060 ;; Time to find or create a frame and apply the big bunch of parameters.
1061 (setq frame
(and frameset--reuse-list
1062 (frameset--reuse-frame display filtered-cfg
)))
1064 (puthash frame
:reused frameset--action-map
)
1065 ;; If a frame needs to be created and it falls partially or fully offscreen,
1066 ;; sometimes it gets "pushed back" onscreen; however, moving it afterwards is
1067 ;; allowed. So we create the frame as invisible and then reapply the full
1068 ;; parameter alist (including position and size parameters).
1069 (setq frame
(make-frame-on-display display
1071 (frameset--initial-params filtered-cfg
))))
1072 (puthash frame
:created frameset--action-map
))
1074 ;; Remove `border-width' from the list of parameters. If it has not
1075 ;; been assigned via `make-frame-on-display', any attempt to assign
1076 ;; it now via `modify-frame-parameters' may result in an error on X
1078 (setq filtered-cfg
(assq-delete-all 'border-width filtered-cfg
))
1080 ;; Try to assign parent-frame right here - it will improve things
1081 ;; for minibuffer-less child frames.
1082 (let* ((frame-id (frame-parameter frame
'frameset--parent-frame
))
1084 (and frame-id
(frameset-frame-with-id frame-id
))))
1085 (when (frame-live-p parent-frame
)
1086 (set-frame-parameter frame
'parent-frame parent-frame
)))
1088 (modify-frame-parameters frame
1089 (if (eq (frame-parameter frame
'fullscreen
) fullscreen
)
1090 ;; Workaround for bug#14949
1091 (assq-delete-all 'fullscreen filtered-cfg
)
1094 ;; If requested, force frames to be onscreen.
1095 (when (and force-onscreen
1096 ;; FIXME: iconified frames should be checked too,
1097 ;; but it is impossible without deiconifying them.
1098 (not (eq (frame-parameter frame
'visibility
) 'icon
)))
1099 (frameset-move-onscreen frame force-onscreen
))
1101 ;; Let's give the finishing touches (visibility, maximization).
1102 (when alt-cfg
(modify-frame-parameters frame alt-cfg
))
1103 ;; Now restore window state.
1104 (window-state-put window-state
(frame-root-window frame
) 'safe
)
1107 (defun frameset--minibufferless-last-p (state1 state2
)
1108 "Predicate to sort frame states in an order suitable for creating frames.
1109 It sorts minibuffer-owning frames before minibufferless ones.
1111 (pcase-let ((`(,hasmini1
,id-def1
) (assq 'frameset--mini
(car state1
)))
1112 (`(,hasmini2
,id-def2
) (assq 'frameset--mini
(car state2
))))
1113 (cond ((eq id-def1 t
) t
)
1114 ((eq id-def2 t
) nil
)
1115 ((not (eq hasmini1 hasmini2
)) (eq hasmini1 t
))
1116 ((eq hasmini1 nil
) (or id-def1 id-def2
))
1119 (defun frameset-keep-original-display-p (force-display)
1120 "True if saved frames' displays should be honored.
1121 For the meaning of FORCE-DISPLAY, see `frameset-restore'."
1122 (cond ((eq system-type
'windows-nt
) nil
) ;; Does ns support more than one display?
1124 (t (not force-display
))))
1126 (defun frameset-minibufferless-first-p (frame1 _frame2
)
1127 "Predicate to sort minibuffer-less frames before other frames."
1128 ;; "Fix semantics of 'minibuffer' frame parameter" change: The
1129 ;; 'minibuffer' frame parameter of a minibuffer-less frame is that
1130 ;; frame's minibuffer window instead of nil.
1131 (windowp (frame-parameter frame1
'minibuffer
)))
1134 (cl-defun frameset-restore (frameset
1135 &key predicate filters reuse-frames
1136 force-display force-onscreen
1138 "Restore a FRAMESET into the current display(s).
1140 PREDICATE is a function called with two arguments, the parameter alist
1141 and the window-state of the frame being restored, in that order (see
1142 the docstring of the `frameset' defstruct for additional details).
1143 If PREDICATE returns nil, the frame described by that parameter alist
1144 and window-state is not restored.
1146 FILTERS is an alist of parameter filters; if nil, the value of
1147 `frameset-filter-alist' is used instead.
1149 REUSE-FRAMES selects the policy to reuse frames when restoring:
1150 t All existing frames can be reused.
1151 nil No existing frame can be reused.
1152 match Only frames with matching frame ids can be reused.
1153 PRED A predicate function; it receives as argument a live frame,
1154 and must return non-nil to allow reusing it, nil otherwise.
1156 FORCE-DISPLAY can be:
1157 t Frames are restored in the current display.
1158 nil Frames are restored, if possible, in their original displays.
1159 delete Frames in other displays are deleted instead of restored.
1160 PRED A function called with two arguments, the parameter alist and
1161 the window state (in that order). It must return t, nil or
1162 `delete', as above but affecting only the frame that will
1163 be created from that parameter alist.
1165 FORCE-ONSCREEN can be:
1166 t Force onscreen only those frames that are fully offscreen.
1167 nil Do not force any frame back onscreen.
1168 all Force onscreen any frame fully or partially offscreen.
1169 PRED A function called with three arguments,
1170 - the live frame just restored,
1171 - a list (LEFT TOP WIDTH HEIGHT), describing the frame,
1172 - a list (LEFT TOP WIDTH HEIGHT), describing the workarea.
1173 It must return non-nil to force the frame onscreen, nil otherwise.
1175 CLEANUP-FRAMES allows \"cleaning up\" the frame list after restoring a frameset:
1176 t Delete all frames that were not created or restored upon.
1177 nil Keep all frames.
1178 FUNC A function called with two arguments:
1179 - FRAME, a live frame.
1180 - ACTION, which can be one of
1181 :rejected Frame existed, but was not a candidate for reuse.
1182 :ignored Frame existed, was a candidate, but wasn't reused.
1183 :reused Frame existed, was a candidate, and restored upon.
1184 :created Frame didn't exist, was created and restored upon.
1185 Return value is ignored.
1187 Note the timing and scope of the operations described above: REUSE-FRAMES
1188 affects existing frames; PREDICATE, FILTERS and FORCE-DISPLAY affect the frame
1189 being restored before that happens; FORCE-ONSCREEN affects the frame once
1190 it has been restored; and CLEANUP-FRAMES affects all frames alive after the
1191 restoration, including those that have been reused or created anew.
1193 All keyword parameters default to nil."
1195 (cl-assert (frameset-valid-p frameset
))
1197 (let* ((frames (frame-list))
1198 (frameset--action-map (make-hash-table :test
#'eq
))
1199 ;; frameset--reuse-list is a list of frames potentially reusable. Later we
1200 ;; will decide which ones can be reused, and how to deal with any leftover.
1201 (frameset--reuse-list
1208 (cl-loop for
(state) in
(frameset-states frameset
)
1209 when
(frameset-frame-with-id (frameset-cfg-id state
) frames
)
1212 (cl-remove-if-not reuse-frames frames
))
1214 (error "Invalid arg :reuse-frames %s" reuse-frames
)))))
1216 ;; Mark existing frames in the map; candidates to reuse are marked as :ignored;
1217 ;; they will be reassigned later, if chosen.
1218 (dolist (frame frames
)
1220 (if (memq frame frameset--reuse-list
) :ignored
:rejected
)
1221 frameset--action-map
))
1223 ;; Sort saved states to guarantee that minibufferless frames will be created
1224 ;; after the frames that contain their minibuffer windows.
1225 (dolist (state (sort (copy-sequence (frameset-states frameset
))
1226 #'frameset--minibufferless-last-p
))
1227 (pcase-let ((`(,frame-cfg .
,window-cfg
) state
))
1228 (when (or (null predicate
) (funcall predicate frame-cfg window-cfg
))
1229 (condition-case-unless-debug err
1230 (let* ((d-mini (cdr (assq 'frameset--mini frame-cfg
)))
1231 (mb-id (cdr d-mini
))
1232 (default (and (car d-mini
) mb-id
))
1233 (force-display (if (functionp force-display
)
1234 (funcall force-display frame-cfg window-cfg
)
1236 (frameset--target-display nil
)
1237 frame to-tty duplicate
)
1238 ;; Only set target if forcing displays and the target display is different.
1239 (unless (or (frameset-keep-original-display-p force-display
)
1240 (equal (frame-parameter nil
'display
)
1241 (cdr (assq 'display frame-cfg
))))
1242 (setq frameset--target-display
(cons 'display
1243 (frame-parameter nil
'display
))
1244 to-tty
(null (cdr frameset--target-display
))))
1245 ;; Time to restore frames and set up their minibuffers as they were.
1246 ;; We only skip a frame (thus deleting it) if either:
1247 ;; - we're switching displays, and the user chose the option to delete, or
1248 ;; - we're switching to tty, and the frame to restore is minibuffer-only.
1249 (unless (and frameset--target-display
1250 (or (eq force-display
'delete
)
1252 (eq (cdr (assq 'minibuffer frame-cfg
)) 'only
))))
1253 ;; To avoid duplicating frame ids after restoration, we note any
1254 ;; existing frame whose id matches a frame configuration in the
1255 ;; frameset. Once the frame config is properly restored, we can
1256 ;; reset the old frame's id to nil.
1257 (setq duplicate
(frameset-frame-with-id (frameset-cfg-id frame-cfg
)
1259 ;; Restore minibuffers. Some of this stuff could be done in a filter
1260 ;; function, but it would be messy because restoring minibuffers affects
1261 ;; global state; it's best to do it here than add a bunch of global
1262 ;; variables to pass info back-and-forth to/from the filter function.
1264 ((null d-mini
)) ;; No frameset--mini. Process as normal frame.
1265 (to-tty) ;; Ignore minibuffer stuff and process as normal frame.
1266 ((car d-mini
) ;; Frame has minibuffer (or it is minibuffer-only).
1267 (when (eq (cdr (assq 'minibuffer frame-cfg
)) 'only
)
1268 (setq frame-cfg
(append '((tool-bar-lines .
0) (menu-bar-lines .
0))
1270 (t ;; Frame depends on other frame's minibuffer window.
1272 (let ((mb-frame (frameset-frame-with-id mb-id
))
1275 (delay-warning 'frameset
1276 (format "Minibuffer frame %S not found" mb-id
)
1278 (setq mb-window
(minibuffer-window mb-frame
))
1279 (unless (and (window-live-p mb-window
)
1280 (window-minibuffer-p mb-window
))
1281 (delay-warning 'frameset
1282 (format "Not a minibuffer window %s" mb-window
)
1284 (setq mb-window nil
)))
1286 (push (cons 'minibuffer mb-window
) frame-cfg
))))))
1287 ;; OK, we're ready at last to create (or reuse) a frame and
1288 ;; restore the window config.
1289 (setq frame
(frameset--restore-frame frame-cfg window-cfg
1290 (or filters frameset-filter-alist
)
1292 ;; Now reset any duplicate frameset--id
1293 (when (and duplicate
(not (eq frame duplicate
)))
1294 (set-frame-parameter duplicate
'frameset--id nil
))
1295 ;; Set default-minibuffer if required.
1296 (when default
(setq default-minibuffer-frame frame
))))
1298 (delay-warning 'frameset
(error-message-string err
) :error
))))))
1300 ;; Setting the parent frame after the frame has been created is a
1301 ;; pain because one can see the frame move on the screen. Ideally,
1302 ;; we would restore minibuffer equipped child frames after their
1303 ;; respective parents have been made but this might interfere with
1304 ;; the reordering of minibuffer frames. Left to the experts ...
1305 (dolist (frame (frame-list))
1306 (let* ((frame-id (frame-parameter frame
'frameset--parent-frame
))
1308 (and frame-id
(frameset-frame-with-id frame-id
))))
1309 (when (and (not (eq (frame-parameter frame
'parent-frame
) parent-frame
))
1310 (frame-live-p parent-frame
))
1311 (set-frame-parameter frame
'parent-frame parent-frame
)))
1312 (let* ((frame-id (frame-parameter frame
'frameset--delete-before
))
1314 (and frame-id
(frameset-frame-with-id frame-id
))))
1315 (when (frame-live-p delete-before
)
1316 (set-frame-parameter frame
'delete-before delete-before
)))
1317 (let* ((frame-id (frame-parameter frame
'frameset--mouse-wheel-frame
))
1319 (and frame-id
(frameset-frame-with-id frame-id
))))
1320 (when (frame-live-p mouse-wheel-frame
)
1321 (set-frame-parameter frame
'mouse-wheel-frame mouse-wheel-frame
))))
1323 ;; In case we try to delete the initial frame, we want to make sure that
1324 ;; other frames are already visible (discussed in thread for bug#14841).
1327 ;; Clean up the frame list
1328 (when cleanup-frames
1330 (cleanup (if (eq cleanup-frames t
)
1331 (lambda (frame action
)
1332 (when (memq action
'(:rejected
:ignored
))
1333 (delete-frame frame
)))
1335 (maphash (lambda (frame _action
) (push frame map
)) frameset--action-map
)
1336 (dolist (frame (sort map
1337 ;; Minibufferless frames must go first to avoid
1338 ;; errors when attempting to delete a frame whose
1339 ;; minibuffer window is used by another frame.
1340 #'frameset-minibufferless-first-p
))
1341 (condition-case-unless-debug err
1342 (funcall cleanup frame
(gethash frame frameset--action-map
))
1344 (delay-warning 'frameset
(error-message-string err
) :warning
))))))
1346 ;; Make sure there's at least one visible frame.
1347 (unless (or (daemonp)
1349 (maphash (lambda (frame _
)
1350 (and (frame-live-p frame
) (frame-visible-p frame
)
1351 (throw 'visible t
)))
1352 frameset--action-map
)))
1353 (make-frame-visible (selected-frame)))))
1359 (defun frameset--jump-to-register (data)
1360 "Restore frameset from DATA stored in register.
1361 Called from `jump-to-register'. Internal use only."
1364 :filters frameset-session-filter-alist
1365 :reuse-frames
(if current-prefix-arg t
'match
)
1366 :cleanup-frames
(if current-prefix-arg
1370 (lambda (frame action
)
1372 (`rejected
(iconify-frame frame
))
1373 ;; In the unexpected case that a frame was a candidate
1374 ;; (matching frame id) and yet not restored, remove it
1375 ;; because it is in fact a duplicate.
1376 (`ignored
(delete-frame frame
))))))
1378 ;; Restore selected frame, buffer and point.
1379 (let ((frame (frameset-frame-with-id (aref data
1)))
1382 (select-frame-set-input-focus frame
)
1383 (when (and (buffer-live-p (setq buffer
(marker-buffer (aref data
2))))
1384 (window-live-p (setq window
(get-buffer-window buffer frame
))))
1385 (set-frame-selected-window frame window
)
1386 (with-current-buffer buffer
(goto-char (aref data
2)))))))
1389 (defun frameset--print-register (data)
1390 "Print basic info about frameset stored in DATA.
1391 Called from `list-registers' and `view-register'. Internal use only."
1392 (let* ((fs (aref data
0))
1393 (ns (length (frameset-states fs
))))
1394 (princ (format "a frameset (%d frame%s, saved on %s)."
1396 (if (= 1 ns
) "" "s")
1397 (format-time-string "%c" (frameset-timestamp fs
))))))
1400 (defun frameset-to-register (register)
1401 "Store the current frameset in register REGISTER.
1402 Use \\[jump-to-register] to restore the frameset.
1403 Argument is a character, naming the register.
1405 Interactively, reads the register using `register-read-with-preview'."
1406 (interactive (list (register-read-with-preview "Frameset to register: ")))
1407 (set-register register
1409 (vector (frameset-save nil
1411 :filters frameset-session-filter-alist
)
1412 ;; frameset-save does not include the value of point
1413 ;; in the current buffer, so record that separately.
1414 (frameset-frame-id nil
)
1416 :print-func
#'frameset--print-register
1417 :jump-func
#'frameset--jump-to-register
)))
1421 ;;; frameset.el ends here