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 | |
44 | ;; Framesets have two fields: | |
45 | ;; - properties: a property list to store both frameset-specific and | |
46 | ;; user-defined serializable data. Currently defined properties | |
47 | ;; include: | |
48 | ;; :version ID - Identifies the version of the frameset struct; | |
49 | ;; this is the only property always present and | |
50 | ;; must not be modified. | |
51 | ;; :app APPINFO - Freeform. Can be used by applications and | |
52 | ;; packages to indicate the intended (but by no | |
53 | ;; means exclusive) use of the frameset. For | |
54 | ;; example, currently desktop.el sets :app to | |
55 | ;; `(desktop . ,desktop-file-version). | |
56 | ;; :name NAME - The name of the frameset instance; a string. | |
57 | ;; :desc TEXT - A description for user consumption (to choose | |
58 | ;; among framesets, etc.); a string. | |
59 | ;; - states: an alist of items (FRAME-PARAMETERS . WINDOW-STATE) in | |
60 | ;; no particular order. Each item represents a frame to be | |
61 | ;; restored. | |
62 | ||
63 | (cl-defstruct (frameset (:type list) :named | |
64 | (:copier nil) | |
65 | (:predicate nil)) | |
66 | properties ;; property list | |
67 | states) ;; list of conses (frame-state . window-state) | |
68 | ||
69 | (defun copy-frameset (frameset) | |
70 | "Return a copy of FRAMESET. | |
71 | This is a deep copy done with `copy-tree'." | |
72 | (copy-tree frameset t)) | |
73 | ||
74 | ;;;autoload | |
75 | (defun frameset-p (frameset) | |
76 | "If FRAMESET is a frameset, return its :version. | |
77 | Else return nil." | |
78 | (and (eq (car-safe frameset) 'frameset) | |
79 | (plist-get (cl-second frameset) :version))) | |
80 | ||
81 | \f | |
82 | ;; Filtering | |
83 | ||
84 | (defvar frameset-filter-alist | |
85 | '((background-color . frameset-filter-sanitize-color) | |
86 | (buffer-list . t) | |
87 | (buffer-predicate . t) | |
88 | (buried-buffer-list . t) | |
89 | (font . frameset-filter-save-parm) | |
90 | (foreground-color . frameset-filter-sanitize-color) | |
91 | (fullscreen . frameset-filter-save-parm) | |
92 | (GUI:font . frameset-filter-restore-parm) | |
93 | (GUI:fullscreen . frameset-filter-restore-parm) | |
94 | (GUI:height . frameset-filter-restore-parm) | |
95 | (GUI:width . frameset-filter-restore-parm) | |
96 | (height . frameset-filter-save-parm) | |
97 | (left . frameset-filter-iconified) | |
98 | (minibuffer . frameset-filter-minibuffer) | |
99 | (top . frameset-filter-iconified) | |
100 | (width . frameset-filter-save-parm)) | |
101 | "Alist of frame parameters and filtering functions. | |
102 | ||
103 | Each element is a cons (PARAM . ACTION), where PARAM is a parameter | |
104 | name (a symbol identifying a frame parameter), and ACTION can be: | |
105 | ||
106 | t The parameter is always removed from the parameter list. | |
107 | :save The parameter is removed when saving the frame. | |
108 | :restore The parameter is removed when restoring the frame. | |
109 | FILTER A filter function. | |
110 | ||
111 | FILTER can be a symbol FILTER-FUN, or a list (FILTER-FUN ARGS...). | |
112 | It will be called with four arguments CURRENT, FILTERED, PARAMETERS | |
113 | and SAVING, plus any additional ARGS: | |
114 | ||
115 | CURRENT A cons (PARAM . VALUE), where PARAM is the one being | |
116 | filtered and VALUE is its current value. | |
117 | FILTERED The alist of parameters filtered so far. | |
118 | PARAMETERS The complete alist of parameters being filtered, | |
119 | SAVING Non-nil if filtering before saving state, nil otherwise. | |
120 | ||
121 | The FILTER-FUN function must return: | |
122 | nil CURRENT is removed from the list. | |
123 | t CURRENT is left as is. | |
124 | (PARAM' . VALUE') Replace CURRENT with this. | |
125 | ||
126 | Frame parameters not on this list are passed intact.") | |
127 | ||
128 | (defvar frameset--target-display nil | |
129 | ;; Either (minibuffer . VALUE) or nil. | |
130 | ;; This refers to the current frame config being processed inside | |
131 | ;; `frame--restore-frames' and its auxiliary functions (like filtering). | |
132 | ;; If nil, there is no need to change the display. | |
133 | ;; If non-nil, display parameter to use when creating the frame. | |
134 | "Internal use only.") | |
135 | ||
136 | (defun frameset-switch-to-gui-p (parameters) | |
137 | "True when switching to a graphic display. | |
138 | Return t if PARAMETERS describes a text-only terminal and | |
139 | the target is a graphic display; otherwise return nil. | |
140 | Only meaningful when called from a filtering function in | |
141 | `frameset-filter-alist'." | |
142 | (and frameset--target-display ; we're switching | |
143 | (null (cdr (assq 'display parameters))) ; from a tty | |
144 | (cdr frameset--target-display))) ; to a GUI display | |
145 | ||
146 | (defun frameset-switch-to-tty-p (parameters) | |
147 | "True when switching to a text-only terminal. | |
148 | Return t if PARAMETERS describes a graphic display and | |
149 | the target is a text-only terminal; otherwise return nil. | |
150 | Only meaningful when called from a filtering function in | |
151 | `frameset-filter-alist'." | |
152 | (and frameset--target-display ; we're switching | |
153 | (cdr (assq 'display parameters)) ; from a GUI display | |
154 | (null (cdr frameset--target-display)))) ; to a tty | |
155 | ||
156 | (defun frameset-filter-sanitize-color (current _filtered parameters saving) | |
157 | "When switching to a GUI frame, remove \"unspecified\" colors. | |
158 | Useful as a filter function for tty-specific parameters." | |
159 | (or saving | |
160 | (not (frameset-switch-to-gui-p parameters)) | |
161 | (not (stringp (cdr current))) | |
162 | (not (string-match-p "^unspecified-[fb]g$" (cdr current))))) | |
163 | ||
164 | (defun frameset-filter-minibuffer (current _filtered _parameters saving) | |
165 | "Convert (minibuffer . #<window>) parameter to (minibuffer . t)." | |
166 | (or (not saving) | |
167 | (if (windowp (cdr current)) | |
168 | '(minibuffer . t) | |
169 | t))) | |
170 | ||
171 | (defun frameset-filter-save-parm (current _filtered parameters saving | |
172 | &optional prefix) | |
173 | "When switching to a tty frame, save parameter P as PREFIX:P. | |
174 | The parameter can be later restored with `frameset-filter-restore-parm'. | |
175 | PREFIX defaults to `GUI'." | |
176 | (unless prefix (setq prefix 'GUI)) | |
177 | (cond (saving t) | |
178 | ((frameset-switch-to-tty-p parameters) | |
179 | (let ((prefix:p (intern (format "%s:%s" prefix (car current))))) | |
180 | (if (assq prefix:p parameters) | |
181 | nil | |
182 | (cons prefix:p (cdr current))))) | |
183 | ((frameset-switch-to-gui-p parameters) | |
184 | (not (assq (intern (format "%s:%s" prefix (car current))) parameters))) | |
185 | (t t))) | |
186 | ||
187 | (defun frameset-filter-restore-parm (current filtered parameters saving) | |
188 | "When switching to a GUI frame, restore PREFIX:P parameter as P. | |
189 | CURRENT must be of the form (PREFIX:P . value)." | |
190 | (or saving | |
191 | (not (frameset-switch-to-gui-p parameters)) | |
192 | (let* ((prefix:p (symbol-name (car current))) | |
193 | (p (intern (substring prefix:p | |
194 | (1+ (string-match-p ":" prefix:p))))) | |
195 | (val (cdr current)) | |
196 | (found (assq p filtered))) | |
197 | (if (not found) | |
198 | (cons p val) | |
199 | (setcdr found val) | |
200 | nil)))) | |
201 | ||
202 | (defun frameset-filter-iconified (_current _filtered parameters saving) | |
203 | "Remove CURRENT when saving an iconified frame. | |
204 | This is used for positions parameters `left' and `top', which are | |
205 | meaningless in an iconified frame, so the frame is restored in a | |
206 | default position." | |
207 | (not (and saving (eq (cdr (assq 'visibility parameters)) 'icon)))) | |
208 | ||
209 | (defun frameset-keep-original-display-p (force-display) | |
210 | "True if saved frames' displays should be honored." | |
211 | (cond ((daemonp) t) | |
212 | ((eq system-type 'windows-nt) nil) | |
213 | (t (null force-display)))) | |
214 | ||
215 | (defun frameset-filter-params (parameters filter-alist saving) | |
216 | "Filter parameter list PARAMETERS and return a filtered list. | |
217 | FILTER-ALIST is an alist of parameter filters, in the format of | |
218 | `frameset-filter-alist' (which see). | |
219 | SAVING is non-nil while filtering parameters to save a frameset, | |
220 | nil while the filtering is done to restore it." | |
221 | (let ((filtered nil)) | |
222 | (dolist (current parameters) | |
223 | (pcase (cdr (assq (car current) filter-alist)) | |
224 | (`nil | |
225 | (push current filtered)) | |
226 | (`t | |
227 | nil) | |
228 | (:save | |
229 | (unless saving (push current filtered))) | |
230 | (:restore | |
231 | (when saving (push current filtered))) | |
232 | ((or `(,fun . ,args) (and fun (pred fboundp))) | |
233 | (let ((this (apply fun filtered current parameters saving args))) | |
234 | (when this | |
235 | (push (if (eq this t) current this) filtered)))) | |
236 | (other | |
237 | (delay-warning 'frameset (format "Unknown filter %S" other) :error)))) | |
238 | ;; Set the display parameter after filtering, so that filter functions | |
239 | ;; have access to its original value. | |
240 | (when frameset--target-display | |
241 | (let ((display (assq 'display filtered))) | |
242 | (if display | |
243 | (setcdr display (cdr frameset--target-display)) | |
244 | (push frameset--target-display filtered)))) | |
245 | filtered)) | |
246 | ||
247 | \f | |
248 | ;; Saving framesets | |
249 | ||
250 | (defun frameset--set-id (frame) | |
251 | "Set FRAME's `frameset-id' if not yet set. | |
252 | Internal use only." | |
253 | (unless (frame-parameter frame 'frameset-id) | |
254 | (set-frame-parameter frame | |
255 | 'frameset-id | |
256 | (mapconcat (lambda (n) (format "%04X" n)) | |
257 | (cl-loop repeat 4 collect (random 65536)) | |
258 | "-")))) | |
259 | ||
260 | (defun frameset--process-minibuffer-frames (frame-list) | |
261 | "Process FRAME-LIST and record minibuffer relationships. | |
262 | FRAME-LIST is a list of frames." | |
263 | ;; Record frames with their own minibuffer | |
264 | (dolist (frame (minibuffer-frame-list)) | |
265 | (when (memq frame frame-list) | |
266 | (frameset--set-id frame) | |
267 | ;; For minibuffer-owning frames, frameset--mini is a cons | |
268 | ;; (t . DEFAULT?), where DEFAULT? is a boolean indicating whether | |
269 | ;; the frame is the one pointed out by `default-minibuffer-frame'. | |
270 | (set-frame-parameter frame | |
271 | 'frameset--mini | |
272 | (cons t (eq frame default-minibuffer-frame))))) | |
273 | ;; Now link minibufferless frames with their minibuffer frames | |
274 | (dolist (frame frame-list) | |
275 | (unless (frame-parameter frame 'frameset--mini) | |
276 | (frameset--set-id frame) | |
277 | (let* ((mb-frame (window-frame (minibuffer-window frame))) | |
278 | (id (and mb-frame (frame-parameter mb-frame 'frameset-id)))) | |
279 | (if (null id) | |
280 | (error "Minibuffer frame %S for %S is excluded" mb-frame frame) | |
281 | ;; For minibufferless frames, frameset--mini is a cons | |
282 | ;; (nil . FRAME-ID), where FRAME-ID is the frameset-id of | |
283 | ;; the frame containing its minibuffer window. | |
284 | (set-frame-parameter frame | |
285 | 'frameset--mini | |
286 | (cons nil id))))))) | |
287 | ||
288 | ;;;autoload | |
289 | (cl-defun frameset-save (frame-list &key filters predicate properties) | |
290 | "Return the frameset of FRAME-LIST, a list of frames. | |
291 | If nil, FRAME-LIST defaults to all live frames. | |
292 | FILTERS is an alist of parameter filters; defaults to `frameset-filter-alist'. | |
293 | PREDICATE is a predicate function, which must return non-nil for frames that | |
294 | should be saved; it defaults to saving all frames from FRAME-LIST. | |
295 | PROPERTIES is a user-defined property list to add to the frameset." | |
296 | (let ((frames (cl-delete-if-not #'frame-live-p | |
297 | (cl-remove-if-not (or predicate #'framep) | |
298 | (or frame-list (frame-list)))))) | |
299 | (frameset--process-minibuffer-frames frames) | |
300 | (make-frameset :properties (append '(:version 1) properties) | |
301 | :states (mapcar | |
302 | (lambda (frame) | |
303 | (cons | |
304 | (frameset-filter-params (frame-parameters frame) | |
305 | (or filters | |
306 | frameset-filter-alist) | |
307 | t) | |
308 | (window-state-get (frame-root-window frame) t))) | |
309 | frames)))) | |
310 | ||
311 | \f | |
312 | ;; Restoring framesets | |
313 | ||
314 | (defvar frameset--reuse-list nil | |
315 | "Internal use only.") | |
316 | ||
317 | (defun frameset--compute-pos (value left/top right/bottom) | |
318 | (pcase value | |
319 | (`(+ ,val) (+ left/top val)) | |
320 | (`(- ,val) (+ right/bottom val)) | |
321 | (val val))) | |
322 | ||
323 | (defun frameset--move-onscreen (frame force-onscreen) | |
324 | "If FRAME is offscreen, move it back onscreen and, if necessary, resize it. | |
325 | For the description of FORCE-ONSCREEN, see `frameset-restore'. | |
326 | When forced onscreen, frames wider than the monitor's workarea are converted | |
327 | to fullwidth, and frames taller than the workarea are converted to fullheight. | |
328 | NOTE: This only works for non-iconified frames. Internal use only." | |
329 | (pcase-let* ((`(,left ,top ,width ,height) (cl-cdadr (frame-monitor-attributes frame))) | |
330 | (right (+ left width -1)) | |
331 | (bottom (+ top height -1)) | |
332 | (fr-left (frameset--compute-pos (frame-parameter frame 'left) left right)) | |
333 | (fr-top (frameset--compute-pos (frame-parameter frame 'top) top bottom)) | |
334 | (ch-width (frame-char-width frame)) | |
335 | (ch-height (frame-char-height frame)) | |
336 | (fr-width (max (frame-pixel-width frame) (* ch-width (frame-width frame)))) | |
337 | (fr-height (max (frame-pixel-height frame) (* ch-height (frame-height frame)))) | |
338 | (fr-right (+ fr-left fr-width -1)) | |
339 | (fr-bottom (+ fr-top fr-height -1))) | |
340 | (when (pcase force-onscreen | |
341 | ;; Any corner is outside the screen. | |
342 | (`all (or (< fr-bottom top) (> fr-bottom bottom) | |
343 | (< fr-left left) (> fr-left right) | |
344 | (< fr-right left) (> fr-right right) | |
345 | (< fr-top top) (> fr-top bottom))) | |
346 | ;; Displaced to the left, right, above or below the screen. | |
347 | (`t (or (> fr-left right) | |
348 | (< fr-right left) | |
349 | (> fr-top bottom) | |
350 | (< fr-bottom top))) | |
351 | ;; Fully inside, no need to do anything. | |
352 | (_ nil)) | |
353 | (let ((fullwidth (> fr-width width)) | |
354 | (fullheight (> fr-height height)) | |
355 | (params nil)) | |
356 | ;; Position frame horizontally. | |
357 | (cond (fullwidth | |
358 | (push `(left . ,left) params)) | |
359 | ((> fr-right right) | |
360 | (push `(left . ,(+ left (- width fr-width))) params)) | |
361 | ((< fr-left left) | |
362 | (push `(left . ,left) params))) | |
363 | ;; Position frame vertically. | |
364 | (cond (fullheight | |
365 | (push `(top . ,top) params)) | |
366 | ((> fr-bottom bottom) | |
367 | (push `(top . ,(+ top (- height fr-height))) params)) | |
368 | ((< fr-top top) | |
369 | (push `(top . ,top) params))) | |
370 | ;; Compute fullscreen state, if required. | |
371 | (when (or fullwidth fullheight) | |
372 | (push (cons 'fullscreen | |
373 | (cond ((not fullwidth) 'fullheight) | |
374 | ((not fullheight) 'fullwidth) | |
375 | (t 'maximized))) | |
376 | params)) | |
377 | ;; Finally, move the frame back onscreen. | |
378 | (when params | |
379 | (modify-frame-parameters frame params)))))) | |
380 | ||
381 | (defun frameset--find-frame (predicate display &rest args) | |
382 | "Find a frame in `frameset--reuse-list' satisfying PREDICATE. | |
383 | Look through available frames whose display property matches DISPLAY | |
384 | and return the first one for which (PREDICATE frame ARGS) returns t. | |
385 | If PREDICATE is nil, it is always satisfied. Internal use only." | |
386 | (cl-find-if (lambda (frame) | |
387 | (and (equal (frame-parameter frame 'display) display) | |
388 | (or (null predicate) | |
389 | (apply predicate frame args)))) | |
390 | frameset--reuse-list)) | |
391 | ||
392 | (defun frameset--reuse-frame (display frame-cfg) | |
393 | "Look for an existing frame to reuse. | |
394 | DISPLAY is the display where the frame will be shown, and FRAME-CFG | |
395 | is the parameter list of the frame being restored. Internal use only." | |
396 | (let ((frame nil) | |
397 | mini) | |
398 | ;; There are no fancy heuristics there. We could implement some | |
399 | ;; based on frame size and/or position, etc., but it is not clear | |
400 | ;; that any "gain" (in the sense of reduced flickering, etc.) is | |
401 | ;; worth the added complexity. In fact, the code below mainly | |
402 | ;; tries to work nicely when M-x desktop-read is used after a | |
403 | ;; desktop session has already been loaded. The other main use | |
404 | ;; case, which is the initial desktop-read upon starting Emacs, | |
405 | ;; will usually have only one frame, and should already work. | |
406 | (cond ((null display) | |
407 | ;; When the target is tty, every existing frame is reusable. | |
408 | (setq frame (frameset--find-frame nil display))) | |
409 | ((car (setq mini (cdr (assq 'frameset--mini frame-cfg)))) | |
410 | ;; If the frame has its own minibuffer, let's see whether | |
411 | ;; that frame has already been loaded (which can happen after | |
412 | ;; M-x desktop-read). | |
413 | (setq frame (frameset--find-frame | |
414 | (lambda (f id) | |
415 | (string= (frame-parameter f 'frameset-id) id)) | |
416 | display (cdr mini))) | |
417 | ;; If it has not been loaded, and it is not a minibuffer-only frame, | |
418 | ;; let's look for an existing non-minibuffer-only frame to reuse. | |
419 | (unless (or frame (eq (cdr (assq 'minibuffer frame-cfg)) 'only)) | |
420 | (setq frame (frameset--find-frame | |
421 | (lambda (f) | |
422 | (let ((w (frame-parameter f 'minibuffer))) | |
423 | (and (window-live-p w) | |
424 | (window-minibuffer-p w) | |
425 | (eq (window-frame w) f)))) | |
426 | display)))) | |
427 | (mini | |
428 | ;; For minibufferless frames, check whether they already exist, | |
429 | ;; and that they are linked to the right minibuffer frame. | |
430 | (setq frame (frameset--find-frame | |
431 | (lambda (f id mini-id) | |
432 | (and (string= (frame-parameter f 'frameset-id) id) | |
433 | (string= (frame-parameter (window-frame (minibuffer-window f)) | |
434 | 'frameset-id) | |
435 | mini-id))) | |
436 | display (cdr (assq 'frameset-id frame-cfg)) (cdr mini)))) | |
437 | (t | |
438 | ;; Default to just finding a frame in the same display. | |
439 | (setq frame (frameset--find-frame nil display)))) | |
440 | ;; If found, remove from the list. | |
441 | (when frame | |
442 | (setq frameset--reuse-list (delq frame frameset--reuse-list))) | |
443 | frame)) | |
444 | ||
445 | (defun frameset--get-frame (frame-cfg window-cfg filters force-onscreen) | |
446 | "Set up and return a frame according to its saved state. | |
447 | That means either reusing an existing frame or creating one anew. | |
448 | FRAME-CFG is the frame's parameter list; WINDOW-CFG is its window state. | |
449 | For the meaning of FORCE-ONSCREEN, see `frameset-restore'." | |
450 | (let* ((fullscreen (cdr (assq 'fullscreen frame-cfg))) | |
451 | (lines (assq 'tool-bar-lines frame-cfg)) | |
452 | (filtered-cfg (frameset-filter-params frame-cfg filters nil)) | |
453 | (display (cdr (assq 'display filtered-cfg))) ;; post-filtering | |
454 | alt-cfg frame) | |
455 | ||
456 | ;; This works around bug#14795 (or feature#14795, if not a bug :-) | |
457 | (setq filtered-cfg (assq-delete-all 'tool-bar-lines filtered-cfg)) | |
458 | (push '(tool-bar-lines . 0) filtered-cfg) | |
459 | ||
460 | (when fullscreen | |
461 | ;; Currently Emacs has the limitation that it does not record the size | |
462 | ;; and position of a frame before maximizing it, so we cannot save & | |
463 | ;; restore that info. Instead, when restoring, we resort to creating | |
464 | ;; invisible "fullscreen" frames of default size and then maximizing them | |
465 | ;; (and making them visible) which at least is somewhat user-friendly | |
466 | ;; when these frames are later de-maximized. | |
467 | (let ((width (and (eq fullscreen 'fullheight) (cdr (assq 'width filtered-cfg)))) | |
468 | (height (and (eq fullscreen 'fullwidth) (cdr (assq 'height filtered-cfg)))) | |
469 | (visible (assq 'visibility filtered-cfg))) | |
470 | (setq filtered-cfg (cl-delete-if (lambda (p) | |
471 | (memq p '(visibility fullscreen width height))) | |
472 | filtered-cfg :key #'car)) | |
473 | (when width | |
474 | (setq filtered-cfg (append `((user-size . t) (width . ,width)) | |
475 | filtered-cfg))) | |
476 | (when height | |
477 | (setq filtered-cfg (append `((user-size . t) (height . ,height)) | |
478 | filtered-cfg))) | |
479 | ;; These are parameters to apply after creating/setting the frame. | |
480 | (push visible alt-cfg) | |
481 | (push (cons 'fullscreen fullscreen) alt-cfg))) | |
482 | ||
483 | ;; Time to find or create a frame an apply the big bunch of parameters. | |
484 | ;; If a frame needs to be created and it falls partially or fully offscreen, | |
485 | ;; sometimes it gets "pushed back" onscreen; however, moving it afterwards is | |
486 | ;; allowed. So we create the frame as invisible and then reapply the full | |
487 | ;; parameter list (including position and size parameters). | |
488 | (setq frame (or (and frameset--reuse-list | |
489 | (frameset--reuse-frame display filtered-cfg)) | |
490 | (make-frame-on-display display | |
491 | (cons '(visibility) | |
492 | (cl-loop | |
493 | for param in '(left top width height minibuffer) | |
494 | collect (assq param filtered-cfg)))))) | |
495 | (modify-frame-parameters frame | |
496 | (if (eq (frame-parameter frame 'fullscreen) fullscreen) | |
497 | ;; Workaround for bug#14949 | |
498 | (assq-delete-all 'fullscreen filtered-cfg) | |
499 | filtered-cfg)) | |
500 | ||
501 | ;; If requested, force frames to be onscreen. | |
502 | (when (and force-onscreen | |
503 | ;; FIXME: iconified frames should be checked too, | |
504 | ;; but it is impossible without deiconifying them. | |
505 | (not (eq (frame-parameter frame 'visibility) 'icon))) | |
506 | (frameset--move-onscreen frame force-onscreen)) | |
507 | ||
508 | ;; Let's give the finishing touches (visibility, tool-bar, maximization). | |
509 | (when lines (push lines alt-cfg)) | |
510 | (when alt-cfg (modify-frame-parameters frame alt-cfg)) | |
511 | ;; Now restore window state. | |
512 | (window-state-put window-cfg (frame-root-window frame) 'safe) | |
513 | frame)) | |
514 | ||
515 | (defun frameset--sort-states (state1 state2) | |
516 | "Predicate to sort frame states in a suitable order to be created. | |
517 | It sorts minibuffer-owning frames before minibufferless ones." | |
518 | (pcase-let ((`(,hasmini1 ,id-def1) (assq 'frameset--mini (car state1))) | |
519 | (`(,hasmini2 ,id-def2) (assq 'frameset--mini (car state2)))) | |
520 | (cond ((eq id-def1 t) t) | |
521 | ((eq id-def2 t) nil) | |
522 | ((not (eq hasmini1 hasmini2)) (eq hasmini1 t)) | |
523 | ((eq hasmini1 nil) (string< id-def1 id-def2)) | |
524 | (t t)))) | |
525 | ||
526 | (defun frameset-sort-frames-for-deletion (frame1 _frame2) | |
527 | "Predicate to sort live frames for deletion. | |
528 | Minibufferless frames must go first to avoid errors when attempting | |
529 | to delete a frame whose minibuffer window is used by another frame." | |
530 | (not (frame-parameter frame1 'minibuffer))) | |
531 | ||
532 | ;;;autoload | |
533 | (cl-defun frameset-restore (frameset &key filters reuse-frames force-display force-onscreen) | |
534 | "Restore a FRAMESET into the current display(s). | |
535 | ||
536 | FILTERS is a list of parameter filters; defaults to `frameset-filter-alist'. | |
537 | ||
538 | REUSE-FRAMES describes how to reuse existing frames while restoring a frameset: | |
539 | t Reuse any existing frame if possible; delete leftover frames. | |
540 | nil Restore frameset in new frames and delete existing frames. | |
541 | keep Restore frameset in new frames and keep the existing ones. | |
542 | LIST A list of frames to reuse; only these will be reused, if possible, | |
543 | and any leftover one will be deleted; other frames not on this | |
544 | list will be kept. | |
545 | ||
546 | FORCE-DISPLAY can be: | |
547 | t Frames will be restored in the current display. | |
548 | nil Frames will be restored, if possible, in their original displays. | |
549 | delete Frames in other displays will be deleted instead of restored. | |
550 | ||
551 | FORCE-ONSCREEN can be: | |
552 | all Force onscreen any frame fully or partially offscreen. | |
553 | t Force onscreen only those frames that are fully offscreen. | |
554 | nil Do not force any frame back onscreen. | |
555 | ||
556 | All keywords default to nil." | |
557 | ||
558 | (cl-assert (frameset-p frameset)) | |
559 | ||
560 | (let* ((delete-saved (eq force-display 'delete)) | |
561 | (forcing (not (frameset-keep-original-display-p force-display))) | |
562 | (target (and forcing (cons 'display (frame-parameter nil 'display)))) | |
563 | other-frames) | |
564 | ||
565 | ;; frameset--reuse-list is a list of frames potentially reusable. Later we | |
566 | ;; will decide which ones can be reused, and how to deal with any leftover. | |
567 | (pcase reuse-frames | |
568 | ((or `nil `keep) | |
569 | (setq frameset--reuse-list nil | |
570 | other-frames (frame-list))) | |
571 | ((pred consp) | |
572 | (setq frameset--reuse-list (copy-sequence reuse-frames) | |
573 | other-frames (cl-delete-if (lambda (frame) | |
574 | (memq frame frameset--reuse-list)) | |
575 | (frame-list)))) | |
576 | (_ | |
577 | (setq frameset--reuse-list (frame-list) | |
578 | other-frames nil))) | |
579 | ||
580 | ;; Sort saved states to guarantee that minibufferless frames will be created | |
581 | ;; after the frames that contain their minibuffer windows. | |
582 | (dolist (state (sort (copy-sequence (frameset-states frameset)) | |
583 | #'frameset--sort-states)) | |
584 | (condition-case-unless-debug err | |
585 | (pcase-let* ((`(,frame-cfg . ,window-cfg) state) | |
586 | ((and d-mini `(,hasmini . ,mb-id)) | |
587 | (cdr (assq 'frameset--mini frame-cfg))) | |
588 | (default (and (booleanp mb-id) mb-id)) | |
589 | (frame nil) (to-tty nil)) | |
590 | ;; Only set target if forcing displays and the target display is different. | |
591 | (if (or (not forcing) | |
592 | (equal target (or (assq 'display frame-cfg) '(display . nil)))) | |
593 | (setq frameset--target-display nil) | |
594 | (setq frameset--target-display target | |
595 | to-tty (null (cdr target)))) | |
596 | ;; If keeping non-reusable frames, and the frame-id of one of them | |
597 | ;; matches the frame-id of a frame being restored (because, for example, | |
598 | ;; the frameset has already been read in the same session), remove the | |
599 | ;; frame-id from the non-reusable frame, which is not useful anymore. | |
600 | (when (and other-frames | |
601 | (or (eq reuse-frames 'keep) (consp reuse-frames))) | |
602 | (let ((dup (cl-find (cdr (assq 'frameset-frame-id frame-cfg)) | |
603 | other-frames | |
604 | :key (lambda (frame) | |
605 | (frame-parameter frame 'frameset-frame-id)) | |
606 | :test #'string=))) | |
607 | (when dup | |
608 | (set-frame-parameter dup 'frameset-frame-id nil)))) | |
609 | ;; Time to restore frames and set up their minibuffers as they were. | |
610 | ;; We only skip a frame (thus deleting it) if either: | |
611 | ;; - we're switching displays, and the user chose the option to delete, or | |
612 | ;; - we're switching to tty, and the frame to restore is minibuffer-only. | |
613 | (unless (and frameset--target-display | |
614 | (or delete-saved | |
615 | (and to-tty | |
616 | (eq (cdr (assq 'minibuffer frame-cfg)) 'only)))) | |
617 | ||
618 | ;; Restore minibuffers. Some of this stuff could be done in a filter | |
619 | ;; function, but it would be messy because restoring minibuffers affects | |
620 | ;; global state; it's best to do it here than add a bunch of global | |
621 | ;; variables to pass info back-and-forth to/from the filter function. | |
622 | (cond | |
623 | ((null d-mini)) ;; No frameset--mini. Process as normal frame. | |
624 | (to-tty) ;; Ignore minibuffer stuff and process as normal frame. | |
625 | (hasmini ;; Frame has minibuffer (or it is minibuffer-only). | |
626 | (when (eq (cdr (assq 'minibuffer frame-cfg)) 'only) | |
627 | (setq frame-cfg (append '((tool-bar-lines . 0) (menu-bar-lines . 0)) | |
628 | frame-cfg)))) | |
629 | (t ;; Frame depends on other frame's minibuffer window. | |
630 | (let* ((mb-frame (or (cl-find-if | |
631 | (lambda (f) | |
632 | (string= (frame-parameter f 'frameset-id) | |
633 | mb-id)) | |
634 | (frame-list)) | |
635 | (error "Minibuffer frame %S not found" mb-id))) | |
636 | (mb-param (assq 'minibuffer frame-cfg)) | |
637 | (mb-window (minibuffer-window mb-frame))) | |
638 | (unless (and (window-live-p mb-window) | |
639 | (window-minibuffer-p mb-window)) | |
640 | (error "Not a minibuffer window %s" mb-window)) | |
641 | (if mb-param | |
642 | (setcdr mb-param mb-window) | |
643 | (push (cons 'minibuffer mb-window) frame-cfg)))))) | |
644 | ;; OK, we're ready at last to create (or reuse) a frame and | |
645 | ;; restore the window config. | |
646 | (setq frame (frameset--get-frame frame-cfg window-cfg | |
647 | (or filters frameset-filter-alist) | |
648 | force-onscreen)) | |
649 | ;; Set default-minibuffer if required. | |
650 | (when default (setq default-minibuffer-frame frame))) | |
651 | (error | |
652 | (delay-warning 'frameset (error-message-string err) :error)))) | |
653 | ||
654 | ;; In case we try to delete the initial frame, we want to make sure that | |
655 | ;; other frames are already visible (discussed in thread for bug#14841). | |
656 | (sit-for 0 t) | |
657 | ||
658 | ;; Delete remaining frames, but do not fail if some resist being deleted. | |
659 | (unless (eq reuse-frames 'keep) | |
660 | (dolist (frame (sort (nconc (if (listp reuse-frames) nil other-frames) | |
661 | frameset--reuse-list) | |
662 | #'frameset-sort-frames-for-deletion)) | |
663 | (condition-case err | |
664 | (delete-frame frame) | |
665 | (error | |
666 | (delay-warning 'frameset (error-message-string err)))))) | |
667 | (setq frameset--reuse-list nil) | |
668 | ||
669 | ;; Make sure there's at least one visible frame. | |
670 | (unless (or (daemonp) (visible-frame-list)) | |
671 | (make-frame-visible (car (frame-list)))))) | |
672 | ||
673 | (provide 'frameset) | |
674 | ||
675 | ;;; frameset.el ends here |