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