(syms_of_keymap): Add missing 2nd arg to Fcons.
[emacs.git] / lisp / frame.el
blob8faa376569f3f2bec0a63d60a60134e064a54d35
1 ;;; frame.el --- multi-frame management independent of window systems.
3 ;;;; Copyright (C) 1993 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: internal
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
22 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 ;;; Code:
26 (defvar frame-creation-function nil
27 "Window-system dependent function to call to create a new frame.
28 The window system startup file should set this to its frame creation
29 function, which should take an alist of parameters as its argument.")
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.
34 (defvar initial-frame-alist '((minibuffer . t))
35 "Alist of frame parameters for creating the initial X window frame.
36 You can set this in your `.emacs' file; for example,
37 (setq initial-frame-alist '((top . 1) (left . 1) (width . 80) (height . 55)))
38 If the value calls for a frame without a minibuffer, and you do not create a
39 minibuffer frame on your own, one is created according to
40 `minibuffer-frame-alist'.
41 Parameters specified here supersede the values given in
42 `default-frame-alist'.")
44 (defvar minibuffer-frame-alist '((width . 80) (height . 2))
45 "Alist of frame parameters for initially creating a minibuffer frame.
46 You can set this in your `.emacs' file; for example,
47 (setq minibuffer-frame-alist
48 '((top . 1) (left . 1) (width . 80) (height . 2)))
49 Parameters specified here supersede the values given in
50 `default-frame-alist'.")
52 (defvar pop-up-frame-alist nil
53 "Alist of frame parameters used when creating pop-up frames.
54 Pop-up frames are used for completions, help, and the like.
55 This variable can be set in your init file, like this:
56 (setq pop-up-frame-alist '((width . 80) (height . 20)))
57 These supercede the values given in `default-frame-alist'.")
59 (setq pop-up-frame-function
60 (function (lambda ()
61 (new-frame pop-up-frame-alist))))
64 ;;;; Arrangement of frames at startup
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
68 ;;; file should create any frames specified in the window system defaults.
69 ;;;
70 ;;; 2) If no frames have been opened, we open an initial text frame.
71 ;;;
72 ;;; 3) Once the init file is done, we apply any newly set parameters
73 ;;; in initial-frame-alist to the frame.
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)
81 ;;; If we create the initial frame, this is it.
82 (defvar frame-initial-frame nil)
84 ;;; startup.el calls this function before loading the user's init
85 ;;; file - if there is no frame with a minibuffer open now, create
86 ;;; one to display messages while loading the init file.
87 (defun frame-initialize ()
89 ;; Are we actually running under a window system at all?
90 (if (and window-system (not noninteractive))
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))
97 (progn
98 (setq default-minibuffer-frame
99 (setq frame-initial-frame
100 (new-frame initial-frame-alist)))
101 ;; Handle `reverse' as a parameter.
102 (if (cdr (or (assq 'reverse initial-frame-alist)
103 (assq 'reverse default-frame-alist)
104 (cons nil
105 (x-get-resource "reverseVideo" "Reversevideo"))))
106 (let ((params (frame-parameters frame-initial-frame)))
107 (modify-frame-parameters
108 frame-initial-frame
109 (list (cons 'foreground-color (cdr (assq 'background-color params)))
110 (cons 'background-color (cdr (assq 'foreground-color params)))
111 (cons 'mouse-color (cdr (assq 'background-color params)))
112 (cons 'cursor-color (cdr (assq 'background-color params)))
113 (cons 'border-color (cdr (assq 'background-color params)))))))))
115 ;; At this point, we know that we have a frame open, so we
116 ;; can delete the terminal frame.
117 (delete-frame terminal-frame)
118 (setq terminal-frame nil))
120 ;; No, we're not running a window system. Arrange to cause errors.
121 (setq frame-creation-function
122 (function
123 (lambda (parameters)
124 (error
125 "Can't create multiple frames without a window system"))))))
127 ;;; startup.el calls this function after loading the user's init
128 ;;; file. Now default-frame-alist and initial-frame-alist contain
129 ;;; information to which we must react; do what needs to be done.
130 (defun frame-notice-user-settings ()
132 ;; Creating and deleting frames may shift the selected frame around,
133 ;; and thus the current buffer. Protect against that. We don't
134 ;; want to use save-excursion here, because that may also try to set
135 ;; the buffer of the selected window, which fails when the selected
136 ;; window is the minibuffer.
137 (let ((old-buffer (current-buffer)))
139 ;; If the initial frame is still around, apply initial-frame-alist
140 ;; and default-frame-alist to it.
141 (if (frame-live-p frame-initial-frame)
143 ;; The initial frame we create above always has a minibuffer.
144 ;; If the user wants to remove it, or make it a minibuffer-only
145 ;; frame, then we'll have to delete the current frame and make a
146 ;; new one; you can't remove or add a root window to/from an
147 ;; existing frame.
149 ;; NOTE: default-frame-alist was nil when we created the
150 ;; existing frame. We need to explicitly include
151 ;; default-frame-alist in the parameters of the screen we
152 ;; create here, so that its new value, gleaned from the user's
153 ;; .emacs file, will be applied to the existing screen.
154 (if (not (eq (cdr (or (assq 'minibuffer initial-frame-alist)
155 (assq 'minibuffer default-frame-alist)
156 '(minibuffer . t)))
158 ;; Create the new frame.
159 (let ((new
160 (new-frame
161 (append initial-frame-alist
162 default-frame-alist
163 (frame-parameters frame-initial-frame)))))
165 ;; The initial frame, which we are about to delete, may be
166 ;; the only frame with a minibuffer. If it is, create a
167 ;; new one.
168 (or (delq frame-initial-frame (minibuffer-frame-list))
169 (new-frame (append minibuffer-frame-alist
170 '((minibuffer . only)))))
172 ;; If the initial frame is serving as a surrogate
173 ;; minibuffer frame for any frames, we need to wean them
174 ;; onto a new frame. The default-minibuffer-frame
175 ;; variable must be handled similarly.
176 (let ((users-of-initial
177 (filtered-frame-list
178 (function (lambda (frame)
179 (and (not (eq frame frame-initial-frame))
180 (eq (window-frame
181 (minibuffer-window frame))
182 frame-initial-frame)))))))
183 (if (or users-of-initial
184 (eq default-minibuffer-frame frame-initial-frame))
186 ;; Choose an appropriate frame. Prefer frames which
187 ;; are only minibuffers.
188 (let* ((new-surrogate
189 (car
190 (or (filtered-frame-list
191 (function
192 (lambda (frame)
193 (eq (cdr (assq 'minibuffer
194 (frame-parameters frame)))
195 'only))))
196 (minibuffer-frame-list))))
197 (new-minibuffer (minibuffer-window new-surrogate)))
199 (if (eq default-minibuffer-frame frame-initial-frame)
200 (setq default-minibuffer-frame new-surrogate))
202 ;; Wean the frames using frame-initial-frame as
203 ;; their minibuffer frame.
204 (mapcar
205 (function
206 (lambda (frame)
207 (modify-frame-parameters
208 frame (list (cons 'minibuffer new-minibuffer)))))
209 users-of-initial))))
211 ;; Redirect events enqueued at this frame to the new frame.
212 ;; Is this a good idea?
213 (redirect-frame-focus frame-initial-frame new)
215 ;; Finally, get rid of the old frame.
216 (delete-frame frame-initial-frame))
218 ;; Otherwise, we don't need all that rigamarole; just apply
219 ;; the new parameters.
220 (modify-frame-parameters frame-initial-frame
221 (append initial-frame-alist
222 default-frame-alist))))
224 ;; Restore the original buffer.
225 (set-buffer old-buffer)
227 ;; Make sure the initial frame can be GC'd if it is ever deleted.
228 ;; Make sure frame-notice-user-settings does nothing if called twice.
229 (setq frame-initial-frame nil)))
232 ;;;; Creation of additional frames, and other frame miscellanea
234 ;;; Return some frame other than the current frame, creating one if
235 ;;; neccessary. Note that the minibuffer frame, if separate, is not
236 ;;; considered (see next-frame).
237 (defun get-other-frame ()
238 (let ((s (if (equal (next-frame (selected-frame)) (selected-frame))
239 (new-frame)
240 (next-frame (selected-frame)))))
243 (defun next-multiframe-window ()
244 "Select the next window, regardless of which frame it is on."
245 (interactive)
246 (select-window (next-window (selected-window)
247 (> (minibuffer-depth) 0)
248 t)))
250 (defun previous-multiframe-window ()
251 "Select the previous window, regardless of which frame it is on."
252 (interactive)
253 (select-window (previous-window (selected-window)
254 (> (minibuffer-depth) 0)
255 t)))
257 ;; Alias, kept temporarily.
258 (defalias 'new-frame 'make-frame)
259 (defun make-frame (&optional parameters)
260 "Create a new frame, displaying the current buffer.
262 Optional argument PARAMETERS is an alist of parameters for the new
263 frame. Specifically, PARAMETERS is a list of pairs, each having one
264 of the following forms:
266 \(name . STRING) - The frame should be named STRING.
268 \(height . NUMBER) - The frame should be NUMBER text lines high. If
269 this parameter is present, the width parameter must also be
270 given.
272 \(width . NUMBER) - The frame should be NUMBER characters in width.
273 If this parameter is present, the height parameter must also
274 be given.
276 \(minibuffer . t) - the frame should have a minibuffer
277 \(minibuffer . nil) - the frame should have no minibuffer
278 \(minibuffer . only) - the frame should contain only a minibuffer
279 \(minibuffer . WINDOW) - the frame should use WINDOW as its minibuffer window.
281 The documentation for the function `x-create-frame' describes
282 additional frame parameters that Emacs recognizes for X window frames."
283 (interactive)
284 (funcall frame-creation-function parameters))
286 (defun filtered-frame-list (predicate)
287 "Return a list of all live frames which satisfy PREDICATE."
288 (let ((frames (frame-list))
289 good-frames)
290 (while (consp frames)
291 (if (funcall predicate (car frames))
292 (setq good-frames (cons (car frames) good-frames)))
293 (setq frames (cdr frames)))
294 good-frames))
296 (defun minibuffer-frame-list ()
297 "Return a list of all frames with their own minibuffers."
298 (filtered-frame-list
299 (function (lambda (frame)
300 (eq frame (window-frame (minibuffer-window frame)))))))
303 ;;;; Frame configurations
305 (defun current-frame-configuration ()
306 "Return a list describing the positions and states of all frames.
307 Its car is `frame-configuration'.
308 Each element of the cdr is a list of the form (FRAME ALIST WINDOW-CONFIG),
309 where
310 FRAME is a frame object,
311 ALIST is an association list specifying some of FRAME's parameters, and
312 WINDOW-CONFIG is a window configuration object for FRAME."
313 (cons 'frame-configuration
314 (mapcar (function
315 (lambda (frame)
316 (list frame
317 (frame-parameters frame)
318 (current-window-configuration frame))))
319 (frame-list))))
321 (defun set-frame-configuration (configuration)
322 "Restore the frames to the state described by CONFIGURATION.
323 Each frame listed in CONFIGURATION has its position, size, window
324 configuration, and other parameters set as specified in CONFIGURATION."
325 (or (frame-configuration-p configuration)
326 (signal 'wrong-type-argument
327 (list 'frame-configuration-p configuration)))
328 (let ((config-alist (cdr configuration))
329 frames-to-delete)
330 (mapcar (function
331 (lambda (frame)
332 (let ((parameters (assq frame config-alist)))
333 (if parameters
334 (progn
335 (modify-frame-parameters frame (nth 1 parameters))
336 (set-window-configuration (nth 2 parameters)))
337 (setq frames-to-delete (cons frame frames-to-delete))))))
338 (frame-list))
339 (mapcar 'delete-frame frames-to-delete)))
341 (defun frame-configuration-p (object)
342 "Return non-nil if OBJECT seems to be a frame configuration.
343 Any list whose car is `frame-configuration' is assumed to be a frame
344 configuration."
345 (and (consp object)
346 (eq (car object) 'frame-configuration)))
349 ;;;; Convenience functions for accessing and interactively changing
350 ;;;; frame parameters.
352 (defun frame-height (&optional frame)
353 "Return number of lines available for display on FRAME.
354 If FRAME is omitted, describe the currently selected frame."
355 (cdr (assq 'height (frame-parameters frame))))
357 (defun frame-width (&optional frame)
358 "Return number of columns available for display on FRAME.
359 If FRAME is omitted, describe the currently selected frame."
360 (cdr (assq 'width (frame-parameters frame))))
362 (defun set-default-font (font-name)
363 "Set the font of the selected frame to FONT.
364 When called interactively, prompt for the name of the font to use."
365 (interactive "sFont name: ")
366 (modify-frame-parameters (selected-frame)
367 (list (cons 'font font-name))))
369 (defun set-background-color (color-name)
370 "Set the background color of the selected frame to COLOR.
371 When called interactively, prompt for the name of the color to use."
372 (interactive "sColor: ")
373 (modify-frame-parameters (selected-frame)
374 (list (cons 'background-color color-name))))
376 (defun set-foreground-color (color-name)
377 "Set the foreground color of the selected frame to COLOR.
378 When called interactively, prompt for the name of the color to use."
379 (interactive "sColor: ")
380 (modify-frame-parameters (selected-frame)
381 (list (cons 'foreground-color color-name))))
383 (defun set-cursor-color (color-name)
384 "Set the text cursor color of the selected frame to COLOR.
385 When called interactively, prompt for the name of the color to use."
386 (interactive "sColor: ")
387 (modify-frame-parameters (selected-frame)
388 (list (cons 'cursor-color color-name))))
390 (defun set-mouse-color (color-name)
391 "Set the color of the mouse pointer of the selected frame to COLOR.
392 When called interactively, prompt for the name of the color to use."
393 (interactive "sColor: ")
394 (modify-frame-parameters (selected-frame)
395 (list (cons 'mouse-color color-name))))
397 (defun set-border-color (color-name)
398 "Set the color of the border of the selected frame to COLOR.
399 When called interactively, prompt for the name of the color to use."
400 (interactive "sColor: ")
401 (modify-frame-parameters (selected-frame)
402 (list (cons 'border-color color-name))))
404 (defun auto-raise-mode (arg)
405 "Toggle whether or not the selected frame should auto-raise.
406 With arg, turn auto-raise mode on if and only if arg is positive."
407 (interactive "P")
408 (if (null arg)
409 (setq arg
410 (if (cdr (assq 'auto-raise (frame-parameters (selected-frame))))
411 -1 1)))
412 (modify-frame-parameters (selected-frame)
413 (list (cons 'auto-raise (> arg 0)))))
415 (defun auto-lower-mode (arg)
416 "Toggle whether or not the selected frame should auto-lower.
417 With arg, turn auto-lower mode on if and only if arg is positive."
418 (interactive "P")
419 (if (null arg)
420 (setq arg
421 (if (cdr (assq 'auto-lower (frame-parameters (selected-frame))))
422 -1 1)))
423 (modify-frame-parameters (selected-frame)
424 (list (cons 'auto-lower (> arg 0)))))
426 (defun toggle-scroll-bar (arg)
427 "Toggle whether or not the selected frame has vertical scroll bars.
428 With arg, turn vertical scroll bars on if and only if arg is positive."
429 (interactive "P")
430 (if (null arg)
431 (setq arg
432 (if (cdr (assq 'vertical-scroll-bars
433 (frame-parameters (selected-frame))))
434 -1 1)))
435 (modify-frame-parameters (selected-frame)
436 (list (cons 'vertical-scroll-bars (> arg 0)))))
438 (defun toggle-horizontal-scroll-bar (arg)
439 "Toggle whether or not the selected frame has horizontal scroll bars.
440 With arg, turn horizontal scroll bars on if and only if arg is positive.
441 Horizontal scroll bars aren't implemented yet."
442 (interactive "P")
443 (error "Horizontal scroll bars aren't implemented yet"))
446 ;;;; Aliases for backward compatibility with Emacs 18.
447 (defalias 'screen-height 'frame-height)
448 (defalias 'screen-width 'frame-width)
450 (defun set-screen-width (cols &optional pretend)
451 "Obsolete function to change the size of the screen to COLS columns.\n\
452 Optional second arg non-nil means that redisplay should use COLS columns\n\
453 but that the idea of the actual width of the frame should not be changed.\n\
454 This function is provided only for compatibility with Emacs 18; new code\n\
455 should use `set-frame-width instead'."
456 (set-frame-width (selected-frame) cols pretend))
458 (defun set-screen-height (lines &optional pretend)
459 "Obsolete function to change the height of the screen to LINES lines.\n\
460 Optional second arg non-nil means that redisplay should use LINES lines\n\
461 but that the idea of the actual height of the screen should not be changed.\n\
462 This function is provided only for compatibility with Emacs 18; new code\n\
463 should use `set-frame-width' instead."
464 (set-frame-height (selected-frame) lines pretend))
466 (make-obsolete 'screen-height 'frame-height)
467 (make-obsolete 'screen-width 'frame-width)
468 (make-obsolete 'set-screen-width 'set-frame-width)
469 (make-obsolete 'set-screen-height 'set-frame-height)
472 ;;;; Key bindings
473 (defvar ctl-x-5-map (make-sparse-keymap)
474 "Keymap for frame commands.")
475 (defalias 'ctl-x-5-prefix ctl-x-5-map)
476 (define-key ctl-x-map "5" 'ctl-x-5-prefix)
478 (define-key ctl-x-5-map "2" 'new-frame)
479 (define-key ctl-x-5-map "0" 'delete-frame)
481 (provide 'frame)
483 ;;; frame.el ends here