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