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