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 | 434 | "Minimum set of parameters to filter for live (on-session) framesets. |
f9dbf1cb | 435 | DO NOT MODIFY. See `frameset-filter-alist' for a full description.") |
d5671a82 JB |
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. | |
f9dbf1cb | 461 | DO NOT MODIFY. See `frameset-filter-alist' for a full description.") |
d5671a82 JB |
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 | ||
f9dbf1cb JB |
470 | Initially, `frameset-filter-alist' is set to, and shares the value of, |
471 | `frameset-persistent-filter-alist'. You can override any item in | |
472 | this alist by `push'ing a new item onto it. If, for some reason, you | |
473 | intend to modify existing values, do | |
474 | ||
475 | (setq frameset-filter-alist (copy-tree frameset-filter-alist)) | |
476 | ||
477 | before changing anything. | |
478 | ||
a912c016 JB |
479 | On saving, PARAMETERS is the parameter alist of each frame processed, |
480 | and FILTERED is the parameter alist that gets saved to the frameset. | |
481 | ||
024b38fc JB |
482 | On restoring, PARAMETERS is the parameter alist extracted from the |
483 | frameset, and FILTERED is the resulting frame parameter alist used | |
063233c3 JB |
484 | to restore the frame. |
485 | ||
024b38fc JB |
486 | Elements of `frameset-filter-alist' are conses (PARAM . ACTION), |
487 | where PARAM is a parameter name (a symbol identifying a frame | |
488 | parameter), and ACTION can be: | |
063233c3 JB |
489 | |
490 | nil The parameter is copied to FILTERED. | |
491 | :never The parameter is never copied to FILTERED. | |
76c5e5ab | 492 | :save The parameter is copied only when saving the frame. |
063233c3 | 493 | :restore The parameter is copied only when restoring the frame. |
76c5e5ab | 494 | FILTER A filter function. |
9421876d JB |
495 | |
496 | FILTER can be a symbol FILTER-FUN, or a list (FILTER-FUN ARGS...). | |
024b38fc JB |
497 | FILTER-FUN is invoked with |
498 | ||
499 | (apply FILTER-FUN CURRENT FILTERED PARAMETERS SAVING ARGS) | |
500 | ||
501 | where | |
9421876d JB |
502 | |
503 | CURRENT A cons (PARAM . VALUE), where PARAM is the one being | |
d5671a82 | 504 | filtered and VALUE is its current value. |
063233c3 | 505 | FILTERED The resulting alist (so far). |
9421876d | 506 | PARAMETERS The complete alist of parameters being filtered, |
76c5e5ab | 507 | SAVING Non-nil if filtering before saving state, nil if filtering |
a912c016 | 508 | before restoring it. |
024b38fc | 509 | ARGS Any additional arguments specified in the ACTION. |
9421876d | 510 | |
307764cc JB |
511 | FILTER-FUN is allowed to modify items in FILTERED, but no other arguments. |
512 | It must return: | |
76c5e5ab JB |
513 | nil Skip CURRENT (do not add it to FILTERED). |
514 | t Add CURRENT to FILTERED as is. | |
063233c3 JB |
515 | (NEW-PARAM . NEW-VALUE) Add this to FILTERED instead of CURRENT. |
516 | ||
517 | Frame parameters not on this alist are passed intact, as if they were | |
518 | defined with ACTION = nil.") | |
9421876d | 519 | |
9421876d JB |
520 | |
521 | (defvar frameset--target-display nil | |
522 | ;; Either (minibuffer . VALUE) or nil. | |
523 | ;; This refers to the current frame config being processed inside | |
063233c3 | 524 | ;; `frameset-restore' and its auxiliary functions (like filtering). |
9421876d JB |
525 | ;; If nil, there is no need to change the display. |
526 | ;; If non-nil, display parameter to use when creating the frame. | |
527 | "Internal use only.") | |
528 | ||
529 | (defun frameset-switch-to-gui-p (parameters) | |
530 | "True when switching to a graphic display. | |
a912c016 JB |
531 | Return non-nil if the parameter alist PARAMETERS describes a frame on a |
532 | text-only terminal, and the frame is being restored on a graphic display; | |
533 | otherwise return nil. Only meaningful when called from a filtering | |
534 | function in `frameset-filter-alist'." | |
76c5e5ab JB |
535 | (and frameset--target-display ; we're switching |
536 | (null (cdr (assq 'display parameters))) ; from a tty | |
537 | (cdr frameset--target-display))) ; to a GUI display | |
9421876d JB |
538 | |
539 | (defun frameset-switch-to-tty-p (parameters) | |
540 | "True when switching to a text-only terminal. | |
a912c016 JB |
541 | Return non-nil if the parameter alist PARAMETERS describes a frame on a |
542 | graphic display, and the frame is being restored on a text-only terminal; | |
543 | otherwise return nil. Only meaningful when called from a filtering | |
544 | function in `frameset-filter-alist'." | |
76c5e5ab JB |
545 | (and frameset--target-display ; we're switching |
546 | (cdr (assq 'display parameters)) ; from a GUI display | |
547 | (null (cdr frameset--target-display)))) ; to a tty | |
9421876d | 548 | |
d5671a82 | 549 | (defun frameset-filter-tty-to-GUI (_current _filtered parameters saving) |
063233c3 JB |
550 | "Remove CURRENT when switching from tty to a graphic display. |
551 | ||
552 | For the meaning of CURRENT, FILTERED, PARAMETERS and SAVING, | |
a912c016 | 553 | see `frameset-filter-alist'." |
d5671a82 JB |
554 | (or saving |
555 | (not (frameset-switch-to-gui-p parameters)))) | |
556 | ||
9421876d JB |
557 | (defun frameset-filter-sanitize-color (current _filtered parameters saving) |
558 | "When switching to a GUI frame, remove \"unspecified\" colors. | |
063233c3 JB |
559 | Useful as a filter function for tty-specific parameters. |
560 | ||
561 | For the meaning of CURRENT, FILTERED, PARAMETERS and SAVING, | |
a912c016 | 562 | see `frameset-filter-alist'." |
9421876d JB |
563 | (or saving |
564 | (not (frameset-switch-to-gui-p parameters)) | |
565 | (not (stringp (cdr current))) | |
566 | (not (string-match-p "^unspecified-[fb]g$" (cdr current))))) | |
567 | ||
568 | (defun frameset-filter-minibuffer (current _filtered _parameters saving) | |
a912c016 | 569 | "When saving, convert (minibuffer . #<window>) to (minibuffer . t). |
063233c3 JB |
570 | |
571 | For the meaning of CURRENT, FILTERED, PARAMETERS and SAVING, | |
a912c016 | 572 | see `frameset-filter-alist'." |
9421876d JB |
573 | (or (not saving) |
574 | (if (windowp (cdr current)) | |
575 | '(minibuffer . t) | |
576 | t))) | |
577 | ||
a912c016 JB |
578 | (defun frameset-filter-shelve-param (current _filtered parameters saving |
579 | &optional prefix) | |
9421876d | 580 | "When switching to a tty frame, save parameter P as PREFIX:P. |
a912c016 | 581 | The parameter can be later restored with `frameset-filter-unshelve-param'. |
063233c3 JB |
582 | PREFIX defaults to `GUI'. |
583 | ||
584 | For the meaning of CURRENT, FILTERED, PARAMETERS and SAVING, | |
a912c016 | 585 | see `frameset-filter-alist'." |
9421876d JB |
586 | (unless prefix (setq prefix 'GUI)) |
587 | (cond (saving t) | |
588 | ((frameset-switch-to-tty-p parameters) | |
589 | (let ((prefix:p (intern (format "%s:%s" prefix (car current))))) | |
590 | (if (assq prefix:p parameters) | |
591 | nil | |
592 | (cons prefix:p (cdr current))))) | |
593 | ((frameset-switch-to-gui-p parameters) | |
594 | (not (assq (intern (format "%s:%s" prefix (car current))) parameters))) | |
595 | (t t))) | |
596 | ||
a912c016 | 597 | (defun frameset-filter-unshelve-param (current filtered parameters saving) |
9421876d | 598 | "When switching to a GUI frame, restore PREFIX:P parameter as P. |
063233c3 JB |
599 | CURRENT must be of the form (PREFIX:P . value). |
600 | ||
601 | For the meaning of CURRENT, FILTERED, PARAMETERS and SAVING, | |
a912c016 | 602 | see `frameset-filter-alist'." |
9421876d JB |
603 | (or saving |
604 | (not (frameset-switch-to-gui-p parameters)) | |
605 | (let* ((prefix:p (symbol-name (car current))) | |
606 | (p (intern (substring prefix:p | |
607 | (1+ (string-match-p ":" prefix:p))))) | |
608 | (val (cdr current)) | |
609 | (found (assq p filtered))) | |
610 | (if (not found) | |
611 | (cons p val) | |
612 | (setcdr found val) | |
613 | nil)))) | |
614 | ||
615 | (defun frameset-filter-iconified (_current _filtered parameters saving) | |
616 | "Remove CURRENT when saving an iconified frame. | |
063233c3 | 617 | This is used for positional parameters `left' and `top', which are |
9421876d | 618 | meaningless in an iconified frame, so the frame is restored in a |
063233c3 JB |
619 | default position. |
620 | ||
621 | For the meaning of CURRENT, FILTERED, PARAMETERS and SAVING, | |
a912c016 | 622 | see `frameset-filter-alist'." |
9421876d JB |
623 | (not (and saving (eq (cdr (assq 'visibility parameters)) 'icon)))) |
624 | ||
9421876d | 625 | (defun frameset-filter-params (parameters filter-alist saving) |
024b38fc | 626 | "Filter parameter alist PARAMETERS and return a filtered alist. |
9421876d JB |
627 | FILTER-ALIST is an alist of parameter filters, in the format of |
628 | `frameset-filter-alist' (which see). | |
629 | SAVING is non-nil while filtering parameters to save a frameset, | |
630 | nil while the filtering is done to restore it." | |
631 | (let ((filtered nil)) | |
632 | (dolist (current parameters) | |
024b38fc JB |
633 | ;; When saving, the parameter alist is temporary, so modifying it |
634 | ;; is not a problem. When restoring, the parameter alist is part | |
307764cc JB |
635 | ;; of a frameset, so we must copy parameters to avoid inadvertent |
636 | ;; modifications. | |
9421876d JB |
637 | (pcase (cdr (assq (car current) filter-alist)) |
638 | (`nil | |
307764cc | 639 | (push (if saving current (copy-tree current)) filtered)) |
063233c3 | 640 | (:never |
9421876d | 641 | nil) |
9421876d | 642 | (:restore |
307764cc | 643 | (unless saving (push (copy-tree current) filtered))) |
063233c3 | 644 | (:save |
9421876d JB |
645 | (when saving (push current filtered))) |
646 | ((or `(,fun . ,args) (and fun (pred fboundp))) | |
307764cc JB |
647 | (let* ((this (apply fun current filtered parameters saving args)) |
648 | (val (if (eq this t) current this))) | |
649 | (when val | |
650 | (push (if saving val (copy-tree val)) filtered)))) | |
9421876d JB |
651 | (other |
652 | (delay-warning 'frameset (format "Unknown filter %S" other) :error)))) | |
653 | ;; Set the display parameter after filtering, so that filter functions | |
654 | ;; have access to its original value. | |
655 | (when frameset--target-display | |
656 | (let ((display (assq 'display filtered))) | |
657 | (if display | |
658 | (setcdr display (cdr frameset--target-display)) | |
659 | (push frameset--target-display filtered)))) | |
660 | filtered)) | |
661 | ||
662 | \f | |
38276e01 | 663 | ;; Frame ids |
9421876d JB |
664 | |
665 | (defun frameset--set-id (frame) | |
38276e01 | 666 | "Set FRAME's id if not yet set. |
9421876d | 667 | Internal use only." |
d5671a82 | 668 | (unless (frame-parameter frame 'frameset--id) |
9421876d | 669 | (set-frame-parameter frame |
d5671a82 | 670 | 'frameset--id |
9421876d JB |
671 | (mapconcat (lambda (n) (format "%04X" n)) |
672 | (cl-loop repeat 4 collect (random 65536)) | |
673 | "-")))) | |
38276e01 JB |
674 | ;;;###autoload |
675 | (defun frameset-frame-id (frame) | |
676 | "Return the frame id of FRAME, if it has one; else, return nil. | |
677 | A frame id is a string that uniquely identifies a frame. | |
678 | It is persistent across `frameset-save' / `frameset-restore' | |
679 | invocations, and once assigned is never changed unless the same | |
680 | frame is duplicated (via `frameset-restore'), in which case the | |
681 | newest frame keeps the id and the old frame's is set to nil." | |
682 | (frame-parameter frame 'frameset--id)) | |
683 | ||
684 | ;;;###autoload | |
685 | (defun frameset-frame-id-equal-p (frame id) | |
686 | "Return non-nil if FRAME's id matches ID." | |
687 | (string= (frameset-frame-id frame) id)) | |
688 | ||
689 | ;;;###autoload | |
a912c016 | 690 | (defun frameset-frame-with-id (id &optional frame-list) |
38276e01 JB |
691 | "Return the live frame with id ID, if exists; else nil. |
692 | If FRAME-LIST is a list of frames, check these frames only. | |
693 | If nil, check all live frames." | |
694 | (cl-find-if (lambda (f) | |
695 | (and (frame-live-p f) | |
696 | (frameset-frame-id-equal-p f id))) | |
697 | (or frame-list (frame-list)))) | |
698 | ||
699 | \f | |
700 | ;; Saving framesets | |
9421876d | 701 | |
a912c016 | 702 | (defun frameset--record-minibuffer-relationships (frame-list) |
9421876d | 703 | "Process FRAME-LIST and record minibuffer relationships. |
d5671a82 | 704 | FRAME-LIST is a list of frames. Internal use only." |
9421876d JB |
705 | ;; Record frames with their own minibuffer |
706 | (dolist (frame (minibuffer-frame-list)) | |
707 | (when (memq frame frame-list) | |
708 | (frameset--set-id frame) | |
709 | ;; For minibuffer-owning frames, frameset--mini is a cons | |
710 | ;; (t . DEFAULT?), where DEFAULT? is a boolean indicating whether | |
711 | ;; the frame is the one pointed out by `default-minibuffer-frame'. | |
712 | (set-frame-parameter frame | |
713 | 'frameset--mini | |
714 | (cons t (eq frame default-minibuffer-frame))))) | |
715 | ;; Now link minibufferless frames with their minibuffer frames | |
716 | (dolist (frame frame-list) | |
717 | (unless (frame-parameter frame 'frameset--mini) | |
718 | (frameset--set-id frame) | |
719 | (let* ((mb-frame (window-frame (minibuffer-window frame))) | |
38276e01 | 720 | (id (and mb-frame (frameset-frame-id mb-frame)))) |
9421876d | 721 | (if (null id) |
063233c3 | 722 | (error "Minibuffer frame %S for %S is not being saved" mb-frame frame) |
9421876d | 723 | ;; For minibufferless frames, frameset--mini is a cons |
d5671a82 JB |
724 | ;; (nil . FRAME-ID), where FRAME-ID is the frameset--id |
725 | ;; of the frame containing its minibuffer window. | |
9421876d JB |
726 | (set-frame-parameter frame |
727 | 'frameset--mini | |
728 | (cons nil id))))))) | |
729 | ||
51d30f2c | 730 | ;;;###autoload |
a912c016 JB |
731 | (cl-defun frameset-save (frame-list |
732 | &key app name description | |
733 | filters predicate properties) | |
734 | "Return a frameset for FRAME-LIST, a list of frames. | |
307764cc JB |
735 | Dead frames and non-frame objects are silently removed from the list. |
736 | If nil, FRAME-LIST defaults to the output of `frame-list' (all live frames). | |
a912c016 JB |
737 | APP, NAME and DESCRIPTION are optional data; see the docstring of the |
738 | `frameset' defstruct for details. | |
739 | FILTERS is an alist of parameter filters; if nil, the value of the variable | |
740 | `frameset-filter-alist' is used instead. | |
9421876d | 741 | PREDICATE is a predicate function, which must return non-nil for frames that |
024b38fc | 742 | should be saved; if PREDICATE is nil, all frames from FRAME-LIST are saved. |
9421876d | 743 | PROPERTIES is a user-defined property list to add to the frameset." |
063233c3 JB |
744 | (let* ((list (or (copy-sequence frame-list) (frame-list))) |
745 | (frames (cl-delete-if-not #'frame-live-p | |
746 | (if predicate | |
747 | (cl-delete-if-not predicate list) | |
748 | list)))) | |
a912c016 JB |
749 | (frameset--record-minibuffer-relationships frames) |
750 | (make-frameset :app app | |
751 | :name name | |
752 | :description description | |
753 | :properties properties | |
754 | :states (mapcar | |
755 | (lambda (frame) | |
756 | (cons | |
757 | (frameset-filter-params (frame-parameters frame) | |
758 | (or filters | |
759 | frameset-filter-alist) | |
760 | t) | |
761 | (window-state-get (frame-root-window frame) t))) | |
762 | frames)))) | |
9421876d JB |
763 | |
764 | \f | |
765 | ;; Restoring framesets | |
766 | ||
767 | (defvar frameset--reuse-list nil | |
063233c3 JB |
768 | "The list of frames potentially reusable. |
769 | Its value is only meaningful during execution of `frameset-restore'. | |
770 | Internal use only.") | |
9421876d | 771 | |
024b38fc JB |
772 | (defun frameset-compute-pos (value left/top right/bottom) |
773 | "Return an absolute positioning value for a frame. | |
774 | VALUE is the value of a positional frame parameter (`left' or `top'). | |
775 | If VALUE is relative to the screen edges (like (+ -35) or (-200), it is | |
776 | converted to absolute by adding it to the corresponding edge; if it is | |
777 | an absolute position, it is returned unmodified. | |
778 | LEFT/TOP and RIGHT/BOTTOM indicate the dimensions of the screen in | |
779 | pixels along the relevant direction: either the position of the left | |
780 | and right edges for a `left' positional parameter, or the position of | |
781 | the top and bottom edges for a `top' parameter." | |
9421876d JB |
782 | (pcase value |
783 | (`(+ ,val) (+ left/top val)) | |
784 | (`(- ,val) (+ right/bottom val)) | |
785 | (val val))) | |
786 | ||
307764cc | 787 | (defun frameset-move-onscreen (frame force-onscreen) |
9421876d JB |
788 | "If FRAME is offscreen, move it back onscreen and, if necessary, resize it. |
789 | For the description of FORCE-ONSCREEN, see `frameset-restore'. | |
790 | When forced onscreen, frames wider than the monitor's workarea are converted | |
791 | to fullwidth, and frames taller than the workarea are converted to fullheight. | |
307764cc | 792 | NOTE: This only works for non-iconified frames." |
9421876d | 793 | (pcase-let* ((`(,left ,top ,width ,height) (cl-cdadr (frame-monitor-attributes frame))) |
d5671a82 JB |
794 | (right (+ left width -1)) |
795 | (bottom (+ top height -1)) | |
024b38fc JB |
796 | (fr-left (frameset-compute-pos (frame-parameter frame 'left) left right)) |
797 | (fr-top (frameset-compute-pos (frame-parameter frame 'top) top bottom)) | |
9421876d JB |
798 | (ch-width (frame-char-width frame)) |
799 | (ch-height (frame-char-height frame)) | |
d5671a82 JB |
800 | (fr-width (max (frame-pixel-width frame) (* ch-width (frame-width frame)))) |
801 | (fr-height (max (frame-pixel-height frame) (* ch-height (frame-height frame)))) | |
802 | (fr-right (+ fr-left fr-width -1)) | |
803 | (fr-bottom (+ fr-top fr-height -1))) | |
9421876d | 804 | (when (pcase force-onscreen |
d5671a82 JB |
805 | ;; A predicate. |
806 | ((pred functionp) | |
807 | (funcall force-onscreen | |
808 | frame | |
809 | (list fr-left fr-top fr-width fr-height) | |
810 | (list left top width height))) | |
9421876d | 811 | ;; Any corner is outside the screen. |
76c5e5ab | 812 | (:all (or (< fr-bottom top) (> fr-bottom bottom) |
9421876d JB |
813 | (< fr-left left) (> fr-left right) |
814 | (< fr-right left) (> fr-right right) | |
76c5e5ab | 815 | (< fr-top top) (> fr-top bottom))) |
9421876d | 816 | ;; Displaced to the left, right, above or below the screen. |
76c5e5ab | 817 | (`t (or (> fr-left right) |
9421876d | 818 | (< fr-right left) |
76c5e5ab | 819 | (> fr-top bottom) |
9421876d JB |
820 | (< fr-bottom top))) |
821 | ;; Fully inside, no need to do anything. | |
822 | (_ nil)) | |
823 | (let ((fullwidth (> fr-width width)) | |
824 | (fullheight (> fr-height height)) | |
825 | (params nil)) | |
826 | ;; Position frame horizontally. | |
827 | (cond (fullwidth | |
828 | (push `(left . ,left) params)) | |
829 | ((> fr-right right) | |
830 | (push `(left . ,(+ left (- width fr-width))) params)) | |
831 | ((< fr-left left) | |
832 | (push `(left . ,left) params))) | |
833 | ;; Position frame vertically. | |
834 | (cond (fullheight | |
835 | (push `(top . ,top) params)) | |
836 | ((> fr-bottom bottom) | |
837 | (push `(top . ,(+ top (- height fr-height))) params)) | |
838 | ((< fr-top top) | |
839 | (push `(top . ,top) params))) | |
840 | ;; Compute fullscreen state, if required. | |
841 | (when (or fullwidth fullheight) | |
842 | (push (cons 'fullscreen | |
843 | (cond ((not fullwidth) 'fullheight) | |
844 | ((not fullheight) 'fullwidth) | |
845 | (t 'maximized))) | |
846 | params)) | |
847 | ;; Finally, move the frame back onscreen. | |
848 | (when params | |
849 | (modify-frame-parameters frame params)))))) | |
850 | ||
a912c016 | 851 | (defun frameset--find-frame-if (predicate display &rest args) |
9421876d JB |
852 | "Find a frame in `frameset--reuse-list' satisfying PREDICATE. |
853 | Look through available frames whose display property matches DISPLAY | |
854 | and return the first one for which (PREDICATE frame ARGS) returns t. | |
855 | If PREDICATE is nil, it is always satisfied. Internal use only." | |
856 | (cl-find-if (lambda (frame) | |
857 | (and (equal (frame-parameter frame 'display) display) | |
858 | (or (null predicate) | |
859 | (apply predicate frame args)))) | |
860 | frameset--reuse-list)) | |
861 | ||
a912c016 JB |
862 | (defun frameset--reuse-frame (display parameters) |
863 | "Return an existing frame to reuse, or nil if none found. | |
864 | DISPLAY is the display where the frame will be shown, and PARAMETERS | |
024b38fc | 865 | is the parameter alist of the frame being restored. Internal use only." |
9421876d JB |
866 | (let ((frame nil) |
867 | mini) | |
868 | ;; There are no fancy heuristics there. We could implement some | |
869 | ;; based on frame size and/or position, etc., but it is not clear | |
870 | ;; that any "gain" (in the sense of reduced flickering, etc.) is | |
871 | ;; worth the added complexity. In fact, the code below mainly | |
872 | ;; tries to work nicely when M-x desktop-read is used after a | |
873 | ;; desktop session has already been loaded. The other main use | |
874 | ;; case, which is the initial desktop-read upon starting Emacs, | |
875 | ;; will usually have only one frame, and should already work. | |
876 | (cond ((null display) | |
877 | ;; When the target is tty, every existing frame is reusable. | |
a912c016 JB |
878 | (setq frame (frameset--find-frame-if nil display))) |
879 | ((car (setq mini (cdr (assq 'frameset--mini parameters)))) | |
9421876d JB |
880 | ;; If the frame has its own minibuffer, let's see whether |
881 | ;; that frame has already been loaded (which can happen after | |
882 | ;; M-x desktop-read). | |
a912c016 | 883 | (setq frame (frameset--find-frame-if |
9421876d | 884 | (lambda (f id) |
38276e01 | 885 | (frameset-frame-id-equal-p f id)) |
a912c016 | 886 | display (cdr (assq 'frameset--id parameters)))) |
9421876d JB |
887 | ;; If it has not been loaded, and it is not a minibuffer-only frame, |
888 | ;; let's look for an existing non-minibuffer-only frame to reuse. | |
a912c016 JB |
889 | (unless (or frame (eq (cdr (assq 'minibuffer parameters)) 'only)) |
890 | (setq frame (frameset--find-frame-if | |
9421876d JB |
891 | (lambda (f) |
892 | (let ((w (frame-parameter f 'minibuffer))) | |
893 | (and (window-live-p w) | |
894 | (window-minibuffer-p w) | |
895 | (eq (window-frame w) f)))) | |
896 | display)))) | |
897 | (mini | |
898 | ;; For minibufferless frames, check whether they already exist, | |
899 | ;; and that they are linked to the right minibuffer frame. | |
a912c016 | 900 | (setq frame (frameset--find-frame-if |
a04d36a0 | 901 | (lambda (f id mini-id) |
38276e01 JB |
902 | (and (frameset-frame-id-equal-p f id) |
903 | (frameset-frame-id-equal-p (window-frame | |
904 | (minibuffer-window f)) | |
905 | mini-id))) | |
a912c016 | 906 | display (cdr (assq 'frameset--id parameters)) (cdr mini)))) |
9421876d JB |
907 | (t |
908 | ;; Default to just finding a frame in the same display. | |
a912c016 | 909 | (setq frame (frameset--find-frame-if nil display)))) |
9421876d JB |
910 | ;; If found, remove from the list. |
911 | (when frame | |
912 | (setq frameset--reuse-list (delq frame frameset--reuse-list))) | |
913 | frame)) | |
914 | ||
a912c016 JB |
915 | (defun frameset--initial-params (parameters) |
916 | "Return a list of PARAMETERS that must be set when creating the frame. | |
d5671a82 | 917 | Setting position and size parameters as soon as possible helps reducing |
a912c016 JB |
918 | flickering; other parameters, like `minibuffer' and `border-width', can |
919 | not be changed once the frame has been created. Internal use only." | |
d5671a82 | 920 | (cl-loop for param in '(left top with height border-width minibuffer) |
a912c016 | 921 | collect (assq param parameters))) |
d5671a82 | 922 | |
a912c016 | 923 | (defun frameset--restore-frame (parameters window-state filters force-onscreen) |
9421876d JB |
924 | "Set up and return a frame according to its saved state. |
925 | That means either reusing an existing frame or creating one anew. | |
a912c016 | 926 | PARAMETERS is the frame's parameter alist; WINDOW-STATE is its window state. |
063233c3 | 927 | For the meaning of FILTERS and FORCE-ONSCREEN, see `frameset-restore'. |
d5671a82 | 928 | Internal use only." |
a912c016 JB |
929 | (let* ((fullscreen (cdr (assq 'fullscreen parameters))) |
930 | (lines (assq 'tool-bar-lines parameters)) | |
931 | (filtered-cfg (frameset-filter-params parameters filters nil)) | |
9421876d JB |
932 | (display (cdr (assq 'display filtered-cfg))) ;; post-filtering |
933 | alt-cfg frame) | |
934 | ||
935 | ;; This works around bug#14795 (or feature#14795, if not a bug :-) | |
936 | (setq filtered-cfg (assq-delete-all 'tool-bar-lines filtered-cfg)) | |
937 | (push '(tool-bar-lines . 0) filtered-cfg) | |
938 | ||
939 | (when fullscreen | |
940 | ;; Currently Emacs has the limitation that it does not record the size | |
941 | ;; and position of a frame before maximizing it, so we cannot save & | |
942 | ;; restore that info. Instead, when restoring, we resort to creating | |
943 | ;; invisible "fullscreen" frames of default size and then maximizing them | |
944 | ;; (and making them visible) which at least is somewhat user-friendly | |
945 | ;; when these frames are later de-maximized. | |
946 | (let ((width (and (eq fullscreen 'fullheight) (cdr (assq 'width filtered-cfg)))) | |
947 | (height (and (eq fullscreen 'fullwidth) (cdr (assq 'height filtered-cfg)))) | |
948 | (visible (assq 'visibility filtered-cfg))) | |
949 | (setq filtered-cfg (cl-delete-if (lambda (p) | |
950 | (memq p '(visibility fullscreen width height))) | |
951 | filtered-cfg :key #'car)) | |
952 | (when width | |
953 | (setq filtered-cfg (append `((user-size . t) (width . ,width)) | |
954 | filtered-cfg))) | |
955 | (when height | |
956 | (setq filtered-cfg (append `((user-size . t) (height . ,height)) | |
957 | filtered-cfg))) | |
958 | ;; These are parameters to apply after creating/setting the frame. | |
959 | (push visible alt-cfg) | |
960 | (push (cons 'fullscreen fullscreen) alt-cfg))) | |
961 | ||
962 | ;; Time to find or create a frame an apply the big bunch of parameters. | |
963 | ;; If a frame needs to be created and it falls partially or fully offscreen, | |
964 | ;; sometimes it gets "pushed back" onscreen; however, moving it afterwards is | |
965 | ;; allowed. So we create the frame as invisible and then reapply the full | |
024b38fc | 966 | ;; parameter alist (including position and size parameters). |
9421876d JB |
967 | (setq frame (or (and frameset--reuse-list |
968 | (frameset--reuse-frame display filtered-cfg)) | |
969 | (make-frame-on-display display | |
970 | (cons '(visibility) | |
d5671a82 | 971 | (frameset--initial-params filtered-cfg))))) |
9421876d JB |
972 | (modify-frame-parameters frame |
973 | (if (eq (frame-parameter frame 'fullscreen) fullscreen) | |
974 | ;; Workaround for bug#14949 | |
975 | (assq-delete-all 'fullscreen filtered-cfg) | |
976 | filtered-cfg)) | |
977 | ||
978 | ;; If requested, force frames to be onscreen. | |
979 | (when (and force-onscreen | |
980 | ;; FIXME: iconified frames should be checked too, | |
981 | ;; but it is impossible without deiconifying them. | |
982 | (not (eq (frame-parameter frame 'visibility) 'icon))) | |
307764cc | 983 | (frameset-move-onscreen frame force-onscreen)) |
9421876d JB |
984 | |
985 | ;; Let's give the finishing touches (visibility, tool-bar, maximization). | |
986 | (when lines (push lines alt-cfg)) | |
987 | (when alt-cfg (modify-frame-parameters frame alt-cfg)) | |
988 | ;; Now restore window state. | |
a912c016 | 989 | (window-state-put window-state (frame-root-window frame) 'safe) |
9421876d JB |
990 | frame)) |
991 | ||
063233c3 | 992 | (defun frameset--minibufferless-last-p (state1 state2) |
307764cc | 993 | "Predicate to sort frame states in an order suitable for creating frames. |
024b38fc JB |
994 | It sorts minibuffer-owning frames before minibufferless ones. |
995 | Internal use only." | |
9421876d JB |
996 | (pcase-let ((`(,hasmini1 ,id-def1) (assq 'frameset--mini (car state1))) |
997 | (`(,hasmini2 ,id-def2) (assq 'frameset--mini (car state2)))) | |
998 | (cond ((eq id-def1 t) t) | |
999 | ((eq id-def2 t) nil) | |
1000 | ((not (eq hasmini1 hasmini2)) (eq hasmini1 t)) | |
1001 | ((eq hasmini1 nil) (string< id-def1 id-def2)) | |
1002 | (t t)))) | |
1003 | ||
d5671a82 | 1004 | (defun frameset-keep-original-display-p (force-display) |
a912c016 JB |
1005 | "True if saved frames' displays should be honored. |
1006 | For the meaning of FORCE-DISPLAY, see `frameset-restore'." | |
d5671a82 | 1007 | (cond ((daemonp) t) |
307764cc | 1008 | ((eq system-type 'windows-nt) nil) ;; Does ns support more than one display? |
d5671a82 JB |
1009 | (t (not force-display)))) |
1010 | ||
063233c3 JB |
1011 | (defun frameset-minibufferless-first-p (frame1 _frame2) |
1012 | "Predicate to sort minibufferless frames before other frames." | |
9421876d JB |
1013 | (not (frame-parameter frame1 'minibuffer))) |
1014 | ||
51d30f2c | 1015 | ;;;###autoload |
d5671a82 | 1016 | (cl-defun frameset-restore (frameset |
a912c016 | 1017 | &key predicate filters reuse-frames |
3677ffeb | 1018 | force-display force-onscreen) |
9421876d JB |
1019 | "Restore a FRAMESET into the current display(s). |
1020 | ||
a912c016 JB |
1021 | PREDICATE is a function called with two arguments, the parameter alist |
1022 | and the window-state of the frame being restored, in that order (see | |
1023 | the docstring of the `frameset' defstruct for additional details). | |
1024 | If PREDICATE returns nil, the frame described by that parameter alist | |
1025 | and window-state is not restored. | |
1026 | ||
1027 | FILTERS is an alist of parameter filters; if nil, the value of | |
1028 | `frameset-filter-alist' is used instead. | |
9421876d | 1029 | |
063233c3 | 1030 | REUSE-FRAMES selects the policy to use to reuse frames when restoring: |
76c5e5ab JB |
1031 | t Reuse existing frames if possible, and delete those not reused. |
1032 | nil Restore frameset in new frames and delete existing frames. | |
1033 | :keep Restore frameset in new frames and keep the existing ones. | |
1034 | LIST A list of frames to reuse; only these are reused (if possible). | |
a912c016 JB |
1035 | Remaining frames in this list are deleted; other frames not |
1036 | included on the list are left untouched. | |
9421876d JB |
1037 | |
1038 | FORCE-DISPLAY can be: | |
76c5e5ab JB |
1039 | t Frames are restored in the current display. |
1040 | nil Frames are restored, if possible, in their original displays. | |
063233c3 | 1041 | :delete Frames in other displays are deleted instead of restored. |
76c5e5ab | 1042 | PRED A function called with two arguments, the parameter alist and |
a912c016 JB |
1043 | the window state (in that order). It must return t, nil or |
1044 | `:delete', as above but affecting only the frame that will | |
1045 | be created from that parameter alist. | |
9421876d JB |
1046 | |
1047 | FORCE-ONSCREEN can be: | |
76c5e5ab JB |
1048 | t Force onscreen only those frames that are fully offscreen. |
1049 | nil Do not force any frame back onscreen. | |
1050 | :all Force onscreen any frame fully or partially offscreen. | |
1051 | PRED A function called with three arguments, | |
d5671a82 JB |
1052 | - the live frame just restored, |
1053 | - a list (LEFT TOP WIDTH HEIGHT), describing the frame, | |
063233c3 JB |
1054 | - a list (LEFT TOP WIDTH HEIGHT), describing the workarea. |
1055 | It must return non-nil to force the frame onscreen, nil otherwise. | |
d5671a82 JB |
1056 | |
1057 | Note the timing and scope of the operations described above: REUSE-FRAMES | |
3677ffeb JB |
1058 | affects existing frames; PREDICATE, FILTERS and FORCE-DISPLAY affect the frame |
1059 | being restored before that happens; and FORCE-ONSCREEN affects the frame once | |
d5671a82 | 1060 | it has been restored. |
9421876d | 1061 | |
a912c016 | 1062 | All keyword parameters default to nil." |
9421876d | 1063 | |
76c5e5ab | 1064 | (cl-assert (frameset-valid-p frameset)) |
9421876d | 1065 | |
d5671a82 | 1066 | (let (other-frames) |
9421876d JB |
1067 | |
1068 | ;; frameset--reuse-list is a list of frames potentially reusable. Later we | |
1069 | ;; will decide which ones can be reused, and how to deal with any leftover. | |
1070 | (pcase reuse-frames | |
d5671a82 | 1071 | ((or `nil `:keep) |
9421876d JB |
1072 | (setq frameset--reuse-list nil |
1073 | other-frames (frame-list))) | |
1074 | ((pred consp) | |
1075 | (setq frameset--reuse-list (copy-sequence reuse-frames) | |
1076 | other-frames (cl-delete-if (lambda (frame) | |
a912c016 JB |
1077 | (memq frame frameset--reuse-list)) |
1078 | (frame-list)))) | |
9421876d JB |
1079 | (_ |
1080 | (setq frameset--reuse-list (frame-list) | |
1081 | other-frames nil))) | |
1082 | ||
1083 | ;; Sort saved states to guarantee that minibufferless frames will be created | |
1084 | ;; after the frames that contain their minibuffer windows. | |
1085 | (dolist (state (sort (copy-sequence (frameset-states frameset)) | |
063233c3 | 1086 | #'frameset--minibufferless-last-p)) |
a912c016 JB |
1087 | (pcase-let ((`(,frame-cfg . ,window-cfg) state)) |
1088 | (when (or (null predicate) (funcall predicate frame-cfg window-cfg)) | |
1089 | (condition-case-unless-debug err | |
1090 | (let* ((d-mini (cdr (assq 'frameset--mini frame-cfg))) | |
1091 | (mb-id (cdr d-mini)) | |
1092 | (default (and (booleanp mb-id) mb-id)) | |
1093 | (force-display (if (functionp force-display) | |
1094 | (funcall force-display frame-cfg window-cfg) | |
1095 | force-display)) | |
1096 | frame to-tty) | |
1097 | ;; Only set target if forcing displays and the target display is different. | |
1098 | (cond ((frameset-keep-original-display-p force-display) | |
1099 | (setq frameset--target-display nil)) | |
1100 | ((eq (frame-parameter nil 'display) (cdr (assq 'display frame-cfg))) | |
1101 | (setq frameset--target-display nil)) | |
1102 | (t | |
1103 | (setq frameset--target-display (cons 'display | |
1104 | (frame-parameter nil 'display)) | |
1105 | to-tty (null (cdr frameset--target-display))))) | |
1106 | ;; Time to restore frames and set up their minibuffers as they were. | |
1107 | ;; We only skip a frame (thus deleting it) if either: | |
1108 | ;; - we're switching displays, and the user chose the option to delete, or | |
1109 | ;; - we're switching to tty, and the frame to restore is minibuffer-only. | |
1110 | (unless (and frameset--target-display | |
1111 | (or (eq force-display :delete) | |
1112 | (and to-tty | |
1113 | (eq (cdr (assq 'minibuffer frame-cfg)) 'only)))) | |
1114 | ;; If keeping non-reusable frames, and the frameset--id of one of them | |
1115 | ;; matches the id of a frame being restored (because, for example, the | |
1116 | ;; frameset has already been read in the same session), remove the | |
1117 | ;; frameset--id from the non-reusable frame, which is not useful anymore. | |
1118 | (when (and other-frames | |
1119 | (or (eq reuse-frames :keep) (consp reuse-frames))) | |
1120 | (let ((dup (frameset-frame-with-id (cdr (assq 'frameset--id frame-cfg)) | |
1121 | other-frames))) | |
1122 | (when dup | |
1123 | (set-frame-parameter dup 'frameset--id nil)))) | |
1124 | ;; Restore minibuffers. Some of this stuff could be done in a filter | |
1125 | ;; function, but it would be messy because restoring minibuffers affects | |
1126 | ;; global state; it's best to do it here than add a bunch of global | |
1127 | ;; variables to pass info back-and-forth to/from the filter function. | |
1128 | (cond | |
1129 | ((null d-mini)) ;; No frameset--mini. Process as normal frame. | |
1130 | (to-tty) ;; Ignore minibuffer stuff and process as normal frame. | |
1131 | ((car d-mini) ;; Frame has minibuffer (or it is minibuffer-only). | |
1132 | (when (eq (cdr (assq 'minibuffer frame-cfg)) 'only) | |
1133 | (setq frame-cfg (append '((tool-bar-lines . 0) (menu-bar-lines . 0)) | |
1134 | frame-cfg)))) | |
1135 | (t ;; Frame depends on other frame's minibuffer window. | |
1136 | (let* ((mb-frame (or (frameset-frame-with-id mb-id) | |
1137 | (error "Minibuffer frame %S not found" mb-id))) | |
1138 | (mb-param (assq 'minibuffer frame-cfg)) | |
1139 | (mb-window (minibuffer-window mb-frame))) | |
1140 | (unless (and (window-live-p mb-window) | |
1141 | (window-minibuffer-p mb-window)) | |
1142 | (error "Not a minibuffer window %s" mb-window)) | |
1143 | (if mb-param | |
1144 | (setcdr mb-param mb-window) | |
1145 | (push (cons 'minibuffer mb-window) frame-cfg))))) | |
1146 | ;; OK, we're ready at last to create (or reuse) a frame and | |
1147 | ;; restore the window config. | |
1148 | (setq frame (frameset--restore-frame frame-cfg window-cfg | |
1149 | (or filters frameset-filter-alist) | |
1150 | force-onscreen)) | |
1151 | ;; Set default-minibuffer if required. | |
1152 | (when default (setq default-minibuffer-frame frame)))) | |
1153 | (error | |
1154 | (delay-warning 'frameset (error-message-string err) :error)))))) | |
9421876d JB |
1155 | |
1156 | ;; In case we try to delete the initial frame, we want to make sure that | |
1157 | ;; other frames are already visible (discussed in thread for bug#14841). | |
1158 | (sit-for 0 t) | |
1159 | ||
1160 | ;; Delete remaining frames, but do not fail if some resist being deleted. | |
d5671a82 | 1161 | (unless (eq reuse-frames :keep) |
9421876d JB |
1162 | (dolist (frame (sort (nconc (if (listp reuse-frames) nil other-frames) |
1163 | frameset--reuse-list) | |
063233c3 JB |
1164 | ;; Minibufferless frames must go first to avoid |
1165 | ;; errors when attempting to delete a frame whose | |
1166 | ;; minibuffer window is used by another frame. | |
1167 | #'frameset-minibufferless-first-p)) | |
9421876d JB |
1168 | (condition-case err |
1169 | (delete-frame frame) | |
1170 | (error | |
1171 | (delay-warning 'frameset (error-message-string err)))))) | |
a912c016 JB |
1172 | (setq frameset--reuse-list nil |
1173 | frameset--target-display nil) | |
9421876d JB |
1174 | |
1175 | ;; Make sure there's at least one visible frame. | |
1176 | (unless (or (daemonp) (visible-frame-list)) | |
1177 | (make-frame-visible (car (frame-list)))))) | |
1178 | ||
1179 | (provide 'frameset) | |
1180 | ||
1181 | ;;; frameset.el ends here |