* frame.c (Fredirect_frame_focus): Allow redirection of focus of
[bpt/emacs.git] / lisp / frame.el
CommitLineData
dc6d9681 1;;; frame.el --- multi-frame management independent of window systems.
c88ab9ce 2
c0f86408 3;;;; Copyright (C) 1993 Free Software Foundation, Inc.
3a801d0c 4
fc68affa 5;; Maintainer: FSF
fd7fa35a 6;; Keywords: internal
fc68affa 7
64c669bc
JB
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
de49a6d3 12;;; the Free Software Foundation; either version 2, or (at your option)
64c669bc
JB
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
22;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
fc68affa
ER
24;;; Code:
25
dc6d9681
JB
26(defvar frame-creation-function nil
27 "Window-system dependent function to call to create a new frame.
28The window system startup file should set this to its frame creation
64c669bc
JB
29function, which should take an alist of parameters as its argument.")
30
7eadab74
JB
31;;; The initial value given here for this must ask for a minibuffer.
32;;; There must always exist a frame with a minibuffer, and after we
33;;; delete the terminal frame, this will be the only frame.
9e2b097b 34(defvar initial-frame-alist '((minibuffer . t))
eaa974e1
JB
35 "Alist of frame parameters for creating the initial X window frame.
36You can set this in your `.emacs' file; for example,
dc6d9681 37 (setq initial-frame-alist '((top . 1) (left . 1) (width . 80) (height . 55)))
eaa974e1
JB
38If the value calls for a frame without a minibuffer, and you do not create a
39minibuffer frame on your own, one is created according to
7eadab74 40`minibuffer-frame-alist'.
eaa974e1
JB
41Parameters specified here supersede the values given in
42`default-frame-alist'.")
64c669bc 43
7eadab74 44(defvar minibuffer-frame-alist '((width . 80) (height . 2))
eaa974e1
JB
45 "Alist of frame parameters for initially creating a minibuffer frame.
46You can set this in your `.emacs' file; for example,
dc6d9681 47 (setq minibuffer-frame-alist
7eadab74 48 '((top . 1) (left . 1) (width . 80) (height . 2)))
eaa974e1
JB
49Parameters specified here supersede the values given in
50`default-frame-alist'.")
64c669bc 51
dc6d9681 52(defvar pop-up-frame-alist nil
eaa974e1 53 "Alist of frame parameters used when creating pop-up frames.
dc6d9681 54Pop-up frames are used for completions, help, and the like.
64c669bc 55This variable can be set in your init file, like this:
dc6d9681 56 (setq pop-up-frame-alist '((width . 80) (height . 20)))
eb8c3be9 57These supersede the values given in `default-frame-alist'.")
64c669bc 58
dc6d9681 59(setq pop-up-frame-function
64c669bc 60 (function (lambda ()
dc6d9681 61 (new-frame pop-up-frame-alist))))
64c669bc
JB
62
63\f
dc6d9681 64;;;; Arrangement of frames at startup
64c669bc
JB
65
66;;; 1) Load the window system startup file from the lisp library and read the
67;;; high-priority arguments (-q and the like). The window system startup
dc6d9681 68;;; file should create any frames specified in the window system defaults.
64c669bc 69;;;
dc6d9681 70;;; 2) If no frames have been opened, we open an initial text frame.
64c669bc
JB
71;;;
72;;; 3) Once the init file is done, we apply any newly set parameters
dc6d9681 73;;; in initial-frame-alist to the frame.
64c669bc 74
fc4d4afb
RS
75;; These are now called explicitly at the proper times,
76;; since that is easier to understand.
77;; Actually using hooks within Emacs is bad for future maintenance. --rms.
78;; (add-hook 'before-init-hook 'frame-initialize)
79;; (add-hook 'window-setup-hook 'frame-notice-user-settings)
64c669bc 80
dc6d9681
JB
81;;; If we create the initial frame, this is it.
82(defvar frame-initial-frame nil)
64c669bc
JB
83
84;;; startup.el calls this function before loading the user's init
dc6d9681 85;;; file - if there is no frame with a minibuffer open now, create
64c669bc 86;;; one to display messages while loading the init file.
dc6d9681 87(defun frame-initialize ()
64c669bc
JB
88
89 ;; Are we actually running under a window system at all?
90 (if (and window-system (not noninteractive))
7eadab74
JB
91 (progn
92 ;; If there is no frame with a minibuffer besides the terminal
93 ;; frame, then we need to create the opening frame. Make sure
94 ;; it has a minibuffer, but let initial-frame-alist omit the
95 ;; minibuffer spec.
96 (or (delq terminal-frame (minibuffer-frame-list))
36fc9c9f
RS
97 (progn
98 (setq default-minibuffer-frame
99 (setq frame-initial-frame
100 (new-frame initial-frame-alist)))
11281034
RS
101 ;; Delete any specifications for window geometry parameters
102 ;; so that we won't reapply them in frame-notice-user-settings.
103 ;; It would be wrong to reapply them then,
104 ;; because that would override explicit user resizing.
5e901379
JB
105 ;; Remember that they may occur more than once.
106 (let ((tail initial-frame-alist))
107 (while (consp tail)
108 (if (and (consp (car tail))
109 (memq (car (car tail)) '(height width top left)))
110 (setq initial-frame-alist
3008ffb6
JB
111 (delq tail initial-frame-alist)))
112 (setq tail (cdr tail))))
36fc9c9f
RS
113 ;; Handle `reverse' as a parameter.
114 (if (cdr (or (assq 'reverse initial-frame-alist)
88046be2 115 (assq 'reverse default-frame-alist)))
36fc9c9f
RS
116 (let ((params (frame-parameters frame-initial-frame)))
117 (modify-frame-parameters
118 frame-initial-frame
949d4ac7
RS
119 ;; Must set cursor-color after background color.
120 ;; So put it first.
118c9082
JB
121 (list (cons 'cursor-color
122 (cdr (assq 'background-color params)))
123 (cons 'foreground-color
124 (cdr (assq 'background-color params)))
125 (cons 'background-color
126 (cdr (assq 'foreground-color params)))
127 (cons 'mouse-color
128 (cdr (assq 'background-color params)))
129 (cons 'border-color
130 (cdr (assq 'background-color params)))))))))
7eadab74 131
dc6d9681
JB
132 ;; At this point, we know that we have a frame open, so we
133 ;; can delete the terminal frame.
134 (delete-frame terminal-frame)
135 (setq terminal-frame nil))
64c669bc
JB
136
137 ;; No, we're not running a window system. Arrange to cause errors.
dc6d9681 138 (setq frame-creation-function
9198945a
MB
139 (function
140 (lambda (parameters)
141 (error
8290babd 142 "Can't create multiple frames without a window system"))))))
64c669bc 143
7eadab74
JB
144;;; startup.el calls this function after loading the user's init
145;;; file. Now default-frame-alist and initial-frame-alist contain
146;;; information to which we must react; do what needs to be done.
dc6d9681 147(defun frame-notice-user-settings ()
7eadab74
JB
148
149 ;; Creating and deleting frames may shift the selected frame around,
150 ;; and thus the current buffer. Protect against that. We don't
151 ;; want to use save-excursion here, because that may also try to set
152 ;; the buffer of the selected window, which fails when the selected
153 ;; window is the minibuffer.
154 (let ((old-buffer (current-buffer)))
155
156 ;; If the initial frame is still around, apply initial-frame-alist
157 ;; and default-frame-alist to it.
158 (if (frame-live-p frame-initial-frame)
159
160 ;; The initial frame we create above always has a minibuffer.
161 ;; If the user wants to remove it, or make it a minibuffer-only
162 ;; frame, then we'll have to delete the current frame and make a
163 ;; new one; you can't remove or add a root window to/from an
164 ;; existing frame.
165 ;;
ec558adc
JB
166 ;; NOTE: default-frame-alist was nil when we created the
167 ;; existing frame. We need to explicitly include
168 ;; default-frame-alist in the parameters of the screen we
169 ;; create here, so that its new value, gleaned from the user's
170 ;; .emacs file, will be applied to the existing screen.
7eadab74
JB
171 (if (not (eq (cdr (or (assq 'minibuffer initial-frame-alist)
172 (assq 'minibuffer default-frame-alist)
173 '(minibuffer . t)))
174 t))
175 ;; Create the new frame.
176 (let ((new
177 (new-frame
178 (append initial-frame-alist
179 default-frame-alist
180 (frame-parameters frame-initial-frame)))))
181
182 ;; The initial frame, which we are about to delete, may be
183 ;; the only frame with a minibuffer. If it is, create a
184 ;; new one.
185 (or (delq frame-initial-frame (minibuffer-frame-list))
186 (new-frame (append minibuffer-frame-alist
187 '((minibuffer . only)))))
188
189 ;; If the initial frame is serving as a surrogate
190 ;; minibuffer frame for any frames, we need to wean them
191 ;; onto a new frame. The default-minibuffer-frame
192 ;; variable must be handled similarly.
193 (let ((users-of-initial
194 (filtered-frame-list
195 (function (lambda (frame)
196 (and (not (eq frame frame-initial-frame))
197 (eq (window-frame
198 (minibuffer-window frame))
199 frame-initial-frame)))))))
200 (if (or users-of-initial
201 (eq default-minibuffer-frame frame-initial-frame))
202
203 ;; Choose an appropriate frame. Prefer frames which
204 ;; are only minibuffers.
205 (let* ((new-surrogate
206 (car
207 (or (filtered-frame-list
208 (function
209 (lambda (frame)
210 (eq (cdr (assq 'minibuffer
211 (frame-parameters frame)))
212 'only))))
213 (minibuffer-frame-list))))
214 (new-minibuffer (minibuffer-window new-surrogate)))
215
216 (if (eq default-minibuffer-frame frame-initial-frame)
217 (setq default-minibuffer-frame new-surrogate))
218
219 ;; Wean the frames using frame-initial-frame as
220 ;; their minibuffer frame.
221 (mapcar
222 (function
223 (lambda (frame)
224 (modify-frame-parameters
225 frame (list (cons 'minibuffer new-minibuffer)))))
226 users-of-initial))))
ec558adc
JB
227
228 ;; Redirect events enqueued at this frame to the new frame.
229 ;; Is this a good idea?
7eadab74 230 (redirect-frame-focus frame-initial-frame new)
ec558adc 231
7eadab74 232 ;; Finally, get rid of the old frame.
dc6d9681 233 (delete-frame frame-initial-frame))
7eadab74
JB
234
235 ;; Otherwise, we don't need all that rigamarole; just apply
236 ;; the new parameters.
dc6d9681 237 (modify-frame-parameters frame-initial-frame
ec558adc 238 (append initial-frame-alist
7eadab74 239 default-frame-alist))))
64c669bc 240
7eadab74
JB
241 ;; Restore the original buffer.
242 (set-buffer old-buffer)
243
244 ;; Make sure the initial frame can be GC'd if it is ever deleted.
d202f1f2
JB
245 ;; Make sure frame-notice-user-settings does nothing if called twice.
246 (setq frame-initial-frame nil)))
64c669bc
JB
247
248\f
7eadab74 249;;;; Creation of additional frames, and other frame miscellanea
dc6d9681 250
7eadab74 251;;; Return some frame other than the current frame, creating one if
eb8c3be9 252;;; necessary. Note that the minibuffer frame, if separate, is not
7eadab74 253;;; considered (see next-frame).
7253d8e0 254(defun get-other-frame ()
dc6d9681
JB
255 (let ((s (if (equal (next-frame (selected-frame)) (selected-frame))
256 (new-frame)
257 (next-frame (selected-frame)))))
64c669bc
JB
258 s))
259
dc6d9681
JB
260(defun next-multiframe-window ()
261 "Select the next window, regardless of which frame it is on."
64c669bc
JB
262 (interactive)
263 (select-window (next-window (selected-window)
264 (> (minibuffer-depth) 0)
265 t)))
266
dc6d9681
JB
267(defun previous-multiframe-window ()
268 "Select the previous window, regardless of which frame it is on."
64c669bc
JB
269 (interactive)
270 (select-window (previous-window (selected-window)
271 (> (minibuffer-depth) 0)
272 t)))
273
92e443b1 274;; Alias, kept temporarily.
31e1d920 275(defalias 'new-frame 'make-frame)
92e443b1 276(defun make-frame (&optional parameters)
dc6d9681 277 "Create a new frame, displaying the current buffer.
bc93c097 278
43a2e52c 279Optional argument PARAMETERS is an alist of parameters for the new
dc6d9681 280frame. Specifically, PARAMETERS is a list of pairs, each having one
43a2e52c
JB
281of the following forms:
282
eaa974e1 283\(name . STRING) - The frame should be named STRING.
43a2e52c 284
eaa974e1 285\(height . NUMBER) - The frame should be NUMBER text lines high. If
43a2e52c
JB
286 this parameter is present, the width parameter must also be
287 given.
288
eaa974e1 289\(width . NUMBER) - The frame should be NUMBER characters in width.
43a2e52c
JB
290 If this parameter is present, the height parameter must also
291 be given.
292
eaa974e1 293\(minibuffer . t) - the frame should have a minibuffer
0ca90494 294\(minibuffer . nil) - the frame should have no minibuffer
eaa974e1
JB
295\(minibuffer . only) - the frame should contain only a minibuffer
296\(minibuffer . WINDOW) - the frame should use WINDOW as its minibuffer window.
43a2e52c 297
eaa974e1
JB
298The documentation for the function `x-create-frame' describes
299additional frame parameters that Emacs recognizes for X window frames."
64c669bc 300 (interactive)
dc6d9681 301 (funcall frame-creation-function parameters))
64c669bc 302
7eadab74
JB
303(defun filtered-frame-list (predicate)
304 "Return a list of all live frames which satisfy PREDICATE."
305 (let ((frames (frame-list))
306 good-frames)
307 (while (consp frames)
308 (if (funcall predicate (car frames))
309 (setq good-frames (cons (car frames) good-frames)))
310 (setq frames (cdr frames)))
311 good-frames))
312
313(defun minibuffer-frame-list ()
314 "Return a list of all frames with their own minibuffers."
315 (filtered-frame-list
316 (function (lambda (frame)
317 (eq frame (window-frame (minibuffer-window frame)))))))
318
64c669bc 319\f
dc6d9681
JB
320;;;; Frame configurations
321
322(defun current-frame-configuration ()
323 "Return a list describing the positions and states of all frames.
376a7584
JB
324Its car is `frame-configuration'.
325Each element of the cdr is a list of the form (FRAME ALIST WINDOW-CONFIG),
326where
327 FRAME is a frame object,
328 ALIST is an association list specifying some of FRAME's parameters, and
329 WINDOW-CONFIG is a window configuration object for FRAME."
330 (cons 'frame-configuration
331 (mapcar (function
332 (lambda (frame)
333 (list frame
334 (frame-parameters frame)
335 (current-window-configuration frame))))
336 (frame-list))))
dc6d9681
JB
337
338(defun set-frame-configuration (configuration)
339 "Restore the frames to the state described by CONFIGURATION.
340Each frame listed in CONFIGURATION has its position, size, window
06b1a5ef 341configuration, and other parameters set as specified in CONFIGURATION."
376a7584
JB
342 (or (frame-configuration-p configuration)
343 (signal 'wrong-type-argument
344 (list 'frame-configuration-p configuration)))
345 (let ((config-alist (cdr configuration))
346 frames-to-delete)
06b1a5ef 347 (mapcar (function
dc6d9681 348 (lambda (frame)
376a7584 349 (let ((parameters (assq frame config-alist)))
06b1a5ef
JB
350 (if parameters
351 (progn
98e9d14b
JB
352 (modify-frame-parameters
353 frame
354 ;; Since we can't set a frame's minibuffer status,
355 ;; we might as well omit the parameter altogether.
356 (let* ((parms (nth 1 parameters))
357 (mini (assq 'minibuffer parms)))
358 (if mini (setq parms (delq mini parms)))
359 parms))
06b1a5ef 360 (set-window-configuration (nth 2 parameters)))
dc6d9681
JB
361 (setq frames-to-delete (cons frame frames-to-delete))))))
362 (frame-list))
363 (mapcar 'delete-frame frames-to-delete)))
64c669bc 364
376a7584
JB
365(defun frame-configuration-p (object)
366 "Return non-nil if OBJECT seems to be a frame configuration.
367Any list whose car is `frame-configuration' is assumed to be a frame
368configuration."
369 (and (consp object)
370 (eq (car object) 'frame-configuration)))
371
64c669bc 372\f
dc6d9681
JB
373;;;; Convenience functions for accessing and interactively changing
374;;;; frame parameters.
64c669bc 375
151bdc83 376(defun frame-height (&optional frame)
dc6d9681
JB
377 "Return number of lines available for display on FRAME.
378If FRAME is omitted, describe the currently selected frame."
151bdc83 379 (cdr (assq 'height (frame-parameters frame))))
dc6d9681
JB
380
381(defun frame-width (&optional frame)
382 "Return number of columns available for display on FRAME.
383If FRAME is omitted, describe the currently selected frame."
151bdc83 384 (cdr (assq 'width (frame-parameters frame))))
dc6d9681 385
64c669bc 386(defun set-default-font (font-name)
7eadab74
JB
387 "Set the font of the selected frame to FONT.
388When called interactively, prompt for the name of the font to use."
64c669bc 389 (interactive "sFont name: ")
dc6d9681 390 (modify-frame-parameters (selected-frame)
7eadab74 391 (list (cons 'font font-name))))
64c669bc 392
8290babd 393(defun set-background-color (color-name)
7eadab74
JB
394 "Set the background color of the selected frame to COLOR.
395When called interactively, prompt for the name of the color to use."
64c669bc 396 (interactive "sColor: ")
dc6d9681 397 (modify-frame-parameters (selected-frame)
7eadab74 398 (list (cons 'background-color color-name))))
64c669bc 399
8290babd 400(defun set-foreground-color (color-name)
7eadab74
JB
401 "Set the foreground color of the selected frame to COLOR.
402When called interactively, prompt for the name of the color to use."
64c669bc 403 (interactive "sColor: ")
dc6d9681 404 (modify-frame-parameters (selected-frame)
7eadab74 405 (list (cons 'foreground-color color-name))))
64c669bc
JB
406
407(defun set-cursor-color (color-name)
7eadab74
JB
408 "Set the text cursor color of the selected frame to COLOR.
409When called interactively, prompt for the name of the color to use."
64c669bc 410 (interactive "sColor: ")
dc6d9681 411 (modify-frame-parameters (selected-frame)
7eadab74 412 (list (cons 'cursor-color color-name))))
64c669bc 413
eaa974e1 414(defun set-mouse-color (color-name)
7eadab74
JB
415 "Set the color of the mouse pointer of the selected frame to COLOR.
416When called interactively, prompt for the name of the color to use."
64c669bc 417 (interactive "sColor: ")
dc6d9681 418 (modify-frame-parameters (selected-frame)
7eadab74
JB
419 (list (cons 'mouse-color color-name))))
420
eaa974e1
JB
421(defun set-border-color (color-name)
422 "Set the color of the border of the selected frame to COLOR.
423When called interactively, prompt for the name of the color to use."
424 (interactive "sColor: ")
425 (modify-frame-parameters (selected-frame)
426 (list (cons 'border-color color-name))))
427
428(defun auto-raise-mode (arg)
7eadab74
JB
429 "Toggle whether or not the selected frame should auto-raise.
430With arg, turn auto-raise mode on if and only if arg is positive."
431 (interactive "P")
432 (if (null arg)
433 (setq arg
434 (if (cdr (assq 'auto-raise (frame-parameters (selected-frame))))
435 -1 1)))
dc6d9681 436 (modify-frame-parameters (selected-frame)
7eadab74
JB
437 (list (cons 'auto-raise (> arg 0)))))
438
eaa974e1 439(defun auto-lower-mode (arg)
7eadab74
JB
440 "Toggle whether or not the selected frame should auto-lower.
441With arg, turn auto-lower mode on if and only if arg is positive."
442 (interactive "P")
443 (if (null arg)
444 (setq arg
445 (if (cdr (assq 'auto-lower (frame-parameters (selected-frame))))
446 -1 1)))
dc6d9681 447 (modify-frame-parameters (selected-frame)
7eadab74
JB
448 (list (cons 'auto-lower (> arg 0)))))
449
7e85c6b5 450(defun toggle-scroll-bar (arg)
0ca90494
JB
451 "Toggle whether or not the selected frame has vertical scroll bars.
452With arg, turn vertical scroll bars on if and only if arg is positive."
7eadab74
JB
453 (interactive "P")
454 (if (null arg)
455 (setq arg
0ca90494 456 (if (cdr (assq 'vertical-scroll-bars
7eadab74
JB
457 (frame-parameters (selected-frame))))
458 -1 1)))
dc6d9681 459 (modify-frame-parameters (selected-frame)
0ca90494 460 (list (cons 'vertical-scroll-bars (> arg 0)))))
7eadab74 461
8290babd 462(defun toggle-horizontal-scroll-bar (arg)
0ca90494
JB
463 "Toggle whether or not the selected frame has horizontal scroll bars.
464With arg, turn horizontal scroll bars on if and only if arg is positive.
465Horizontal scroll bars aren't implemented yet."
7eadab74 466 (interactive "P")
8290babd 467 (error "Horizontal scroll bars aren't implemented yet"))
64c669bc 468
64c669bc 469\f
dc6d9681 470;;;; Aliases for backward compatibility with Emacs 18.
31e1d920
ER
471(defalias 'screen-height 'frame-height)
472(defalias 'screen-width 'frame-width)
9e2b097b
JB
473
474(defun set-screen-width (cols &optional pretend)
475 "Obsolete function to change the size of the screen to COLS columns.\n\
476Optional second arg non-nil means that redisplay should use COLS columns\n\
477but that the idea of the actual width of the frame should not be changed.\n\
478This function is provided only for compatibility with Emacs 18; new code\n\
fe668515 479should use `set-frame-width instead'."
9e2b097b
JB
480 (set-frame-width (selected-frame) cols pretend))
481
482(defun set-screen-height (lines &optional pretend)
483 "Obsolete function to change the height of the screen to LINES lines.\n\
484Optional second arg non-nil means that redisplay should use LINES lines\n\
485but that the idea of the actual height of the screen should not be changed.\n\
486This function is provided only for compatibility with Emacs 18; new code\n\
fe668515 487should use `set-frame-width' instead."
9e2b097b
JB
488 (set-frame-height (selected-frame) lines pretend))
489
490(make-obsolete 'screen-height 'frame-height)
491(make-obsolete 'screen-width 'frame-width)
492(make-obsolete 'set-screen-width 'set-frame-width)
493(make-obsolete 'set-screen-height 'set-frame-height)
dc6d9681
JB
494
495\f
64c669bc 496;;;; Key bindings
daa37602 497(defvar ctl-x-5-map (make-sparse-keymap)
dc6d9681 498 "Keymap for frame commands.")
31e1d920 499(defalias 'ctl-x-5-prefix ctl-x-5-map)
daa37602 500(define-key ctl-x-map "5" 'ctl-x-5-prefix)
64c669bc 501
dc6d9681
JB
502(define-key ctl-x-5-map "2" 'new-frame)
503(define-key ctl-x-5-map "0" 'delete-frame)
49116ac0 504
dc6d9681 505(provide 'frame)
c88ab9ce 506
dc6d9681 507;;; frame.el ends here