(debug, debugger-eval-expression): Preserve match-data.
[emacs.git] / lisp / frame.el
blob9ef36acf308787656884528c4c015ed1b0f10d79
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 supersede 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 ;; Record the parameters used in frame-initialize to make the initial frame.
85 (defvar frame-initial-frame-alist)
87 ;;; startup.el calls this function before loading the user's init
88 ;;; file - if there is no frame with a minibuffer open now, create
89 ;;; one to display messages while loading the init file.
90 (defun frame-initialize ()
92 ;; Are we actually running under a window system at all?
93 (if (and window-system (not noninteractive))
94 (progn
95 ;; If there is no frame with a minibuffer besides the terminal
96 ;; frame, then we need to create the opening frame. Make sure
97 ;; it has a minibuffer, but let initial-frame-alist omit the
98 ;; minibuffer spec.
99 (or (delq terminal-frame (minibuffer-frame-list))
100 (progn
101 (setq frame-initial-frame-alist
102 (append initial-frame-alist default-frame-alist))
103 (setq default-minibuffer-frame
104 (setq frame-initial-frame
105 (new-frame initial-frame-alist)))
106 ;; Delete any specifications for window geometry parameters
107 ;; so that we won't reapply them in frame-notice-user-settings.
108 ;; It would be wrong to reapply them then,
109 ;; because that would override explicit user resizing.
110 (setq initial-frame-alist
111 (frame-remove-geometry-params initial-frame-alist))
112 ;; Handle `reverse' as a parameter.
113 (if (cdr (or (assq 'reverse initial-frame-alist)
114 (assq 'reverse default-frame-alist)
115 (cons nil
116 (member (x-get-resource "reverseVideo" "ReverseVideo")
117 '("on" "true")))))
118 (let ((params (frame-parameters frame-initial-frame)))
119 (modify-frame-parameters
120 frame-initial-frame
121 ;; Must set cursor-color after background color.
122 ;; So put it first.
123 (list (cons 'cursor-color
124 (cdr (assq 'background-color params)))
125 (cons 'foreground-color
126 (cdr (assq 'background-color params)))
127 (cons 'background-color
128 (cdr (assq 'foreground-color params)))
129 (cons 'mouse-color
130 (cdr (assq 'background-color params)))
131 (cons 'border-color
132 (cdr (assq 'background-color params)))))))))
134 ;; At this point, we know that we have a frame open, so we
135 ;; can delete the terminal frame.
136 (delete-frame terminal-frame)
137 (setq terminal-frame nil))
139 ;; No, we're not running a window system. Arrange to cause errors.
140 (setq frame-creation-function
141 (function
142 (lambda (parameters)
143 (error
144 "Can't create multiple frames without a window system"))))))
146 ;;; startup.el calls this function after loading the user's init
147 ;;; file. Now default-frame-alist and initial-frame-alist contain
148 ;;; information to which we must react; do what needs to be done.
149 (defun frame-notice-user-settings ()
151 ;; Creating and deleting frames may shift the selected frame around,
152 ;; and thus the current buffer. Protect against that. We don't
153 ;; want to use save-excursion here, because that may also try to set
154 ;; the buffer of the selected window, which fails when the selected
155 ;; window is the minibuffer.
156 (let ((old-buffer (current-buffer)))
158 ;; If the initial frame is still around, apply initial-frame-alist
159 ;; and default-frame-alist to it.
160 (if (frame-live-p frame-initial-frame)
162 ;; The initial frame we create above always has a minibuffer.
163 ;; If the user wants to remove it, or make it a minibuffer-only
164 ;; frame, then we'll have to delete the current frame and make a
165 ;; new one; you can't remove or add a root window to/from an
166 ;; existing frame.
168 ;; NOTE: default-frame-alist was nil when we created the
169 ;; existing frame. We need to explicitly include
170 ;; default-frame-alist in the parameters of the screen we
171 ;; create here, so that its new value, gleaned from the user's
172 ;; .emacs file, will be applied to the existing screen.
173 (if (not (eq (cdr (or (assq 'minibuffer initial-frame-alist)
174 (assq 'minibuffer default-frame-alist)
175 '(minibuffer . t)))
177 ;; Create the new frame.
178 (let ((new
179 (new-frame
180 (append initial-frame-alist
181 default-frame-alist
182 (frame-parameters frame-initial-frame)))))
184 ;; The initial frame, which we are about to delete, may be
185 ;; the only frame with a minibuffer. If it is, create a
186 ;; new one.
187 (or (delq frame-initial-frame (minibuffer-frame-list))
188 (new-frame (append minibuffer-frame-alist
189 '((minibuffer . only)))))
191 ;; If the initial frame is serving as a surrogate
192 ;; minibuffer frame for any frames, we need to wean them
193 ;; onto a new frame. The default-minibuffer-frame
194 ;; variable must be handled similarly.
195 (let ((users-of-initial
196 (filtered-frame-list
197 (function (lambda (frame)
198 (and (not (eq frame frame-initial-frame))
199 (eq (window-frame
200 (minibuffer-window frame))
201 frame-initial-frame)))))))
202 (if (or users-of-initial
203 (eq default-minibuffer-frame frame-initial-frame))
205 ;; Choose an appropriate frame. Prefer frames which
206 ;; are only minibuffers.
207 (let* ((new-surrogate
208 (car
209 (or (filtered-frame-list
210 (function
211 (lambda (frame)
212 (eq (cdr (assq 'minibuffer
213 (frame-parameters frame)))
214 'only))))
215 (minibuffer-frame-list))))
216 (new-minibuffer (minibuffer-window new-surrogate)))
218 (if (eq default-minibuffer-frame frame-initial-frame)
219 (setq default-minibuffer-frame new-surrogate))
221 ;; Wean the frames using frame-initial-frame as
222 ;; their minibuffer frame.
223 (mapcar
224 (function
225 (lambda (frame)
226 (modify-frame-parameters
227 frame (list (cons 'minibuffer new-minibuffer)))))
228 users-of-initial))))
230 ;; Redirect events enqueued at this frame to the new frame.
231 ;; Is this a good idea?
232 (redirect-frame-focus frame-initial-frame new)
234 ;; Finally, get rid of the old frame.
235 (delete-frame frame-initial-frame t))
237 ;; Otherwise, we don't need all that rigamarole; just apply
238 ;; the new parameters.
239 (let (newparms allparms tail)
240 (setq allparms (append initial-frame-alist
241 default-frame-alist))
242 (setq tail allparms)
243 ;; Find just the parms that have changed since we first
244 ;; made this frame. Those are the ones actually set by
245 ;; the init file. For those parms whose values we already knew
246 ;; (such as those spec'd by command line options)
247 ;; it is undesirable to specify the parm again
248 ;; once the user has seen the frame and been able to alter it
249 ;; manually.
250 (while tail
251 (let (newval oldval)
252 (setq oldval (cdr (assq (car (car tail))
253 frame-initial-frame-alist)))
254 (setq newval (cdr (assq (car (car tail)) allparms)))
255 (or (eq oldval newval)
256 (setq newparms
257 (cons (cons (car (car tail)) newval) newparms))))
258 (setq tail (cdr tail)))
259 (modify-frame-parameters frame-initial-frame
260 (nreverse newparms)))))
262 ;; Restore the original buffer.
263 (set-buffer old-buffer)
265 ;; Make sure the initial frame can be GC'd if it is ever deleted.
266 ;; Make sure frame-notice-user-settings does nothing if called twice.
267 (setq frame-initial-frame nil)))
270 ;;;; Creation of additional frames, and other frame miscellanea
272 ;;; Return some frame other than the current frame, creating one if
273 ;;; necessary. Note that the minibuffer frame, if separate, is not
274 ;;; considered (see next-frame).
275 (defun get-other-frame ()
276 (let ((s (if (equal (next-frame (selected-frame)) (selected-frame))
277 (new-frame)
278 (next-frame (selected-frame)))))
281 (defun next-multiframe-window ()
282 "Select the next window, regardless of which frame it is on."
283 (interactive)
284 (select-window (next-window (selected-window)
285 (> (minibuffer-depth) 0)
286 t)))
288 (defun previous-multiframe-window ()
289 "Select the previous window, regardless of which frame it is on."
290 (interactive)
291 (select-window (previous-window (selected-window)
292 (> (minibuffer-depth) 0)
293 t)))
295 ;; Alias, kept temporarily.
296 (defalias 'new-frame 'make-frame)
297 (defun make-frame (&optional parameters)
298 "Create a new frame, displaying the current buffer.
300 Optional argument PARAMETERS is an alist of parameters for the new
301 frame. Specifically, PARAMETERS is a list of pairs, each having one
302 of the following forms:
304 \(name . STRING) - The frame should be named STRING.
306 \(height . NUMBER) - The frame should be NUMBER text lines high. If
307 this parameter is present, the width parameter must also be
308 given.
310 \(width . NUMBER) - The frame should be NUMBER characters in width.
311 If this parameter is present, the height parameter must also
312 be given.
314 \(minibuffer . t) - the frame should have a minibuffer
315 \(minibuffer . nil) - the frame should have no minibuffer
316 \(minibuffer . only) - the frame should contain only a minibuffer
317 \(minibuffer . WINDOW) - the frame should use WINDOW as its minibuffer window.
319 The documentation for the function `x-create-frame' describes
320 additional frame parameters that Emacs recognizes for X window frames."
321 (interactive)
322 (let ((nframe))
323 (run-hooks 'before-make-frame-hook)
324 (setq nframe (funcall frame-creation-function parameters))
325 (run-hooks 'after-make-frame-hook)
326 nframe))
328 (defun filtered-frame-list (predicate)
329 "Return a list of all live frames which satisfy PREDICATE."
330 (let ((frames (frame-list))
331 good-frames)
332 (while (consp frames)
333 (if (funcall predicate (car frames))
334 (setq good-frames (cons (car frames) good-frames)))
335 (setq frames (cdr frames)))
336 good-frames))
338 (defun minibuffer-frame-list ()
339 "Return a list of all frames with their own minibuffers."
340 (filtered-frame-list
341 (function (lambda (frame)
342 (eq frame (window-frame (minibuffer-window frame)))))))
344 (defun frame-remove-geometry-params (param-list)
345 "Return the parameter list PARAM-LIST, but with geometry specs removed.
346 This deletes all bindings in PARAM-LIST for `top', `left', `width',
347 and `height' parameters.
348 Emacs uses this to avoid overriding explicit moves and resizings from
349 the user during startup."
350 (setq param-list (cons nil param-list))
351 (let ((tail param-list))
352 (while (consp (cdr tail))
353 (if (and (consp (car (cdr tail)))
354 (memq (car (car (cdr tail))) '(height width top left)))
355 (setcdr tail (cdr (cdr tail)))
356 (setq tail (cdr tail)))))
357 (cdr param-list))
360 (defun other-frame (arg)
361 "Select the ARG'th different visible frame, and raise it.
362 All frames are arranged in a cyclic order.
363 This command selects the frame ARG steps away in that order.
364 A negative ARG moves in the opposite order."
365 (interactive "p")
366 (let ((frame (selected-frame)))
367 (while (> arg 0)
368 (setq frame (next-frame frame))
369 (while (not (eq (frame-visible-p frame) t))
370 (setq frame (next-frame frame)))
371 (setq arg (1- arg)))
372 (while (< arg 0)
373 (setq frame (previous-frame frame))
374 (while (not (eq (frame-visible-p frame) t))
375 (setq frame (previous-frame frame)))
376 (setq arg (1+ arg)))
377 (raise-frame frame)
378 (select-frame frame)
379 (set-mouse-position (selected-frame) (1- (frame-width)) 0)
380 (unfocus-frame)))
382 ;;;; Frame configurations
384 (defun current-frame-configuration ()
385 "Return a list describing the positions and states of all frames.
386 Its car is `frame-configuration'.
387 Each element of the cdr is a list of the form (FRAME ALIST WINDOW-CONFIG),
388 where
389 FRAME is a frame object,
390 ALIST is an association list specifying some of FRAME's parameters, and
391 WINDOW-CONFIG is a window configuration object for FRAME."
392 (cons 'frame-configuration
393 (mapcar (function
394 (lambda (frame)
395 (list frame
396 (frame-parameters frame)
397 (current-window-configuration frame))))
398 (frame-list))))
400 (defun set-frame-configuration (configuration &optional nodelete)
401 "Restore the frames to the state described by CONFIGURATION.
402 Each frame listed in CONFIGURATION has its position, size, window
403 configuration, and other parameters set as specified in CONFIGURATION.
404 Ordinarily, this function deletes all existing frames not
405 listed in CONFIGURATION. But if optional second argument NODELETE
406 is given and non-nil, the unwanted frames are iconified instead."
407 (or (frame-configuration-p configuration)
408 (signal 'wrong-type-argument
409 (list 'frame-configuration-p configuration)))
410 (let ((config-alist (cdr configuration))
411 frames-to-delete)
412 (mapcar (function
413 (lambda (frame)
414 (let ((parameters (assq frame config-alist)))
415 (if parameters
416 (progn
417 (modify-frame-parameters
418 frame
419 ;; Since we can't set a frame's minibuffer status,
420 ;; we might as well omit the parameter altogether.
421 (let* ((parms (nth 1 parameters))
422 (mini (assq 'minibuffer parms)))
423 (if mini (setq parms (delq mini parms)))
424 parms))
425 (set-window-configuration (nth 2 parameters)))
426 (setq frames-to-delete (cons frame frames-to-delete))))))
427 (frame-list))
428 (if nodelete
429 ;; Note: making frames invisible here was tried
430 ;; but led to some strange behavior--each time the frame
431 ;; was made visible again, the window manager asked afresh
432 ;; for where to put it.
433 (mapcar 'iconify-frame frames-to-delete)
434 (mapcar 'delete-frame frames-to-delete))))
436 (defun frame-configuration-p (object)
437 "Return non-nil if OBJECT seems to be a frame configuration.
438 Any list whose car is `frame-configuration' is assumed to be a frame
439 configuration."
440 (and (consp object)
441 (eq (car object) 'frame-configuration)))
444 ;;;; Convenience functions for accessing and interactively changing
445 ;;;; frame parameters.
447 (defun frame-height (&optional frame)
448 "Return number of lines available for display on FRAME.
449 If FRAME is omitted, describe the currently selected frame."
450 (cdr (assq 'height (frame-parameters frame))))
452 (defun frame-width (&optional frame)
453 "Return number of columns available for display on FRAME.
454 If FRAME is omitted, describe the currently selected frame."
455 (cdr (assq 'width (frame-parameters frame))))
457 (defun set-default-font (font-name)
458 "Set the font of the selected frame to FONT.
459 When called interactively, prompt for the name of the font to use."
460 (interactive "sFont name: ")
461 (modify-frame-parameters (selected-frame)
462 (list (cons 'font font-name))))
464 (defun set-background-color (color-name)
465 "Set the background color of the selected frame to COLOR.
466 When called interactively, prompt for the name of the color to use."
467 (interactive "sColor: ")
468 (modify-frame-parameters (selected-frame)
469 (list (cons 'background-color color-name))))
471 (defun set-foreground-color (color-name)
472 "Set the foreground color of the selected frame to COLOR.
473 When called interactively, prompt for the name of the color to use."
474 (interactive "sColor: ")
475 (modify-frame-parameters (selected-frame)
476 (list (cons 'foreground-color color-name))))
478 (defun set-cursor-color (color-name)
479 "Set the text cursor color of the selected frame to COLOR.
480 When called interactively, prompt for the name of the color to use."
481 (interactive "sColor: ")
482 (modify-frame-parameters (selected-frame)
483 (list (cons 'cursor-color color-name))))
485 (defun set-mouse-color (color-name)
486 "Set the color of the mouse pointer of the selected frame to COLOR.
487 When called interactively, prompt for the name of the color to use."
488 (interactive "sColor: ")
489 (modify-frame-parameters (selected-frame)
490 (list (cons 'mouse-color color-name))))
492 (defun set-border-color (color-name)
493 "Set the color of the border of the selected frame to COLOR.
494 When called interactively, prompt for the name of the color to use."
495 (interactive "sColor: ")
496 (modify-frame-parameters (selected-frame)
497 (list (cons 'border-color color-name))))
499 (defun auto-raise-mode (arg)
500 "Toggle whether or not the selected frame should auto-raise.
501 With arg, turn auto-raise mode on if and only if arg is positive."
502 (interactive "P")
503 (if (null arg)
504 (setq arg
505 (if (cdr (assq 'auto-raise (frame-parameters (selected-frame))))
506 -1 1)))
507 (modify-frame-parameters (selected-frame)
508 (list (cons 'auto-raise (> arg 0)))))
510 (defun auto-lower-mode (arg)
511 "Toggle whether or not the selected frame should auto-lower.
512 With arg, turn auto-lower mode on if and only if arg is positive."
513 (interactive "P")
514 (if (null arg)
515 (setq arg
516 (if (cdr (assq 'auto-lower (frame-parameters (selected-frame))))
517 -1 1)))
518 (modify-frame-parameters (selected-frame)
519 (list (cons 'auto-lower (> arg 0)))))
521 (defun toggle-scroll-bar (arg)
522 "Toggle whether or not the selected frame has vertical scroll bars.
523 With arg, turn vertical scroll bars on if and only if arg is positive."
524 (interactive "P")
525 (if (null arg)
526 (setq arg
527 (if (cdr (assq 'vertical-scroll-bars
528 (frame-parameters (selected-frame))))
529 -1 1)))
530 (modify-frame-parameters (selected-frame)
531 (list (cons 'vertical-scroll-bars (> arg 0)))))
533 (defun toggle-horizontal-scroll-bar (arg)
534 "Toggle whether or not the selected frame has horizontal scroll bars.
535 With arg, turn horizontal scroll bars on if and only if arg is positive.
536 Horizontal scroll bars aren't implemented yet."
537 (interactive "P")
538 (error "Horizontal scroll bars aren't implemented yet"))
541 ;;;; Aliases for backward compatibility with Emacs 18.
542 (defalias 'screen-height 'frame-height)
543 (defalias 'screen-width 'frame-width)
545 (defun set-screen-width (cols &optional pretend)
546 "Obsolete function to change the size of the screen to COLS columns.\n\
547 Optional second arg non-nil means that redisplay should use COLS columns\n\
548 but that the idea of the actual width of the frame should not be changed.\n\
549 This function is provided only for compatibility with Emacs 18; new code\n\
550 should use `set-frame-width instead'."
551 (set-frame-width (selected-frame) cols pretend))
553 (defun set-screen-height (lines &optional pretend)
554 "Obsolete function to change the height of the screen to LINES lines.\n\
555 Optional second arg non-nil means that redisplay should use LINES lines\n\
556 but that the idea of the actual height of the screen should not be changed.\n\
557 This function is provided only for compatibility with Emacs 18; new code\n\
558 should use `set-frame-width' instead."
559 (set-frame-height (selected-frame) lines pretend))
561 (make-obsolete 'screen-height 'frame-height)
562 (make-obsolete 'screen-width 'frame-width)
563 (make-obsolete 'set-screen-width 'set-frame-width)
564 (make-obsolete 'set-screen-height 'set-frame-height)
567 ;;;; Key bindings
568 (defvar ctl-x-5-map (make-sparse-keymap)
569 "Keymap for frame commands.")
570 (defalias 'ctl-x-5-prefix ctl-x-5-map)
571 (define-key ctl-x-map "5" 'ctl-x-5-prefix)
573 (define-key ctl-x-5-map "2" 'new-frame)
574 (define-key ctl-x-5-map "0" 'delete-frame)
575 (define-key ctl-x-5-map "o" 'other-frame)
577 (provide 'frame)
579 ;;; frame.el ends here