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