(update-file-autoloads): Do nothing when there are no cookies.
[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)))
eb8c3be9 57These supersede 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
fc4d4afb
RS
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)
64c669bc 80
dc6d9681
JB
81;;; If we create the initial frame, this is it.
82(defvar frame-initial-frame nil)
64c669bc
JB
83
84;;; startup.el calls this function before loading the user's init
dc6d9681 85;;; file - if there is no frame with a minibuffer open now, create
64c669bc 86;;; one to display messages while loading the init file.
dc6d9681 87(defun frame-initialize ()
64c669bc
JB
88
89 ;; Are we actually running under a window system at all?
90 (if (and window-system (not noninteractive))
7eadab74
JB
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))
36fc9c9f
RS
97 (progn
98 (setq default-minibuffer-frame
99 (setq frame-initial-frame
100 (new-frame initial-frame-alist)))
11281034
RS
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.
5e901379
JB
105 ;; Remember that they may occur more than once.
106 (let ((tail initial-frame-alist))
107 (while (consp tail)
108 (if (and (consp (car tail))
109 (memq (car (car tail)) '(height width top left)))
110 (setq initial-frame-alist
3008ffb6
JB
111 (delq tail initial-frame-alist)))
112 (setq tail (cdr tail))))
36fc9c9f
RS
113 ;; Handle `reverse' as a parameter.
114 (if (cdr (or (assq 'reverse initial-frame-alist)
c1bc92ce
RS
115 (assq 'reverse default-frame-alist)
116 (cons nil
792a0e1c 117 (x-get-resource "reverseVideo" "ReverseVideo"))))
36fc9c9f
RS
118 (let ((params (frame-parameters frame-initial-frame)))
119 (modify-frame-parameters
120 frame-initial-frame
949d4ac7
RS
121 ;; Must set cursor-color after background color.
122 ;; So put it first.
118c9082
JB
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)))))))))
7eadab74 133
dc6d9681
JB
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))
64c669bc
JB
138
139 ;; No, we're not running a window system. Arrange to cause errors.
dc6d9681 140 (setq frame-creation-function
9198945a
MB
141 (function
142 (lambda (parameters)
143 (error
8290babd 144 "Can't create multiple frames without a window system"))))))
64c669bc 145
7eadab74
JB
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.
dc6d9681 149(defun frame-notice-user-settings ()
7eadab74
JB
150
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)))
157
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)
161
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.
167 ;;
ec558adc
JB
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.
7eadab74
JB
173 (if (not (eq (cdr (or (assq 'minibuffer initial-frame-alist)
174 (assq 'minibuffer default-frame-alist)
175 '(minibuffer . t)))
176 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)))))
183
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)))))
190
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))
204
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)))
217
218 (if (eq default-minibuffer-frame frame-initial-frame)
219 (setq default-minibuffer-frame new-surrogate))
220
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))))
ec558adc
JB
229
230 ;; Redirect events enqueued at this frame to the new frame.
231 ;; Is this a good idea?
7eadab74 232 (redirect-frame-focus frame-initial-frame new)
ec558adc 233
7eadab74 234 ;; Finally, get rid of the old frame.
dc6d9681 235 (delete-frame frame-initial-frame))
7eadab74
JB
236
237 ;; Otherwise, we don't need all that rigamarole; just apply
238 ;; the new parameters.
dc6d9681 239 (modify-frame-parameters frame-initial-frame
ec558adc 240 (append initial-frame-alist
7eadab74 241 default-frame-alist))))
64c669bc 242
7eadab74
JB
243 ;; Restore the original buffer.
244 (set-buffer old-buffer)
245
246 ;; Make sure the initial frame can be GC'd if it is ever deleted.
d202f1f2
JB
247 ;; Make sure frame-notice-user-settings does nothing if called twice.
248 (setq frame-initial-frame nil)))
64c669bc
JB
249
250\f
7eadab74 251;;;; Creation of additional frames, and other frame miscellanea
dc6d9681 252
7eadab74 253;;; Return some frame other than the current frame, creating one if
eb8c3be9 254;;; necessary. Note that the minibuffer frame, if separate, is not
7eadab74 255;;; considered (see next-frame).
7253d8e0 256(defun get-other-frame ()
dc6d9681
JB
257 (let ((s (if (equal (next-frame (selected-frame)) (selected-frame))
258 (new-frame)
259 (next-frame (selected-frame)))))
64c669bc
JB
260 s))
261
dc6d9681
JB
262(defun next-multiframe-window ()
263 "Select the next window, regardless of which frame it is on."
64c669bc
JB
264 (interactive)
265 (select-window (next-window (selected-window)
266 (> (minibuffer-depth) 0)
267 t)))
268
dc6d9681
JB
269(defun previous-multiframe-window ()
270 "Select the previous window, regardless of which frame it is on."
64c669bc
JB
271 (interactive)
272 (select-window (previous-window (selected-window)
273 (> (minibuffer-depth) 0)
274 t)))
275
92e443b1 276;; Alias, kept temporarily.
31e1d920 277(defalias 'new-frame 'make-frame)
92e443b1 278(defun make-frame (&optional parameters)
dc6d9681 279 "Create a new frame, displaying the current buffer.
bc93c097 280
43a2e52c 281Optional argument PARAMETERS is an alist of parameters for the new
dc6d9681 282frame. Specifically, PARAMETERS is a list of pairs, each having one
43a2e52c
JB
283of the following forms:
284
eaa974e1 285\(name . STRING) - The frame should be named STRING.
43a2e52c 286
eaa974e1 287\(height . NUMBER) - The frame should be NUMBER text lines high. If
43a2e52c
JB
288 this parameter is present, the width parameter must also be
289 given.
290
eaa974e1 291\(width . NUMBER) - The frame should be NUMBER characters in width.
43a2e52c
JB
292 If this parameter is present, the height parameter must also
293 be given.
294
eaa974e1 295\(minibuffer . t) - the frame should have a minibuffer
0ca90494 296\(minibuffer . nil) - the frame should have no minibuffer
eaa974e1
JB
297\(minibuffer . only) - the frame should contain only a minibuffer
298\(minibuffer . WINDOW) - the frame should use WINDOW as its minibuffer window.
43a2e52c 299
eaa974e1
JB
300The documentation for the function `x-create-frame' describes
301additional frame parameters that Emacs recognizes for X window frames."
64c669bc 302 (interactive)
dc6d9681 303 (funcall frame-creation-function parameters))
64c669bc 304
7eadab74
JB
305(defun filtered-frame-list (predicate)
306 "Return a list of all live frames which satisfy PREDICATE."
307 (let ((frames (frame-list))
308 good-frames)
309 (while (consp frames)
310 (if (funcall predicate (car frames))
311 (setq good-frames (cons (car frames) good-frames)))
312 (setq frames (cdr frames)))
313 good-frames))
314
315(defun minibuffer-frame-list ()
316 "Return a list of all frames with their own minibuffers."
317 (filtered-frame-list
318 (function (lambda (frame)
319 (eq frame (window-frame (minibuffer-window frame)))))))
320
64c669bc 321\f
dc6d9681
JB
322;;;; Frame configurations
323
324(defun current-frame-configuration ()
325 "Return a list describing the positions and states of all frames.
376a7584
JB
326Its car is `frame-configuration'.
327Each element of the cdr is a list of the form (FRAME ALIST WINDOW-CONFIG),
328where
329 FRAME is a frame object,
330 ALIST is an association list specifying some of FRAME's parameters, and
331 WINDOW-CONFIG is a window configuration object for FRAME."
332 (cons 'frame-configuration
333 (mapcar (function
334 (lambda (frame)
335 (list frame
336 (frame-parameters frame)
337 (current-window-configuration frame))))
338 (frame-list))))
dc6d9681
JB
339
340(defun set-frame-configuration (configuration)
341 "Restore the frames to the state described by CONFIGURATION.
342Each frame listed in CONFIGURATION has its position, size, window
06b1a5ef 343configuration, and other parameters set as specified in CONFIGURATION."
376a7584
JB
344 (or (frame-configuration-p configuration)
345 (signal 'wrong-type-argument
346 (list 'frame-configuration-p configuration)))
347 (let ((config-alist (cdr configuration))
348 frames-to-delete)
06b1a5ef 349 (mapcar (function
dc6d9681 350 (lambda (frame)
376a7584 351 (let ((parameters (assq frame config-alist)))
06b1a5ef
JB
352 (if parameters
353 (progn
98e9d14b
JB
354 (modify-frame-parameters
355 frame
356 ;; Since we can't set a frame's minibuffer status,
357 ;; we might as well omit the parameter altogether.
358 (let* ((parms (nth 1 parameters))
359 (mini (assq 'minibuffer parms)))
360 (if mini (setq parms (delq mini parms)))
361 parms))
06b1a5ef 362 (set-window-configuration (nth 2 parameters)))
dc6d9681
JB
363 (setq frames-to-delete (cons frame frames-to-delete))))))
364 (frame-list))
365 (mapcar 'delete-frame frames-to-delete)))
64c669bc 366
376a7584
JB
367(defun frame-configuration-p (object)
368 "Return non-nil if OBJECT seems to be a frame configuration.
369Any list whose car is `frame-configuration' is assumed to be a frame
370configuration."
371 (and (consp object)
372 (eq (car object) 'frame-configuration)))
373
64c669bc 374\f
dc6d9681
JB
375;;;; Convenience functions for accessing and interactively changing
376;;;; frame parameters.
64c669bc 377
151bdc83 378(defun frame-height (&optional frame)
dc6d9681
JB
379 "Return number of lines available for display on FRAME.
380If FRAME is omitted, describe the currently selected frame."
151bdc83 381 (cdr (assq 'height (frame-parameters frame))))
dc6d9681
JB
382
383(defun frame-width (&optional frame)
384 "Return number of columns available for display on FRAME.
385If FRAME is omitted, describe the currently selected frame."
151bdc83 386 (cdr (assq 'width (frame-parameters frame))))
dc6d9681 387
64c669bc 388(defun set-default-font (font-name)
7eadab74
JB
389 "Set the font of the selected frame to FONT.
390When called interactively, prompt for the name of the font to use."
64c669bc 391 (interactive "sFont name: ")
dc6d9681 392 (modify-frame-parameters (selected-frame)
7eadab74 393 (list (cons 'font font-name))))
64c669bc 394
8290babd 395(defun set-background-color (color-name)
7eadab74
JB
396 "Set the background color of the selected frame to COLOR.
397When called interactively, prompt for the name of the color to use."
64c669bc 398 (interactive "sColor: ")
dc6d9681 399 (modify-frame-parameters (selected-frame)
7eadab74 400 (list (cons 'background-color color-name))))
64c669bc 401
8290babd 402(defun set-foreground-color (color-name)
7eadab74
JB
403 "Set the foreground color of the selected frame to COLOR.
404When called interactively, prompt for the name of the color to use."
64c669bc 405 (interactive "sColor: ")
dc6d9681 406 (modify-frame-parameters (selected-frame)
7eadab74 407 (list (cons 'foreground-color color-name))))
64c669bc
JB
408
409(defun set-cursor-color (color-name)
7eadab74
JB
410 "Set the text cursor color of the selected frame to COLOR.
411When called interactively, prompt for the name of the color to use."
64c669bc 412 (interactive "sColor: ")
dc6d9681 413 (modify-frame-parameters (selected-frame)
7eadab74 414 (list (cons 'cursor-color color-name))))
64c669bc 415
eaa974e1 416(defun set-mouse-color (color-name)
7eadab74
JB
417 "Set the color of the mouse pointer of the selected frame to COLOR.
418When called interactively, prompt for the name of the color to use."
64c669bc 419 (interactive "sColor: ")
dc6d9681 420 (modify-frame-parameters (selected-frame)
7eadab74
JB
421 (list (cons 'mouse-color color-name))))
422
eaa974e1
JB
423(defun set-border-color (color-name)
424 "Set the color of the border of the selected frame to COLOR.
425When called interactively, prompt for the name of the color to use."
426 (interactive "sColor: ")
427 (modify-frame-parameters (selected-frame)
428 (list (cons 'border-color color-name))))
429
430(defun auto-raise-mode (arg)
7eadab74
JB
431 "Toggle whether or not the selected frame should auto-raise.
432With arg, turn auto-raise mode on if and only if arg is positive."
433 (interactive "P")
434 (if (null arg)
435 (setq arg
436 (if (cdr (assq 'auto-raise (frame-parameters (selected-frame))))
437 -1 1)))
dc6d9681 438 (modify-frame-parameters (selected-frame)
7eadab74
JB
439 (list (cons 'auto-raise (> arg 0)))))
440
eaa974e1 441(defun auto-lower-mode (arg)
7eadab74
JB
442 "Toggle whether or not the selected frame should auto-lower.
443With arg, turn auto-lower mode on if and only if arg is positive."
444 (interactive "P")
445 (if (null arg)
446 (setq arg
447 (if (cdr (assq 'auto-lower (frame-parameters (selected-frame))))
448 -1 1)))
dc6d9681 449 (modify-frame-parameters (selected-frame)
7eadab74
JB
450 (list (cons 'auto-lower (> arg 0)))))
451
7e85c6b5 452(defun toggle-scroll-bar (arg)
0ca90494
JB
453 "Toggle whether or not the selected frame has vertical scroll bars.
454With arg, turn vertical scroll bars on if and only if arg is positive."
7eadab74
JB
455 (interactive "P")
456 (if (null arg)
457 (setq arg
0ca90494 458 (if (cdr (assq 'vertical-scroll-bars
7eadab74
JB
459 (frame-parameters (selected-frame))))
460 -1 1)))
dc6d9681 461 (modify-frame-parameters (selected-frame)
0ca90494 462 (list (cons 'vertical-scroll-bars (> arg 0)))))
7eadab74 463
8290babd 464(defun toggle-horizontal-scroll-bar (arg)
0ca90494
JB
465 "Toggle whether or not the selected frame has horizontal scroll bars.
466With arg, turn horizontal scroll bars on if and only if arg is positive.
467Horizontal scroll bars aren't implemented yet."
7eadab74 468 (interactive "P")
8290babd 469 (error "Horizontal scroll bars aren't implemented yet"))
64c669bc 470
64c669bc 471\f
dc6d9681 472;;;; Aliases for backward compatibility with Emacs 18.
31e1d920
ER
473(defalias 'screen-height 'frame-height)
474(defalias 'screen-width 'frame-width)
9e2b097b
JB
475
476(defun set-screen-width (cols &optional pretend)
477 "Obsolete function to change the size of the screen to COLS columns.\n\
478Optional second arg non-nil means that redisplay should use COLS columns\n\
479but that the idea of the actual width of the frame should not be changed.\n\
480This function is provided only for compatibility with Emacs 18; new code\n\
fe668515 481should use `set-frame-width instead'."
9e2b097b
JB
482 (set-frame-width (selected-frame) cols pretend))
483
484(defun set-screen-height (lines &optional pretend)
485 "Obsolete function to change the height of the screen to LINES lines.\n\
486Optional second arg non-nil means that redisplay should use LINES lines\n\
487but that the idea of the actual height of the screen should not be changed.\n\
488This function is provided only for compatibility with Emacs 18; new code\n\
fe668515 489should use `set-frame-width' instead."
9e2b097b
JB
490 (set-frame-height (selected-frame) lines pretend))
491
492(make-obsolete 'screen-height 'frame-height)
493(make-obsolete 'screen-width 'frame-width)
494(make-obsolete 'set-screen-width 'set-frame-width)
495(make-obsolete 'set-screen-height 'set-frame-height)
dc6d9681
JB
496
497\f
64c669bc 498;;;; Key bindings
daa37602 499(defvar ctl-x-5-map (make-sparse-keymap)
dc6d9681 500 "Keymap for frame commands.")
31e1d920 501(defalias 'ctl-x-5-prefix ctl-x-5-map)
daa37602 502(define-key ctl-x-map "5" 'ctl-x-5-prefix)
64c669bc 503
dc6d9681
JB
504(define-key ctl-x-5-map "2" 'new-frame)
505(define-key ctl-x-5-map "0" 'delete-frame)
49116ac0 506
dc6d9681 507(provide 'frame)
c88ab9ce 508
dc6d9681 509;;; frame.el ends here