1 ;;; frame.el --- multi-frame management independent of window systems.
3 ;;;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
8 ;;; This file is part of GNU Emacs.
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.
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.
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.
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 supersede the values given in `default-frame-alist'.")
59 (setq pop-up-frame-function
61 (make-frame pop-up-frame-alist))))
63 (defvar special-display-frame-alist
64 '((height . 14) (width . 80) (unsplittable . t))
65 "*Alist of frame parameters used when creating special frames.
66 Special frames are used for buffers whose names are in
67 `special-display-buffer-names' and for buffers whose names match
68 one of the regular expressions in `special-display-regexps'.
69 This variable can be set in your init file, like this:
70 (setq special-display-frame-alist '((width . 80) (height . 20)))
71 These supersede the values given in `default-frame-alist'.")
73 ;; Display BUFFER in its own frame, reusing an existing window if any.
74 ;; Return the window chosen.
75 ;; Currently we do not insist on selecting the window within its frame.
76 (defun special-display-popup-frame (buffer)
77 (let ((window (get-buffer-window buffer t)))
79 ;; If we have a window already, make it visible.
80 (let ((frame (window-frame window)))
81 (make-frame-visible frame)
84 ;; If no window yet, make one in a new frame.
85 (let ((frame (make-frame special-display-frame-alist)))
86 (set-window-buffer (frame-selected-window frame) buffer)
87 (set-window-dedicated-p (frame-selected-window frame) t)
88 (frame-selected-window frame)))))
90 (setq special-display-function 'special-display-popup-frame)
92 ;;;; Arrangement of frames at startup
94 ;;; 1) Load the window system startup file from the lisp library and read the
95 ;;; high-priority arguments (-q and the like). The window system startup
96 ;;; file should create any frames specified in the window system defaults.
98 ;;; 2) If no frames have been opened, we open an initial text frame.
100 ;;; 3) Once the init file is done, we apply any newly set parameters
101 ;;; in initial-frame-alist to the frame.
103 ;; These are now called explicitly at the proper times,
104 ;; since that is easier to understand.
105 ;; Actually using hooks within Emacs is bad for future maintenance. --rms.
106 ;; (add-hook 'before-init-hook 'frame-initialize)
107 ;; (add-hook 'window-setup-hook 'frame-notice-user-settings)
109 ;;; If we create the initial frame, this is it.
110 (defvar frame-initial-frame nil)
112 ;; Record the parameters used in frame-initialize to make the initial frame.
113 (defvar frame-initial-frame-alist)
115 (defvar frame-initial-geometry-arguments nil)
117 ;;; startup.el calls this function before loading the user's init
118 ;;; file - if there is no frame with a minibuffer open now, create
119 ;;; one to display messages while loading the init file.
120 (defun frame-initialize ()
122 ;; Are we actually running under a window system at all?
123 (if (and window-system (not noninteractive))
125 ;; If there is no frame with a minibuffer besides the terminal
126 ;; frame, then we need to create the opening frame. Make sure
127 ;; it has a minibuffer, but let initial-frame-alist omit the
129 (or (delq terminal-frame (minibuffer-frame-list))
131 (setq frame-initial-frame-alist
132 (append initial-frame-alist default-frame-alist))
133 (setq default-minibuffer-frame
134 (setq frame-initial-frame
135 (make-frame initial-frame-alist)))
136 ;; Delete any specifications for window geometry parameters
137 ;; so that we won't reapply them in frame-notice-user-settings.
138 ;; It would be wrong to reapply them then,
139 ;; because that would override explicit user resizing.
140 (setq initial-frame-alist
141 (frame-remove-geometry-params initial-frame-alist))))
142 ;; At this point, we know that we have a frame open, so we
143 ;; can delete the terminal frame.
144 (delete-frame terminal-frame)
145 (setq terminal-frame nil))
147 ;; No, we're not running a window system. Arrange to cause errors.
148 (setq frame-creation-function
152 "Can't create multiple frames without a window system"))))))
154 ;;; startup.el calls this function after loading the user's init
155 ;;; file. Now default-frame-alist and initial-frame-alist contain
156 ;;; information to which we must react; do what needs to be done.
157 (defun frame-notice-user-settings ()
159 ;; Creating and deleting frames may shift the selected frame around,
160 ;; and thus the current buffer. Protect against that. We don't
161 ;; want to use save-excursion here, because that may also try to set
162 ;; the buffer of the selected window, which fails when the selected
163 ;; window is the minibuffer.
164 (let ((old-buffer (current-buffer)))
166 ;; If the initial frame is still around, apply initial-frame-alist
167 ;; and default-frame-alist to it.
168 (if (frame-live-p frame-initial-frame)
170 ;; The initial frame we create above always has a minibuffer.
171 ;; If the user wants to remove it, or make it a minibuffer-only
172 ;; frame, then we'll have to delete the current frame and make a
173 ;; new one; you can't remove or add a root window to/from an
176 ;; NOTE: default-frame-alist was nil when we created the
177 ;; existing frame. We need to explicitly include
178 ;; default-frame-alist in the parameters of the screen we
179 ;; create here, so that its new value, gleaned from the user's
180 ;; .emacs file, will be applied to the existing screen.
181 (if (not (eq (cdr (or (assq 'minibuffer initial-frame-alist)
182 (assq 'minibuffer default-frame-alist)
185 ;; Create the new frame.
187 ;; If the frame isn't visible yet, wait till it is.
188 ;; If the user has to position the window,
189 ;; Emacs doesn't know its real position until
190 ;; the frame is seen to be visible.
191 (while (not (cdr (assq 'visibility
192 (frame-parameters frame-initial-frame))))
194 (setq parms (append initial-frame-alist
196 (frame-parameters frame-initial-frame)
198 ;; Get rid of `reverse', because that was handled
199 ;; when we first made the frame.
200 (setq parms (cons '(reverse) (delq (assq 'reverse parms) parms)))
201 (if (assq 'height frame-initial-geometry-arguments)
202 (setq parms (delq (assq 'height parms) parms)))
203 (if (assq 'width frame-initial-geometry-arguments)
204 (setq parms (delq (assq 'width parms) parms)))
205 (if (assq 'left frame-initial-geometry-arguments)
206 (setq parms (delq (assq 'left parms) parms)))
207 (if (assq 'top frame-initial-geometry-arguments)
208 (setq parms (delq (assq 'top parms) parms)))
211 ;; Use the geometry args that created the existing
212 ;; frame, rather than the parms we get for it.
213 (append frame-initial-geometry-arguments parms)))
214 ;; The initial frame, which we are about to delete, may be
215 ;; the only frame with a minibuffer. If it is, create a
217 (or (delq frame-initial-frame (minibuffer-frame-list))
218 (make-frame (append minibuffer-frame-alist
219 '((minibuffer . only)))))
221 ;; If the initial frame is serving as a surrogate
222 ;; minibuffer frame for any frames, we need to wean them
223 ;; onto a new frame. The default-minibuffer-frame
224 ;; variable must be handled similarly.
225 (let ((users-of-initial
227 (function (lambda (frame)
228 (and (not (eq frame frame-initial-frame))
230 (minibuffer-window frame))
231 frame-initial-frame)))))))
232 (if (or users-of-initial
233 (eq default-minibuffer-frame frame-initial-frame))
235 ;; Choose an appropriate frame. Prefer frames which
236 ;; are only minibuffers.
237 (let* ((new-surrogate
239 (or (filtered-frame-list
242 (eq (cdr (assq 'minibuffer
243 (frame-parameters frame)))
245 (minibuffer-frame-list))))
246 (new-minibuffer (minibuffer-window new-surrogate)))
248 (if (eq default-minibuffer-frame frame-initial-frame)
249 (setq default-minibuffer-frame new-surrogate))
251 ;; Wean the frames using frame-initial-frame as
252 ;; their minibuffer frame.
256 (modify-frame-parameters
257 frame (list (cons 'minibuffer new-minibuffer)))))
260 ;; Redirect events enqueued at this frame to the new frame.
261 ;; Is this a good idea?
262 (redirect-frame-focus frame-initial-frame new)
264 ;; Finally, get rid of the old frame.
265 (delete-frame frame-initial-frame t))
267 ;; Otherwise, we don't need all that rigamarole; just apply
268 ;; the new parameters.
269 (let (newparms allparms tail)
270 (setq allparms (append initial-frame-alist
271 default-frame-alist))
273 ;; Find just the parms that have changed since we first
274 ;; made this frame. Those are the ones actually set by
275 ;; the init file. For those parms whose values we already knew
276 ;; (such as those spec'd by command line options)
277 ;; it is undesirable to specify the parm again
278 ;; once the user has seen the frame and been able to alter it
282 (setq oldval (cdr (assq (car (car tail))
283 frame-initial-frame-alist)))
284 (setq newval (cdr (assq (car (car tail)) allparms)))
285 (or (eq oldval newval)
287 (cons (cons (car (car tail)) newval) newparms))))
288 (setq tail (cdr tail)))
289 (modify-frame-parameters frame-initial-frame
290 (nreverse newparms)))))
292 ;; Restore the original buffer.
293 (set-buffer old-buffer)
295 ;; Make sure the initial frame can be GC'd if it is ever deleted.
296 ;; Make sure frame-notice-user-settings does nothing if called twice.
297 (setq frame-initial-frame nil)))
300 ;;;; Creation of additional frames, and other frame miscellanea
302 ;;; Return some frame other than the current frame, creating one if
303 ;;; necessary. Note that the minibuffer frame, if separate, is not
304 ;;; considered (see next-frame).
305 (defun get-other-frame ()
306 (let ((s (if (equal (next-frame (selected-frame)) (selected-frame))
308 (next-frame (selected-frame)))))
311 (defun next-multiframe-window ()
312 "Select the next window, regardless of which frame it is on."
314 (select-window (next-window (selected-window)
315 (> (minibuffer-depth) 0)
318 (defun previous-multiframe-window ()
319 "Select the previous window, regardless of which frame it is on."
321 (select-window (previous-window (selected-window)
322 (> (minibuffer-depth) 0)
325 ;; Alias, kept temporarily.
326 (defalias 'new-frame 'make-frame)
327 (defun make-frame (&optional parameters)
328 "Create a new frame, displaying the current buffer.
330 Optional argument PARAMETERS is an alist of parameters for the new
331 frame. Specifically, PARAMETERS is a list of pairs, each having one
332 of the following forms:
334 \(name . STRING) - The frame should be named STRING.
336 \(height . NUMBER) - The frame should be NUMBER text lines high. If
337 this parameter is present, the width parameter must also be
340 \(width . NUMBER) - The frame should be NUMBER characters in width.
341 If this parameter is present, the height parameter must also
344 \(minibuffer . t) - the frame should have a minibuffer
345 \(minibuffer . nil) - the frame should have no minibuffer
346 \(minibuffer . only) - the frame should contain only a minibuffer
347 \(minibuffer . WINDOW) - the frame should use WINDOW as its minibuffer window.
349 The documentation for the function `x-create-frame' describes
350 additional frame parameters that Emacs recognizes for X window frames."
353 (run-hooks 'before-make-frame-hook)
354 (setq nframe (funcall frame-creation-function parameters))
355 (run-hooks 'after-make-frame-hook)
358 (defun filtered-frame-list (predicate)
359 "Return a list of all live frames which satisfy PREDICATE."
360 (let ((frames (frame-list))
362 (while (consp frames)
363 (if (funcall predicate (car frames))
364 (setq good-frames (cons (car frames) good-frames)))
365 (setq frames (cdr frames)))
368 (defun minibuffer-frame-list ()
369 "Return a list of all frames with their own minibuffers."
371 (function (lambda (frame)
372 (eq frame (window-frame (minibuffer-window frame)))))))
374 (defun frame-remove-geometry-params (param-list)
375 "Return the parameter list PARAM-LIST, but with geometry specs removed.
376 This deletes all bindings in PARAM-LIST for `top', `left', `width',
377 `height', `user-size' and `user-position' parameters.
378 Emacs uses this to avoid overriding explicit moves and resizings from
379 the user during startup."
380 (setq param-list (cons nil param-list))
381 (let ((tail param-list))
382 (while (consp (cdr tail))
383 (if (and (consp (car (cdr tail)))
384 (memq (car (car (cdr tail)))
385 '(height width top left user-position user-size)))
387 (setq frame-initial-geometry-arguments
388 (cons (car (cdr tail)) frame-initial-geometry-arguments))
389 (setcdr tail (cdr (cdr tail))))
390 (setq tail (cdr tail)))))
394 (defun other-frame (arg)
395 "Select the ARG'th different visible frame, and raise it.
396 All frames are arranged in a cyclic order.
397 This command selects the frame ARG steps away in that order.
398 A negative ARG moves in the opposite order."
400 (let ((frame (selected-frame)))
402 (setq frame (next-frame frame))
403 (while (not (eq (frame-visible-p frame) t))
404 (setq frame (next-frame frame)))
407 (setq frame (previous-frame frame))
408 (while (not (eq (frame-visible-p frame) t))
409 (setq frame (previous-frame frame)))
413 (set-mouse-position (selected-frame) (1- (frame-width)) 0)
416 ;;;; Frame configurations
418 (defun current-frame-configuration ()
419 "Return a list describing the positions and states of all frames.
420 Its car is `frame-configuration'.
421 Each element of the cdr is a list of the form (FRAME ALIST WINDOW-CONFIG),
423 FRAME is a frame object,
424 ALIST is an association list specifying some of FRAME's parameters, and
425 WINDOW-CONFIG is a window configuration object for FRAME."
426 (cons 'frame-configuration
430 (frame-parameters frame)
431 (current-window-configuration frame))))
434 (defun set-frame-configuration (configuration &optional nodelete)
435 "Restore the frames to the state described by CONFIGURATION.
436 Each frame listed in CONFIGURATION has its position, size, window
437 configuration, and other parameters set as specified in CONFIGURATION.
438 Ordinarily, this function deletes all existing frames not
439 listed in CONFIGURATION. But if optional second argument NODELETE
440 is given and non-nil, the unwanted frames are iconified instead."
441 (or (frame-configuration-p configuration)
442 (signal 'wrong-type-argument
443 (list 'frame-configuration-p configuration)))
444 (let ((config-alist (cdr configuration))
448 (let ((parameters (assq frame config-alist)))
451 (modify-frame-parameters
453 ;; Since we can't set a frame's minibuffer status,
454 ;; we might as well omit the parameter altogether.
455 (let* ((parms (nth 1 parameters))
456 (mini (assq 'minibuffer parms)))
457 (if mini (setq parms (delq mini parms)))
459 (set-window-configuration (nth 2 parameters)))
460 (setq frames-to-delete (cons frame frames-to-delete))))))
463 ;; Note: making frames invisible here was tried
464 ;; but led to some strange behavior--each time the frame
465 ;; was made visible again, the window manager asked afresh
466 ;; for where to put it.
467 (mapcar 'iconify-frame frames-to-delete)
468 (mapcar 'delete-frame frames-to-delete))))
470 (defun frame-configuration-p (object)
471 "Return non-nil if OBJECT seems to be a frame configuration.
472 Any list whose car is `frame-configuration' is assumed to be a frame
475 (eq (car object) 'frame-configuration)))
478 ;;;; Convenience functions for accessing and interactively changing
479 ;;;; frame parameters.
481 (defun frame-height (&optional frame)
482 "Return number of lines available for display on FRAME.
483 If FRAME is omitted, describe the currently selected frame."
484 (cdr (assq 'height (frame-parameters frame))))
486 (defun frame-width (&optional frame)
487 "Return number of columns available for display on FRAME.
488 If FRAME is omitted, describe the currently selected frame."
489 (cdr (assq 'width (frame-parameters frame))))
491 (defun set-default-font (font-name)
492 "Set the font of the selected frame to FONT.
493 When called interactively, prompt for the name of the font to use."
494 (interactive "sFont name: ")
495 (modify-frame-parameters (selected-frame)
496 (list (cons 'font font-name)))
497 ;; Update faces that want a bold or italic version of the default font.
498 (frame-update-faces (selected-frame)))
500 (defun set-background-color (color-name)
501 "Set the background color of the selected frame to COLOR.
502 When called interactively, prompt for the name of the color to use."
503 (interactive "sColor: ")
504 (modify-frame-parameters (selected-frame)
505 (list (cons 'background-color color-name))))
507 (defun set-foreground-color (color-name)
508 "Set the foreground color of the selected frame to COLOR.
509 When called interactively, prompt for the name of the color to use."
510 (interactive "sColor: ")
511 (modify-frame-parameters (selected-frame)
512 (list (cons 'foreground-color color-name))))
514 (defun set-cursor-color (color-name)
515 "Set the text cursor color of the selected frame to COLOR.
516 When called interactively, prompt for the name of the color to use."
517 (interactive "sColor: ")
518 (modify-frame-parameters (selected-frame)
519 (list (cons 'cursor-color color-name))))
521 (defun set-mouse-color (color-name)
522 "Set the color of the mouse pointer of the selected frame to COLOR.
523 When called interactively, prompt for the name of the color to use."
524 (interactive "sColor: ")
525 (modify-frame-parameters (selected-frame)
526 (list (cons 'mouse-color color-name))))
528 (defun set-border-color (color-name)
529 "Set the color of the border of the selected frame to COLOR.
530 When called interactively, prompt for the name of the color to use."
531 (interactive "sColor: ")
532 (modify-frame-parameters (selected-frame)
533 (list (cons 'border-color color-name))))
535 (defun auto-raise-mode (arg)
536 "Toggle whether or not the selected frame should auto-raise.
537 With arg, turn auto-raise mode on if and only if arg is positive."
541 (if (cdr (assq 'auto-raise (frame-parameters (selected-frame))))
543 (modify-frame-parameters (selected-frame)
544 (list (cons 'auto-raise (> arg 0)))))
546 (defun auto-lower-mode (arg)
547 "Toggle whether or not the selected frame should auto-lower.
548 With arg, turn auto-lower mode on if and only if arg is positive."
552 (if (cdr (assq 'auto-lower (frame-parameters (selected-frame))))
554 (modify-frame-parameters (selected-frame)
555 (list (cons 'auto-lower (> arg 0)))))
557 (defun toggle-scroll-bar (arg)
558 "Toggle whether or not the selected frame has vertical scroll bars.
559 With arg, turn vertical scroll bars on if and only if arg is positive."
563 (if (cdr (assq 'vertical-scroll-bars
564 (frame-parameters (selected-frame))))
566 (modify-frame-parameters (selected-frame)
567 (list (cons 'vertical-scroll-bars (> arg 0)))))
569 (defun toggle-horizontal-scroll-bar (arg)
570 "Toggle whether or not the selected frame has horizontal scroll bars.
571 With arg, turn horizontal scroll bars on if and only if arg is positive.
572 Horizontal scroll bars aren't implemented yet."
574 (error "Horizontal scroll bars aren't implemented yet"))
577 ;;;; Aliases for backward compatibility with Emacs 18.
578 (defalias 'screen-height 'frame-height)
579 (defalias 'screen-width 'frame-width)
581 (defun set-screen-width (cols &optional pretend)
582 "Obsolete function to change the size of the screen to COLS columns.\n\
583 Optional second arg non-nil means that redisplay should use COLS columns\n\
584 but that the idea of the actual width of the frame should not be changed.\n\
585 This function is provided only for compatibility with Emacs 18; new code\n\
586 should use `set-frame-width instead'."
587 (set-frame-width (selected-frame) cols pretend))
589 (defun set-screen-height (lines &optional pretend)
590 "Obsolete function to change the height of the screen to LINES lines.\n\
591 Optional second arg non-nil means that redisplay should use LINES lines\n\
592 but that the idea of the actual height of the screen should not be changed.\n\
593 This function is provided only for compatibility with Emacs 18; new code\n\
594 should use `set-frame-width' instead."
595 (set-frame-height (selected-frame) lines pretend))
597 (make-obsolete 'screen-height 'frame-height)
598 (make-obsolete 'screen-width 'frame-width)
599 (make-obsolete 'set-screen-width 'set-frame-width)
600 (make-obsolete 'set-screen-height 'set-frame-height)
604 (defvar ctl-x-5-map (make-sparse-keymap)
605 "Keymap for frame commands.")
606 (defalias 'ctl-x-5-prefix ctl-x-5-map)
607 (define-key ctl-x-map "5" 'ctl-x-5-prefix)
609 (define-key ctl-x-5-map "2" 'make-frame)
610 (define-key ctl-x-5-map "0" 'delete-frame)
611 (define-key ctl-x-5-map "o" 'other-frame)
615 ;;; frame.el ends here