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