Update FSF's address.
[bpt/emacs.git] / lisp / frame.el
CommitLineData
dc6d9681 1;;; frame.el --- multi-frame management independent of window systems.
c88ab9ce 2
b578f267 3;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
3a801d0c 4
fc68affa 5;; Maintainer: FSF
fd7fa35a 6;; Keywords: internal
fc68affa 7
b578f267
EN
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 2, or (at your option)
13;; 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; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
64c669bc 24
fc68affa
ER
25;;; Code:
26
dc6d9681
JB
27(defvar frame-creation-function nil
28 "Window-system dependent function to call to create a new frame.
29The window system startup file should set this to its frame creation
64c669bc
JB
30function, which should take an alist of parameters as its argument.")
31
7eadab74
JB
32;;; The initial value given here for this must ask for a minibuffer.
33;;; There must always exist a frame with a minibuffer, and after we
34;;; delete the terminal frame, this will be the only frame.
9e2b097b 35(defvar initial-frame-alist '((minibuffer . t))
eaa974e1
JB
36 "Alist of frame parameters for creating the initial X window frame.
37You can set this in your `.emacs' file; for example,
dc6d9681 38 (setq initial-frame-alist '((top . 1) (left . 1) (width . 80) (height . 55)))
de17da15
RS
39Parameters specified here supersede the values given in `default-frame-alist'.
40
41If the value calls for a frame without a minibuffer, and you have not created
42a minibuffer frame on your own, one is created according to
7eadab74 43`minibuffer-frame-alist'.
de17da15
RS
44
45You can specify geometry-related options for just the initial frame
46by setting this variable in your `.emacs' file; however, they won't
85557d7e 47take effect until Emacs reads `.emacs', which happens after first creating
de17da15
RS
48the frame. If you want the frame to have the proper geometry as soon
49as it appears, you need to use this three-step process:
50* Specify X resources to give the geometry you want.
51* Set `default-frame-alist' to override these options so that they
52 don't affect subsequent frames.
53* Set `initial-frame-alist' in a way that matches the X resources,
54 to override what you put in `default-frame-alist'.")
64c669bc 55
7eadab74 56(defvar minibuffer-frame-alist '((width . 80) (height . 2))
eaa974e1
JB
57 "Alist of frame parameters for initially creating a minibuffer frame.
58You can set this in your `.emacs' file; for example,
dc6d9681 59 (setq minibuffer-frame-alist
7eadab74 60 '((top . 1) (left . 1) (width . 80) (height . 2)))
eaa974e1
JB
61Parameters specified here supersede the values given in
62`default-frame-alist'.")
64c669bc 63
dc6d9681 64(defvar pop-up-frame-alist nil
eaa974e1 65 "Alist of frame parameters used when creating pop-up frames.
dc6d9681 66Pop-up frames are used for completions, help, and the like.
64c669bc 67This variable can be set in your init file, like this:
dc6d9681 68 (setq pop-up-frame-alist '((width . 80) (height . 20)))
eb8c3be9 69These supersede the values given in `default-frame-alist'.")
64c669bc 70
dc6d9681 71(setq pop-up-frame-function
64c669bc 72 (function (lambda ()
6eb018ba 73 (make-frame pop-up-frame-alist))))
64c669bc 74
85557d7e 75(defvar special-display-frame-alist
8a9e86e6
RS
76 '((height . 14) (width . 80) (unsplittable . t))
77 "*Alist of frame parameters used when creating special frames.
78Special frames are used for buffers whose names are in
79`special-display-buffer-names' and for buffers whose names match
80one of the regular expressions in `special-display-regexps'.
81This variable can be set in your init file, like this:
82 (setq special-display-frame-alist '((width . 80) (height . 20)))
83These supersede the values given in `default-frame-alist'.")
84
85;; Display BUFFER in its own frame, reusing an existing window if any.
86;; Return the window chosen.
87;; Currently we do not insist on selecting the window within its frame.
95e6cf39 88;; If ARGS is an alist, use it as a list of frame parameter specs.
85557d7e 89;; If ARGS is a list whose car is a symbol,
95e6cf39
RS
90;; use (car ARGS) as a function to do the work.
91;; Pass it BUFFER as first arg, and (cdr ARGS) gives the rest of the args.
92(defun special-display-popup-frame (buffer &optional args)
93 (if (and args (symbolp (car args)))
94 (apply (car args) buffer (cdr args))
95 (let ((window (get-buffer-window buffer t)))
96 (if window
97 ;; If we have a window already, make it visible.
98 (let ((frame (window-frame window)))
99 (make-frame-visible frame)
100 (raise-frame frame)
101 window)
102 ;; If no window yet, make one in a new frame.
103 (let ((frame (make-frame (append args special-display-frame-alist))))
104 (set-window-buffer (frame-selected-window frame) buffer)
105 (set-window-dedicated-p (frame-selected-window frame) t)
106 (frame-selected-window frame))))))
8a9e86e6 107
924be53a
RS
108;; Handle delete-frame events from the X server.
109(defun handle-delete-frame (event)
110 (interactive "e")
111 (let ((frame (posn-window (event-start event)))
112 (i 0)
113 (tail (frame-list)))
114 (while tail
115 (and (frame-visible-p (car tail))
116 (not (eq (car tail) frame))
117 (setq i (1+ i)))
118 (setq tail (cdr tail)))
119 (if (> i 0)
120 (delete-frame frame t)
121 (kill-emacs))))
64c669bc 122\f
dc6d9681 123;;;; Arrangement of frames at startup
64c669bc
JB
124
125;;; 1) Load the window system startup file from the lisp library and read the
126;;; high-priority arguments (-q and the like). The window system startup
dc6d9681 127;;; file should create any frames specified in the window system defaults.
85557d7e 128;;;
dc6d9681 129;;; 2) If no frames have been opened, we open an initial text frame.
64c669bc
JB
130;;;
131;;; 3) Once the init file is done, we apply any newly set parameters
dc6d9681 132;;; in initial-frame-alist to the frame.
64c669bc 133
85557d7e 134;; These are now called explicitly at the proper times,
fc4d4afb
RS
135;; since that is easier to understand.
136;; Actually using hooks within Emacs is bad for future maintenance. --rms.
137;; (add-hook 'before-init-hook 'frame-initialize)
138;; (add-hook 'window-setup-hook 'frame-notice-user-settings)
64c669bc 139
dc6d9681
JB
140;;; If we create the initial frame, this is it.
141(defvar frame-initial-frame nil)
64c669bc 142
3f2f8c83
RS
143;; Record the parameters used in frame-initialize to make the initial frame.
144(defvar frame-initial-frame-alist)
145
791e09d8
RS
146(defvar frame-initial-geometry-arguments nil)
147
64c669bc 148;;; startup.el calls this function before loading the user's init
dc6d9681 149;;; file - if there is no frame with a minibuffer open now, create
64c669bc 150;;; one to display messages while loading the init file.
dc6d9681 151(defun frame-initialize ()
85557d7e 152
64c669bc
JB
153 ;; Are we actually running under a window system at all?
154 (if (and window-system (not noninteractive))
7eadab74 155 (progn
62642af9
RS
156 ;; Turn on special-display processing only if there's a window system.
157 (setq special-display-function 'special-display-popup-frame)
158
7eadab74
JB
159 ;; If there is no frame with a minibuffer besides the terminal
160 ;; frame, then we need to create the opening frame. Make sure
161 ;; it has a minibuffer, but let initial-frame-alist omit the
162 ;; minibuffer spec.
163 (or (delq terminal-frame (minibuffer-frame-list))
36fc9c9f 164 (progn
3f2f8c83
RS
165 (setq frame-initial-frame-alist
166 (append initial-frame-alist default-frame-alist))
a8bb5882
RS
167 ;; Record these with their default values
168 ;; if they don't have any values explicitly.
169 (or (assq 'vertical-scroll-bars frame-initial-frame-alist)
170 (setq frame-initial-frame-alist
171 (cons '(vertical-scroll-bars . t)
172 frame-initial-frame-alist)))
173 (or (assq 'horizontal-scroll-bars frame-initial-frame-alist)
174 (setq frame-initial-frame-alist
175 (cons '(horizontal-scroll-bars . t)
176 frame-initial-frame-alist)))
36fc9c9f
RS
177 (setq default-minibuffer-frame
178 (setq frame-initial-frame
6eb018ba 179 (make-frame initial-frame-alist)))
11281034
RS
180 ;; Delete any specifications for window geometry parameters
181 ;; so that we won't reapply them in frame-notice-user-settings.
182 ;; It would be wrong to reapply them then,
183 ;; because that would override explicit user resizing.
58bf6042 184 (setq initial-frame-alist
c2079f0a 185 (frame-remove-geometry-params initial-frame-alist))))
85557d7e 186 ;; At this point, we know that we have a frame open, so we
dc6d9681
JB
187 ;; can delete the terminal frame.
188 (delete-frame terminal-frame)
189 (setq terminal-frame nil))
85557d7e 190
0759c4d0
KH
191 ;; No, we're not running a window system. Use make-terminal-frame if
192 ;; we support that feature, otherwise arrange to cause errors.
dc6d9681 193 (setq frame-creation-function
0759c4d0
KH
194 (if (fboundp 'make-terminal-frame)
195 'make-terminal-frame
196 (function
197 (lambda (parameters)
198 (error
199 "Can't create multiple frames without a window system")))))))
85557d7e 200
7eadab74
JB
201;;; startup.el calls this function after loading the user's init
202;;; file. Now default-frame-alist and initial-frame-alist contain
203;;; information to which we must react; do what needs to be done.
dc6d9681 204(defun frame-notice-user-settings ()
7eadab74 205
2ffa6186 206 ;; Make menu-bar-mode and default-frame-alist consistent.
b65be6a8
KH
207 (if (boundp 'menu-bar-mode)
208 (let ((default (assq 'menu-bar-lines default-frame-alist)))
209 (if default
210 (setq menu-bar-mode (not (eq (cdr default) 0)))
211 (setq default-frame-alist
212 (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0))
213 default-frame-alist)))))
2ffa6186 214
7eadab74
JB
215 ;; Creating and deleting frames may shift the selected frame around,
216 ;; and thus the current buffer. Protect against that. We don't
217 ;; want to use save-excursion here, because that may also try to set
218 ;; the buffer of the selected window, which fails when the selected
219 ;; window is the minibuffer.
220 (let ((old-buffer (current-buffer)))
221
222 ;; If the initial frame is still around, apply initial-frame-alist
223 ;; and default-frame-alist to it.
224 (if (frame-live-p frame-initial-frame)
225
226 ;; The initial frame we create above always has a minibuffer.
227 ;; If the user wants to remove it, or make it a minibuffer-only
228 ;; frame, then we'll have to delete the current frame and make a
229 ;; new one; you can't remove or add a root window to/from an
230 ;; existing frame.
231 ;;
ec558adc
JB
232 ;; NOTE: default-frame-alist was nil when we created the
233 ;; existing frame. We need to explicitly include
234 ;; default-frame-alist in the parameters of the screen we
235 ;; create here, so that its new value, gleaned from the user's
236 ;; .emacs file, will be applied to the existing screen.
7eadab74
JB
237 (if (not (eq (cdr (or (assq 'minibuffer initial-frame-alist)
238 (assq 'minibuffer default-frame-alist)
239 '(minibuffer . t)))
240 t))
241 ;; Create the new frame.
b65e2f74
RS
242 (let (parms new)
243 ;; If the frame isn't visible yet, wait till it is.
244 ;; If the user has to position the window,
245 ;; Emacs doesn't know its real position until
246 ;; the frame is seen to be visible.
247 (while (not (cdr (assq 'visibility
248 (frame-parameters frame-initial-frame))))
249 (sleep-for 1))
06d7dff2
RS
250 (setq parms (frame-parameters frame-initial-frame))
251 ;; Get rid of `name' unless it was specified explicitly before.
252 (or (assq 'name frame-initial-frame-alist)
253 (setq parms (delq (assq 'name parms) parms)))
b65e2f74 254 (setq parms (append initial-frame-alist
6eb018ba 255 default-frame-alist
06d7dff2 256 parms
6eb018ba 257 nil))
c1e67401
RS
258 ;; Get rid of `reverse', because that was handled
259 ;; when we first made the frame.
260 (setq parms (cons '(reverse) (delq (assq 'reverse parms) parms)))
261 (if (assq 'height frame-initial-geometry-arguments)
5b66a32f 262 (setq parms (frame-delete-all 'height parms)))
c1e67401 263 (if (assq 'width frame-initial-geometry-arguments)
5b66a32f 264 (setq parms (frame-delete-all 'width parms)))
c1e67401 265 (if (assq 'left frame-initial-geometry-arguments)
5b66a32f 266 (setq parms (frame-delete-all 'left parms)))
c1e67401 267 (if (assq 'top frame-initial-geometry-arguments)
5b66a32f 268 (setq parms (frame-delete-all 'top parms)))
c1e67401
RS
269 (setq new
270 (make-frame
271 ;; Use the geometry args that created the existing
272 ;; frame, rather than the parms we get for it.
3ce3ff11
KH
273 (append frame-initial-geometry-arguments
274 '((user-size . t) (user-position . t))
275 parms)))
7eadab74
JB
276 ;; The initial frame, which we are about to delete, may be
277 ;; the only frame with a minibuffer. If it is, create a
278 ;; new one.
279 (or (delq frame-initial-frame (minibuffer-frame-list))
746bd265 280 (make-initial-minibuffer-frame nil))
7eadab74
JB
281
282 ;; If the initial frame is serving as a surrogate
283 ;; minibuffer frame for any frames, we need to wean them
284 ;; onto a new frame. The default-minibuffer-frame
285 ;; variable must be handled similarly.
286 (let ((users-of-initial
287 (filtered-frame-list
288 (function (lambda (frame)
289 (and (not (eq frame frame-initial-frame))
290 (eq (window-frame
291 (minibuffer-window frame))
292 frame-initial-frame)))))))
293 (if (or users-of-initial
294 (eq default-minibuffer-frame frame-initial-frame))
295
296 ;; Choose an appropriate frame. Prefer frames which
297 ;; are only minibuffers.
298 (let* ((new-surrogate
299 (car
300 (or (filtered-frame-list
301 (function
302 (lambda (frame)
303 (eq (cdr (assq 'minibuffer
304 (frame-parameters frame)))
305 'only))))
306 (minibuffer-frame-list))))
307 (new-minibuffer (minibuffer-window new-surrogate)))
308
309 (if (eq default-minibuffer-frame frame-initial-frame)
310 (setq default-minibuffer-frame new-surrogate))
311
312 ;; Wean the frames using frame-initial-frame as
313 ;; their minibuffer frame.
314 (mapcar
315 (function
316 (lambda (frame)
317 (modify-frame-parameters
318 frame (list (cons 'minibuffer new-minibuffer)))))
319 users-of-initial))))
ec558adc
JB
320
321 ;; Redirect events enqueued at this frame to the new frame.
322 ;; Is this a good idea?
7eadab74 323 (redirect-frame-focus frame-initial-frame new)
ec558adc 324
7eadab74 325 ;; Finally, get rid of the old frame.
0cffbbb9 326 (delete-frame frame-initial-frame t))
7eadab74
JB
327
328 ;; Otherwise, we don't need all that rigamarole; just apply
329 ;; the new parameters.
3f2f8c83
RS
330 (let (newparms allparms tail)
331 (setq allparms (append initial-frame-alist
332 default-frame-alist))
5b66a32f
RS
333 (if (assq 'height frame-initial-geometry-arguments)
334 (setq allparms (frame-delete-all 'height allparms)))
335 (if (assq 'width frame-initial-geometry-arguments)
336 (setq allparms (frame-delete-all 'width allparms)))
337 (if (assq 'left frame-initial-geometry-arguments)
338 (setq allparms (frame-delete-all 'left allparms)))
339 (if (assq 'top frame-initial-geometry-arguments)
340 (setq allparms (frame-delete-all 'top allparms)))
3f2f8c83
RS
341 (setq tail allparms)
342 ;; Find just the parms that have changed since we first
343 ;; made this frame. Those are the ones actually set by
344 ;; the init file. For those parms whose values we already knew
345 ;; (such as those spec'd by command line options)
346 ;; it is undesirable to specify the parm again
347 ;; once the user has seen the frame and been able to alter it
348 ;; manually.
349 (while tail
350 (let (newval oldval)
66de157d
RS
351 (setq oldval (assq (car (car tail))
352 frame-initial-frame-alist))
3f2f8c83 353 (setq newval (cdr (assq (car (car tail)) allparms)))
66de157d 354 (or (and oldval (eq (cdr oldval) newval))
3f2f8c83
RS
355 (setq newparms
356 (cons (cons (car (car tail)) newval) newparms))))
357 (setq tail (cdr tail)))
86e58378 358 (setq newparms (nreverse newparms))
3f2f8c83 359 (modify-frame-parameters frame-initial-frame
86e58378
RS
360 newparms)
361 (if (assq 'font newparms)
362 (frame-update-faces frame-initial-frame)))))
64c669bc 363
7eadab74
JB
364 ;; Restore the original buffer.
365 (set-buffer old-buffer)
366
367 ;; Make sure the initial frame can be GC'd if it is ever deleted.
d202f1f2
JB
368 ;; Make sure frame-notice-user-settings does nothing if called twice.
369 (setq frame-initial-frame nil)))
64c669bc 370
746bd265
KH
371(defun make-initial-minibuffer-frame (display)
372 (let ((parms (append minibuffer-frame-alist '((minibuffer . only)))))
373 (if display
374 (make-frame-on-display display parms)
375 (make-frame parms))))
376
5b66a32f
RS
377;; Delete from ALIST all elements whose car is KEY.
378;; Return the modified alist.
379(defun frame-delete-all (key alist)
9759578f 380 (setq alist (copy-sequence alist))
5b66a32f
RS
381 (let ((tail alist))
382 (while tail
383 (if (eq (car (car tail)) key)
384 (setq alist (delq (car tail) alist)))
385 (setq tail (cdr tail)))
386 alist))
64c669bc 387\f
7eadab74 388;;;; Creation of additional frames, and other frame miscellanea
dc6d9681 389
7eadab74 390;;; Return some frame other than the current frame, creating one if
eb8c3be9 391;;; necessary. Note that the minibuffer frame, if separate, is not
7eadab74 392;;; considered (see next-frame).
7253d8e0 393(defun get-other-frame ()
dc6d9681 394 (let ((s (if (equal (next-frame (selected-frame)) (selected-frame))
6eb018ba 395 (make-frame)
dc6d9681 396 (next-frame (selected-frame)))))
64c669bc
JB
397 s))
398
dc6d9681
JB
399(defun next-multiframe-window ()
400 "Select the next window, regardless of which frame it is on."
64c669bc
JB
401 (interactive)
402 (select-window (next-window (selected-window)
403 (> (minibuffer-depth) 0)
404 t)))
405
dc6d9681
JB
406(defun previous-multiframe-window ()
407 "Select the previous window, regardless of which frame it is on."
64c669bc
JB
408 (interactive)
409 (select-window (previous-window (selected-window)
410 (> (minibuffer-depth) 0)
411 t)))
412
6d73e337
RS
413(defun make-frame-on-display (display &optional parameters)
414 "Make a frame on display DISPLAY.
415The optional second argument PARAMETERS specifies additional frame parameters."
416 (interactive "sMake frame on display: ")
417 (make-frame (cons (cons 'display display) parameters)))
418
92e443b1 419;; Alias, kept temporarily.
31e1d920 420(defalias 'new-frame 'make-frame)
92e443b1 421(defun make-frame (&optional parameters)
dc6d9681 422 "Create a new frame, displaying the current buffer.
bc93c097 423
43a2e52c 424Optional argument PARAMETERS is an alist of parameters for the new
a105105a
RS
425frame. Specifically, PARAMETERS is a list of pairs, each having
426the form (NAME . VALUE).
427
428Here are some of the parameters allowed (not a complete list):
43a2e52c 429
eaa974e1 430\(name . STRING) - The frame should be named STRING.
43a2e52c 431
eaa974e1 432\(height . NUMBER) - The frame should be NUMBER text lines high. If
43a2e52c
JB
433 this parameter is present, the width parameter must also be
434 given.
435
eaa974e1 436\(width . NUMBER) - The frame should be NUMBER characters in width.
43a2e52c
JB
437 If this parameter is present, the height parameter must also
438 be given.
439
eaa974e1 440\(minibuffer . t) - the frame should have a minibuffer
0ca90494 441\(minibuffer . nil) - the frame should have no minibuffer
eaa974e1 442\(minibuffer . only) - the frame should contain only a minibuffer
8dbd5c78 443\(minibuffer . WINDOW) - the frame should use WINDOW as its minibuffer window."
64c669bc 444 (interactive)
4133ab49
RS
445 (let ((nframe))
446 (run-hooks 'before-make-frame-hook)
447 (setq nframe (funcall frame-creation-function parameters))
448 (run-hooks 'after-make-frame-hook)
449 nframe))
64c669bc 450
7eadab74
JB
451(defun filtered-frame-list (predicate)
452 "Return a list of all live frames which satisfy PREDICATE."
453 (let ((frames (frame-list))
454 good-frames)
455 (while (consp frames)
456 (if (funcall predicate (car frames))
457 (setq good-frames (cons (car frames) good-frames)))
458 (setq frames (cdr frames)))
459 good-frames))
460
461(defun minibuffer-frame-list ()
462 "Return a list of all frames with their own minibuffers."
463 (filtered-frame-list
464 (function (lambda (frame)
465 (eq frame (window-frame (minibuffer-window frame)))))))
466
58bf6042
JB
467(defun frame-remove-geometry-params (param-list)
468 "Return the parameter list PARAM-LIST, but with geometry specs removed.
469This deletes all bindings in PARAM-LIST for `top', `left', `width',
791e09d8 470`height', `user-size' and `user-position' parameters.
58bf6042
JB
471Emacs uses this to avoid overriding explicit moves and resizings from
472the user during startup."
473 (setq param-list (cons nil param-list))
474 (let ((tail param-list))
475 (while (consp (cdr tail))
476 (if (and (consp (car (cdr tail)))
791e09d8
RS
477 (memq (car (car (cdr tail)))
478 '(height width top left user-position user-size)))
479 (progn
480 (setq frame-initial-geometry-arguments
481 (cons (car (cdr tail)) frame-initial-geometry-arguments))
482 (setcdr tail (cdr (cdr tail))))
58bf6042 483 (setq tail (cdr tail)))))
e11c3dc2
KH
484 (setq frame-initial-geometry-arguments
485 (nreverse frame-initial-geometry-arguments))
58bf6042
JB
486 (cdr param-list))
487
488
ceab6935 489(defun other-frame (arg)
a569dbc3 490 "Select the ARG'th different visible frame, and raise it.
ceab6935
RM
491All frames are arranged in a cyclic order.
492This command selects the frame ARG steps away in that order.
493A negative ARG moves in the opposite order."
494 (interactive "p")
495 (let ((frame (selected-frame)))
496 (while (> arg 0)
a569dbc3
RM
497 (setq frame (next-frame frame))
498 (while (not (eq (frame-visible-p frame) t))
499 (setq frame (next-frame frame)))
500 (setq arg (1- arg)))
ceab6935 501 (while (< arg 0)
a569dbc3
RM
502 (setq frame (previous-frame frame))
503 (while (not (eq (frame-visible-p frame) t))
504 (setq frame (previous-frame frame)))
915cfd1f 505 (setq arg (1+ arg)))
ceab6935 506 (raise-frame frame)
699213bc 507 (select-frame frame)
2b88f7a0 508 (set-mouse-position (selected-frame) (1- (frame-width)) 0)
b65be6a8
KH
509 (if (fboundp 'unfocus-frame)
510 (unfocus-frame))))
64c669bc 511\f
dc6d9681
JB
512;;;; Frame configurations
513
514(defun current-frame-configuration ()
515 "Return a list describing the positions and states of all frames.
376a7584
JB
516Its car is `frame-configuration'.
517Each element of the cdr is a list of the form (FRAME ALIST WINDOW-CONFIG),
518where
519 FRAME is a frame object,
520 ALIST is an association list specifying some of FRAME's parameters, and
521 WINDOW-CONFIG is a window configuration object for FRAME."
522 (cons 'frame-configuration
523 (mapcar (function
524 (lambda (frame)
525 (list frame
526 (frame-parameters frame)
527 (current-window-configuration frame))))
528 (frame-list))))
dc6d9681 529
68cd265f 530(defun set-frame-configuration (configuration &optional nodelete)
dc6d9681
JB
531 "Restore the frames to the state described by CONFIGURATION.
532Each frame listed in CONFIGURATION has its position, size, window
68cd265f 533configuration, and other parameters set as specified in CONFIGURATION.
a78db71c
RS
534Ordinarily, this function deletes all existing frames not
535listed in CONFIGURATION. But if optional second argument NODELETE
5da841d2 536is given and non-nil, the unwanted frames are iconified instead."
376a7584
JB
537 (or (frame-configuration-p configuration)
538 (signal 'wrong-type-argument
539 (list 'frame-configuration-p configuration)))
540 (let ((config-alist (cdr configuration))
541 frames-to-delete)
06b1a5ef 542 (mapcar (function
dc6d9681 543 (lambda (frame)
376a7584 544 (let ((parameters (assq frame config-alist)))
06b1a5ef
JB
545 (if parameters
546 (progn
98e9d14b
JB
547 (modify-frame-parameters
548 frame
85557d7e 549 ;; Since we can't set a frame's minibuffer status,
98e9d14b
JB
550 ;; we might as well omit the parameter altogether.
551 (let* ((parms (nth 1 parameters))
552 (mini (assq 'minibuffer parms)))
553 (if mini (setq parms (delq mini parms)))
554 parms))
06b1a5ef 555 (set-window-configuration (nth 2 parameters)))
dc6d9681
JB
556 (setq frames-to-delete (cons frame frames-to-delete))))))
557 (frame-list))
a78db71c 558 (if nodelete
5da841d2
RS
559 ;; Note: making frames invisible here was tried
560 ;; but led to some strange behavior--each time the frame
561 ;; was made visible again, the window manager asked afresh
562 ;; for where to put it.
563 (mapcar 'iconify-frame frames-to-delete)
a78db71c 564 (mapcar 'delete-frame frames-to-delete))))
64c669bc 565\f
dc6d9681
JB
566;;;; Convenience functions for accessing and interactively changing
567;;;; frame parameters.
64c669bc 568
151bdc83 569(defun frame-height (&optional frame)
dc6d9681
JB
570 "Return number of lines available for display on FRAME.
571If FRAME is omitted, describe the currently selected frame."
151bdc83 572 (cdr (assq 'height (frame-parameters frame))))
dc6d9681
JB
573
574(defun frame-width (&optional frame)
575 "Return number of columns available for display on FRAME.
576If FRAME is omitted, describe the currently selected frame."
151bdc83 577 (cdr (assq 'width (frame-parameters frame))))
dc6d9681 578
64c669bc 579(defun set-default-font (font-name)
7eadab74
JB
580 "Set the font of the selected frame to FONT.
581When called interactively, prompt for the name of the font to use."
64c669bc 582 (interactive "sFont name: ")
dc6d9681 583 (modify-frame-parameters (selected-frame)
4033f46b
RS
584 (list (cons 'font font-name)))
585 ;; Update faces that want a bold or italic version of the default font.
586 (frame-update-faces (selected-frame)))
64c669bc 587
8290babd 588(defun set-background-color (color-name)
7eadab74
JB
589 "Set the background color of the selected frame to COLOR.
590When called interactively, prompt for the name of the color to use."
64c669bc 591 (interactive "sColor: ")
dc6d9681 592 (modify-frame-parameters (selected-frame)
1787c22a
RS
593 (list (cons 'background-color color-name)))
594 (frame-update-face-colors (selected-frame)))
64c669bc 595
8290babd 596(defun set-foreground-color (color-name)
7eadab74
JB
597 "Set the foreground color of the selected frame to COLOR.
598When called interactively, prompt for the name of the color to use."
64c669bc 599 (interactive "sColor: ")
dc6d9681 600 (modify-frame-parameters (selected-frame)
1787c22a
RS
601 (list (cons 'foreground-color color-name)))
602 (frame-update-face-colors (selected-frame)))
64c669bc
JB
603
604(defun set-cursor-color (color-name)
7eadab74
JB
605 "Set the text cursor color of the selected frame to COLOR.
606When called interactively, prompt for the name of the color to use."
64c669bc 607 (interactive "sColor: ")
dc6d9681 608 (modify-frame-parameters (selected-frame)
7eadab74 609 (list (cons 'cursor-color color-name))))
64c669bc 610
eaa974e1 611(defun set-mouse-color (color-name)
7eadab74
JB
612 "Set the color of the mouse pointer of the selected frame to COLOR.
613When called interactively, prompt for the name of the color to use."
64c669bc 614 (interactive "sColor: ")
dc6d9681 615 (modify-frame-parameters (selected-frame)
7eadab74
JB
616 (list (cons 'mouse-color color-name))))
617
eaa974e1
JB
618(defun set-border-color (color-name)
619 "Set the color of the border of the selected frame to COLOR.
620When called interactively, prompt for the name of the color to use."
621 (interactive "sColor: ")
622 (modify-frame-parameters (selected-frame)
623 (list (cons 'border-color color-name))))
624
625(defun auto-raise-mode (arg)
7eadab74 626 "Toggle whether or not the selected frame should auto-raise.
79e6ae33
RS
627With arg, turn auto-raise mode on if and only if arg is positive.
628Note that this controls Emacs's own auto-raise feature.
629Some window managers allow you to enable auto-raise for certain windows.
630You can use that for Emacs windows if you wish, but if you do,
631that is beyond the control of Emacs and this command has no effect on it."
7eadab74
JB
632 (interactive "P")
633 (if (null arg)
634 (setq arg
635 (if (cdr (assq 'auto-raise (frame-parameters (selected-frame))))
636 -1 1)))
dc6d9681 637 (modify-frame-parameters (selected-frame)
7eadab74
JB
638 (list (cons 'auto-raise (> arg 0)))))
639
eaa974e1 640(defun auto-lower-mode (arg)
7eadab74 641 "Toggle whether or not the selected frame should auto-lower.
79e6ae33
RS
642With arg, turn auto-lower mode on if and only if arg is positive.
643Note that this controls Emacs's own auto-lower feature.
644Some window managers allow you to enable auto-lower for certain windows.
645You can use that for Emacs windows if you wish, but if you do,
646that is beyond the control of Emacs and this command has no effect on it."
7eadab74
JB
647 (interactive "P")
648 (if (null arg)
649 (setq arg
650 (if (cdr (assq 'auto-lower (frame-parameters (selected-frame))))
651 -1 1)))
dc6d9681 652 (modify-frame-parameters (selected-frame)
7eadab74
JB
653 (list (cons 'auto-lower (> arg 0)))))
654
7e85c6b5 655(defun toggle-scroll-bar (arg)
0ca90494
JB
656 "Toggle whether or not the selected frame has vertical scroll bars.
657With arg, turn vertical scroll bars on if and only if arg is positive."
7eadab74
JB
658 (interactive "P")
659 (if (null arg)
660 (setq arg
0ca90494 661 (if (cdr (assq 'vertical-scroll-bars
7eadab74
JB
662 (frame-parameters (selected-frame))))
663 -1 1)))
dc6d9681 664 (modify-frame-parameters (selected-frame)
0ca90494 665 (list (cons 'vertical-scroll-bars (> arg 0)))))
7eadab74 666
8290babd 667(defun toggle-horizontal-scroll-bar (arg)
0ca90494
JB
668 "Toggle whether or not the selected frame has horizontal scroll bars.
669With arg, turn horizontal scroll bars on if and only if arg is positive.
670Horizontal scroll bars aren't implemented yet."
7eadab74 671 (interactive "P")
8290babd 672 (error "Horizontal scroll bars aren't implemented yet"))
64c669bc 673
64c669bc 674\f
dc6d9681 675;;;; Aliases for backward compatibility with Emacs 18.
31e1d920
ER
676(defalias 'screen-height 'frame-height)
677(defalias 'screen-width 'frame-width)
9e2b097b
JB
678
679(defun set-screen-width (cols &optional pretend)
680 "Obsolete function to change the size of the screen to COLS columns.\n\
681Optional second arg non-nil means that redisplay should use COLS columns\n\
682but that the idea of the actual width of the frame should not be changed.\n\
683This function is provided only for compatibility with Emacs 18; new code\n\
fe668515 684should use `set-frame-width instead'."
9e2b097b
JB
685 (set-frame-width (selected-frame) cols pretend))
686
687(defun set-screen-height (lines &optional pretend)
688 "Obsolete function to change the height of the screen to LINES lines.\n\
689Optional second arg non-nil means that redisplay should use LINES lines\n\
690but that the idea of the actual height of the screen should not be changed.\n\
691This function is provided only for compatibility with Emacs 18; new code\n\
fe668515 692should use `set-frame-width' instead."
9e2b097b
JB
693 (set-frame-height (selected-frame) lines pretend))
694
695(make-obsolete 'screen-height 'frame-height)
696(make-obsolete 'screen-width 'frame-width)
697(make-obsolete 'set-screen-width 'set-frame-width)
698(make-obsolete 'set-screen-height 'set-frame-height)
dc6d9681
JB
699
700\f
64c669bc 701;;;; Key bindings
daa37602 702(defvar ctl-x-5-map (make-sparse-keymap)
dc6d9681 703 "Keymap for frame commands.")
31e1d920 704(defalias 'ctl-x-5-prefix ctl-x-5-map)
daa37602 705(define-key ctl-x-map "5" 'ctl-x-5-prefix)
64c669bc 706
6eb018ba 707(define-key ctl-x-5-map "2" 'make-frame)
dc6d9681 708(define-key ctl-x-5-map "0" 'delete-frame)
ceab6935 709(define-key ctl-x-5-map "o" 'other-frame)
49116ac0 710
dc6d9681 711(provide 'frame)
c88ab9ce 712
dc6d9681 713;;; frame.el ends here