(w32_wnd_proc): Fix bug introduced by previous change;
[bpt/emacs.git] / lisp / frame.el
CommitLineData
dc6d9681 1;;; frame.el --- multi-frame management independent of window systems.
c88ab9ce 2
30e19aee 3;; Copyright (C) 1993, 1994, 1996, 1997 Free Software Foundation, Inc.
3a801d0c 4
fc68affa 5;; Maintainer: FSF
fd7fa35a 6;; Keywords: internal
fc68affa 7
b578f267
EN
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 the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
64c669bc 24
fc68affa
ER
25;;; Code:
26
dc6d9681
JB
27(defvar frame-creation-function nil
28 "Window-system dependent function to call to create a new frame.
29The window system startup file should set this to its frame creation
64c669bc
JB
30function, which should take an alist of parameters as its argument.")
31
216f1169
RS
32;;; The initial value given here for used to ask for a minibuffer.
33;;; But that's not necessary, because the default is to have one.
34;;; By not specifying it here, we let an X resource specify it.
0c966f88 35(defcustom initial-frame-alist nil
eaa974e1
JB
36 "Alist of frame parameters for creating the initial X window frame.
37You can set this in your `.emacs' file; for example,
dc6d9681 38 (setq initial-frame-alist '((top . 1) (left . 1) (width . 80) (height . 55)))
de17da15
RS
39Parameters specified here supersede the values given in `default-frame-alist'.
40
41If the value calls for a frame without a minibuffer, and you have not created
42a minibuffer frame on your own, one is created according to
7eadab74 43`minibuffer-frame-alist'.
de17da15
RS
44
45You can specify geometry-related options for just the initial frame
46by setting this variable in your `.emacs' file; however, they won't
85557d7e 47take effect until Emacs reads `.emacs', which happens after first creating
de17da15
RS
48the frame. If you want the frame to have the proper geometry as soon
49as it appears, you need to use this three-step process:
50* Specify X resources to give the geometry you want.
51* Set `default-frame-alist' to override these options so that they
52 don't affect subsequent frames.
53* Set `initial-frame-alist' in a way that matches the X resources,
0c966f88
RS
54 to override what you put in `default-frame-alist'."
55 :type '(repeat (cons :format "%v"
56 (symbol :tag "Parameter")
57 (sexp :tag "Value")))
58 :group 'frames)
64c669bc 59
7eadab74 60(defvar minibuffer-frame-alist '((width . 80) (height . 2))
eaa974e1
JB
61 "Alist of frame parameters for initially creating a minibuffer frame.
62You can set this in your `.emacs' file; for example,
dc6d9681 63 (setq minibuffer-frame-alist
7eadab74 64 '((top . 1) (left . 1) (width . 80) (height . 2)))
eaa974e1
JB
65Parameters specified here supersede the values given in
66`default-frame-alist'.")
64c669bc 67
dc6d9681 68(defvar pop-up-frame-alist nil
eaa974e1 69 "Alist of frame parameters used when creating pop-up frames.
dc6d9681 70Pop-up frames are used for completions, help, and the like.
64c669bc 71This variable can be set in your init file, like this:
dc6d9681 72 (setq pop-up-frame-alist '((width . 80) (height . 20)))
eb8c3be9 73These supersede the values given in `default-frame-alist'.")
64c669bc 74
dc6d9681 75(setq pop-up-frame-function
64c669bc 76 (function (lambda ()
6eb018ba 77 (make-frame pop-up-frame-alist))))
64c669bc 78
30e19aee 79(defcustom special-display-frame-alist
8a9e86e6
RS
80 '((height . 14) (width . 80) (unsplittable . t))
81 "*Alist of frame parameters used when creating special frames.
82Special frames are used for buffers whose names are in
83`special-display-buffer-names' and for buffers whose names match
84one of the regular expressions in `special-display-regexps'.
85This variable can be set in your init file, like this:
86 (setq special-display-frame-alist '((width . 80) (height . 20)))
30e19aee
RS
87These supersede the values given in `default-frame-alist'."
88 :type '(repeat (cons :format "%v"
89 (symbol :tag "Parameter")
90 (sexp :tag "Value")))
91 :group 'frames)
8a9e86e6
RS
92
93;; Display BUFFER in its own frame, reusing an existing window if any.
94;; Return the window chosen.
95;; Currently we do not insist on selecting the window within its frame.
95e6cf39 96;; If ARGS is an alist, use it as a list of frame parameter specs.
85557d7e 97;; If ARGS is a list whose car is a symbol,
95e6cf39
RS
98;; use (car ARGS) as a function to do the work.
99;; Pass it BUFFER as first arg, and (cdr ARGS) gives the rest of the args.
100(defun special-display-popup-frame (buffer &optional args)
101 (if (and args (symbolp (car args)))
102 (apply (car args) buffer (cdr args))
103 (let ((window (get-buffer-window buffer t)))
104 (if window
105 ;; If we have a window already, make it visible.
106 (let ((frame (window-frame window)))
107 (make-frame-visible frame)
108 (raise-frame frame)
109 window)
110 ;; If no window yet, make one in a new frame.
111 (let ((frame (make-frame (append args special-display-frame-alist))))
112 (set-window-buffer (frame-selected-window frame) buffer)
113 (set-window-dedicated-p (frame-selected-window frame) t)
114 (frame-selected-window frame))))))
8a9e86e6 115
924be53a
RS
116;; Handle delete-frame events from the X server.
117(defun handle-delete-frame (event)
118 (interactive "e")
119 (let ((frame (posn-window (event-start event)))
120 (i 0)
121 (tail (frame-list)))
122 (while tail
123 (and (frame-visible-p (car tail))
124 (not (eq (car tail) frame))
125 (setq i (1+ i)))
126 (setq tail (cdr tail)))
127 (if (> i 0)
128 (delete-frame frame t)
a9562d19
RS
129 ;; Gildea@x.org says it is ok to ask questions before terminating.
130 (save-buffers-kill-emacs))))
64c669bc 131\f
dc6d9681 132;;;; Arrangement of frames at startup
64c669bc
JB
133
134;;; 1) Load the window system startup file from the lisp library and read the
135;;; high-priority arguments (-q and the like). The window system startup
dc6d9681 136;;; file should create any frames specified in the window system defaults.
85557d7e 137;;;
dc6d9681 138;;; 2) If no frames have been opened, we open an initial text frame.
64c669bc
JB
139;;;
140;;; 3) Once the init file is done, we apply any newly set parameters
dc6d9681 141;;; in initial-frame-alist to the frame.
64c669bc 142
85557d7e 143;; These are now called explicitly at the proper times,
fc4d4afb
RS
144;; since that is easier to understand.
145;; Actually using hooks within Emacs is bad for future maintenance. --rms.
146;; (add-hook 'before-init-hook 'frame-initialize)
147;; (add-hook 'window-setup-hook 'frame-notice-user-settings)
64c669bc 148
dc6d9681
JB
149;;; If we create the initial frame, this is it.
150(defvar frame-initial-frame nil)
64c669bc 151
3f2f8c83
RS
152;; Record the parameters used in frame-initialize to make the initial frame.
153(defvar frame-initial-frame-alist)
154
791e09d8
RS
155(defvar frame-initial-geometry-arguments nil)
156
64c669bc 157;;; startup.el calls this function before loading the user's init
dc6d9681 158;;; file - if there is no frame with a minibuffer open now, create
64c669bc 159;;; one to display messages while loading the init file.
dc6d9681 160(defun frame-initialize ()
85557d7e 161
64c669bc 162 ;; Are we actually running under a window system at all?
004f057a 163 (if (and window-system (not noninteractive) (not (eq window-system 'pc)))
7eadab74 164 (progn
62642af9
RS
165 ;; Turn on special-display processing only if there's a window system.
166 (setq special-display-function 'special-display-popup-frame)
167
7eadab74
JB
168 ;; If there is no frame with a minibuffer besides the terminal
169 ;; frame, then we need to create the opening frame. Make sure
170 ;; it has a minibuffer, but let initial-frame-alist omit the
171 ;; minibuffer spec.
172 (or (delq terminal-frame (minibuffer-frame-list))
36fc9c9f 173 (progn
3f2f8c83
RS
174 (setq frame-initial-frame-alist
175 (append initial-frame-alist default-frame-alist))
a8bb5882
RS
176 (or (assq 'horizontal-scroll-bars frame-initial-frame-alist)
177 (setq frame-initial-frame-alist
178 (cons '(horizontal-scroll-bars . t)
179 frame-initial-frame-alist)))
36fc9c9f
RS
180 (setq default-minibuffer-frame
181 (setq frame-initial-frame
96a84970 182 (make-frame frame-initial-frame-alist)))
11281034
RS
183 ;; Delete any specifications for window geometry parameters
184 ;; so that we won't reapply them in frame-notice-user-settings.
185 ;; It would be wrong to reapply them then,
186 ;; because that would override explicit user resizing.
58bf6042 187 (setq initial-frame-alist
c2079f0a 188 (frame-remove-geometry-params initial-frame-alist))))
85557d7e 189 ;; At this point, we know that we have a frame open, so we
dc6d9681
JB
190 ;; can delete the terminal frame.
191 (delete-frame terminal-frame)
192 (setq terminal-frame nil))
85557d7e 193
0759c4d0
KH
194 ;; No, we're not running a window system. Use make-terminal-frame if
195 ;; we support that feature, otherwise arrange to cause errors.
004f057a
RS
196 (or (eq window-system 'pc)
197 (setq frame-creation-function
198 (if (fboundp 'make-terminal-frame)
199 'make-terminal-frame
200 (function
201 (lambda (parameters)
202 (error
203 "Can't create multiple frames without a window system"))))))))
85557d7e 204
7eadab74
JB
205;;; startup.el calls this function after loading the user's init
206;;; file. Now default-frame-alist and initial-frame-alist contain
207;;; information to which we must react; do what needs to be done.
dc6d9681 208(defun frame-notice-user-settings ()
7eadab74 209
2ffa6186 210 ;; Make menu-bar-mode and default-frame-alist consistent.
b65be6a8
KH
211 (if (boundp 'menu-bar-mode)
212 (let ((default (assq 'menu-bar-lines default-frame-alist)))
213 (if default
214 (setq menu-bar-mode (not (eq (cdr default) 0)))
215 (setq default-frame-alist
216 (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0))
217 default-frame-alist)))))
2ffa6186 218
7eadab74
JB
219 ;; Creating and deleting frames may shift the selected frame around,
220 ;; and thus the current buffer. Protect against that. We don't
221 ;; want to use save-excursion here, because that may also try to set
222 ;; the buffer of the selected window, which fails when the selected
223 ;; window is the minibuffer.
224 (let ((old-buffer (current-buffer)))
225
226 ;; If the initial frame is still around, apply initial-frame-alist
227 ;; and default-frame-alist to it.
228 (if (frame-live-p frame-initial-frame)
229
230 ;; The initial frame we create above always has a minibuffer.
231 ;; If the user wants to remove it, or make it a minibuffer-only
232 ;; frame, then we'll have to delete the current frame and make a
233 ;; new one; you can't remove or add a root window to/from an
234 ;; existing frame.
235 ;;
ec558adc
JB
236 ;; NOTE: default-frame-alist was nil when we created the
237 ;; existing frame. We need to explicitly include
238 ;; default-frame-alist in the parameters of the screen we
239 ;; create here, so that its new value, gleaned from the user's
240 ;; .emacs file, will be applied to the existing screen.
7eadab74
JB
241 (if (not (eq (cdr (or (assq 'minibuffer initial-frame-alist)
242 (assq 'minibuffer default-frame-alist)
243 '(minibuffer . t)))
244 t))
245 ;; Create the new frame.
b65e2f74
RS
246 (let (parms new)
247 ;; If the frame isn't visible yet, wait till it is.
248 ;; If the user has to position the window,
249 ;; Emacs doesn't know its real position until
250 ;; the frame is seen to be visible.
251 (while (not (cdr (assq 'visibility
252 (frame-parameters frame-initial-frame))))
253 (sleep-for 1))
06d7dff2
RS
254 (setq parms (frame-parameters frame-initial-frame))
255 ;; Get rid of `name' unless it was specified explicitly before.
256 (or (assq 'name frame-initial-frame-alist)
257 (setq parms (delq (assq 'name parms) parms)))
b65e2f74 258 (setq parms (append initial-frame-alist
6eb018ba 259 default-frame-alist
06d7dff2 260 parms
6eb018ba 261 nil))
c1e67401
RS
262 ;; Get rid of `reverse', because that was handled
263 ;; when we first made the frame.
264 (setq parms (cons '(reverse) (delq (assq 'reverse parms) parms)))
265 (if (assq 'height frame-initial-geometry-arguments)
5b66a32f 266 (setq parms (frame-delete-all 'height parms)))
c1e67401 267 (if (assq 'width frame-initial-geometry-arguments)
5b66a32f 268 (setq parms (frame-delete-all 'width parms)))
c1e67401 269 (if (assq 'left frame-initial-geometry-arguments)
5b66a32f 270 (setq parms (frame-delete-all 'left parms)))
c1e67401 271 (if (assq 'top frame-initial-geometry-arguments)
5b66a32f 272 (setq parms (frame-delete-all 'top parms)))
c1e67401
RS
273 (setq new
274 (make-frame
275 ;; Use the geometry args that created the existing
276 ;; frame, rather than the parms we get for it.
3ce3ff11
KH
277 (append frame-initial-geometry-arguments
278 '((user-size . t) (user-position . t))
279 parms)))
7eadab74
JB
280 ;; The initial frame, which we are about to delete, may be
281 ;; the only frame with a minibuffer. If it is, create a
282 ;; new one.
283 (or (delq frame-initial-frame (minibuffer-frame-list))
746bd265 284 (make-initial-minibuffer-frame nil))
7eadab74
JB
285
286 ;; If the initial frame is serving as a surrogate
287 ;; minibuffer frame for any frames, we need to wean them
288 ;; onto a new frame. The default-minibuffer-frame
289 ;; variable must be handled similarly.
290 (let ((users-of-initial
291 (filtered-frame-list
292 (function (lambda (frame)
293 (and (not (eq frame frame-initial-frame))
294 (eq (window-frame
295 (minibuffer-window frame))
296 frame-initial-frame)))))))
297 (if (or users-of-initial
298 (eq default-minibuffer-frame frame-initial-frame))
299
300 ;; Choose an appropriate frame. Prefer frames which
301 ;; are only minibuffers.
302 (let* ((new-surrogate
303 (car
304 (or (filtered-frame-list
305 (function
306 (lambda (frame)
307 (eq (cdr (assq 'minibuffer
308 (frame-parameters frame)))
309 'only))))
310 (minibuffer-frame-list))))
311 (new-minibuffer (minibuffer-window new-surrogate)))
312
313 (if (eq default-minibuffer-frame frame-initial-frame)
314 (setq default-minibuffer-frame new-surrogate))
315
316 ;; Wean the frames using frame-initial-frame as
317 ;; their minibuffer frame.
318 (mapcar
319 (function
320 (lambda (frame)
321 (modify-frame-parameters
322 frame (list (cons 'minibuffer new-minibuffer)))))
323 users-of-initial))))
ec558adc
JB
324
325 ;; Redirect events enqueued at this frame to the new frame.
326 ;; Is this a good idea?
7eadab74 327 (redirect-frame-focus frame-initial-frame new)
ec558adc 328
7eadab74 329 ;; Finally, get rid of the old frame.
0cffbbb9 330 (delete-frame frame-initial-frame t))
7eadab74
JB
331
332 ;; Otherwise, we don't need all that rigamarole; just apply
333 ;; the new parameters.
3f2f8c83
RS
334 (let (newparms allparms tail)
335 (setq allparms (append initial-frame-alist
336 default-frame-alist))
5b66a32f
RS
337 (if (assq 'height frame-initial-geometry-arguments)
338 (setq allparms (frame-delete-all 'height allparms)))
339 (if (assq 'width frame-initial-geometry-arguments)
340 (setq allparms (frame-delete-all 'width allparms)))
341 (if (assq 'left frame-initial-geometry-arguments)
342 (setq allparms (frame-delete-all 'left allparms)))
343 (if (assq 'top frame-initial-geometry-arguments)
344 (setq allparms (frame-delete-all 'top allparms)))
3f2f8c83
RS
345 (setq tail allparms)
346 ;; Find just the parms that have changed since we first
347 ;; made this frame. Those are the ones actually set by
348 ;; the init file. For those parms whose values we already knew
349 ;; (such as those spec'd by command line options)
350 ;; it is undesirable to specify the parm again
351 ;; once the user has seen the frame and been able to alter it
352 ;; manually.
353 (while tail
354 (let (newval oldval)
66de157d
RS
355 (setq oldval (assq (car (car tail))
356 frame-initial-frame-alist))
3f2f8c83 357 (setq newval (cdr (assq (car (car tail)) allparms)))
66de157d 358 (or (and oldval (eq (cdr oldval) newval))
3f2f8c83
RS
359 (setq newparms
360 (cons (cons (car (car tail)) newval) newparms))))
361 (setq tail (cdr tail)))
86e58378 362 (setq newparms (nreverse newparms))
3f2f8c83 363 (modify-frame-parameters frame-initial-frame
86e58378
RS
364 newparms)
365 (if (assq 'font newparms)
366 (frame-update-faces frame-initial-frame)))))
64c669bc 367
7eadab74
JB
368 ;; Restore the original buffer.
369 (set-buffer old-buffer)
370
371 ;; Make sure the initial frame can be GC'd if it is ever deleted.
d202f1f2
JB
372 ;; Make sure frame-notice-user-settings does nothing if called twice.
373 (setq frame-initial-frame nil)))
64c669bc 374
746bd265
KH
375(defun make-initial-minibuffer-frame (display)
376 (let ((parms (append minibuffer-frame-alist '((minibuffer . only)))))
377 (if display
378 (make-frame-on-display display parms)
379 (make-frame parms))))
380
5b66a32f
RS
381;; Delete from ALIST all elements whose car is KEY.
382;; Return the modified alist.
383(defun frame-delete-all (key alist)
9759578f 384 (setq alist (copy-sequence alist))
5b66a32f
RS
385 (let ((tail alist))
386 (while tail
387 (if (eq (car (car tail)) key)
388 (setq alist (delq (car tail) alist)))
389 (setq tail (cdr tail)))
390 alist))
64c669bc 391\f
7eadab74 392;;;; Creation of additional frames, and other frame miscellanea
dc6d9681 393
7eadab74 394;;; Return some frame other than the current frame, creating one if
eb8c3be9 395;;; necessary. Note that the minibuffer frame, if separate, is not
7eadab74 396;;; considered (see next-frame).
7253d8e0 397(defun get-other-frame ()
dc6d9681 398 (let ((s (if (equal (next-frame (selected-frame)) (selected-frame))
6eb018ba 399 (make-frame)
dc6d9681 400 (next-frame (selected-frame)))))
64c669bc
JB
401 s))
402
dc6d9681
JB
403(defun next-multiframe-window ()
404 "Select the next window, regardless of which frame it is on."
64c669bc
JB
405 (interactive)
406 (select-window (next-window (selected-window)
407 (> (minibuffer-depth) 0)
408 t)))
409
dc6d9681
JB
410(defun previous-multiframe-window ()
411 "Select the previous window, regardless of which frame it is on."
64c669bc
JB
412 (interactive)
413 (select-window (previous-window (selected-window)
414 (> (minibuffer-depth) 0)
415 t)))
416
6d73e337
RS
417(defun make-frame-on-display (display &optional parameters)
418 "Make a frame on display DISPLAY.
419The optional second argument PARAMETERS specifies additional frame parameters."
420 (interactive "sMake frame on display: ")
a9ca74cd
RS
421 (or (string-match "\\`[^:]*:[0-9]+\\(\\.[0-9]+\\)?\\'" display)
422 (error "Invalid display, not HOST:SERVER or HOST:SERVER.SCREEN"))
6d73e337
RS
423 (make-frame (cons (cons 'display display) parameters)))
424
6238bfaf
RS
425(defun make-frame-command ()
426 "Make a new frame, and select it if the terminal displays only one frame."
427 (interactive)
c1b1350b 428 (if (and window-system (not (eq window-system 'pc)))
6238bfaf
RS
429 (make-frame)
430 (select-frame (make-frame))))
431
45c4fdeb
SM
432(defvar before-make-frame-hook nil
433 "Functions to run before a frame is created.")
434
435(defvar after-make-frame-functions nil
436 "Functions to run after a frame is created.
437The functions are run with one arg, the newly created frame.")
438
92e443b1 439;; Alias, kept temporarily.
31e1d920 440(defalias 'new-frame 'make-frame)
bc93c097 441
45c4fdeb
SM
442(defun make-frame (&optional parameters)
443 "Return a newly created frame displaying the current buffer.
444Optional argument PARAMETERS is an alist of parameters for the new frame.
445Each element of PARAMETERS should have the form (NAME . VALUE), for example:
a105105a 446
45c4fdeb 447 (name . STRING) The frame should be named STRING.
43a2e52c 448
45c4fdeb
SM
449 (width . NUMBER) The frame should be NUMBER characters in width.
450 (height . NUMBER) The frame should be NUMBER text lines high.
43a2e52c 451
45c4fdeb 452You cannot specify either `width' or `height', you must use neither or both.
43a2e52c 453
45c4fdeb
SM
454 (minibuffer . t) The frame should have a minibuffer.
455 (minibuffer . nil) The frame should have no minibuffer.
456 (minibuffer . only) The frame should contain only a minibuffer.
457 (minibuffer . WINDOW) The frame should use WINDOW as its minibuffer window.
43a2e52c 458
45c4fdeb
SM
459Before the frame is created (via `frame-creation-function'), functions on the
460hook `before-make-frame-hook' are run. After the frame is created, functions
461on `after-make-frame-functions' are run with one arg, the newly created frame."
64c669bc 462 (interactive)
45c4fdeb
SM
463 (run-hooks 'before-make-frame-hook)
464 (let ((frame (funcall frame-creation-function parameters)))
465 (run-hook-with-args 'after-make-frame-functions frame)
466 frame))
64c669bc 467
7eadab74
JB
468(defun filtered-frame-list (predicate)
469 "Return a list of all live frames which satisfy PREDICATE."
470 (let ((frames (frame-list))
471 good-frames)
472 (while (consp frames)
473 (if (funcall predicate (car frames))
474 (setq good-frames (cons (car frames) good-frames)))
475 (setq frames (cdr frames)))
476 good-frames))
477
478(defun minibuffer-frame-list ()
479 "Return a list of all frames with their own minibuffers."
480 (filtered-frame-list
481 (function (lambda (frame)
482 (eq frame (window-frame (minibuffer-window frame)))))))
483
58bf6042
JB
484(defun frame-remove-geometry-params (param-list)
485 "Return the parameter list PARAM-LIST, but with geometry specs removed.
486This deletes all bindings in PARAM-LIST for `top', `left', `width',
791e09d8 487`height', `user-size' and `user-position' parameters.
58bf6042
JB
488Emacs uses this to avoid overriding explicit moves and resizings from
489the user during startup."
490 (setq param-list (cons nil param-list))
491 (let ((tail param-list))
492 (while (consp (cdr tail))
493 (if (and (consp (car (cdr tail)))
791e09d8
RS
494 (memq (car (car (cdr tail)))
495 '(height width top left user-position user-size)))
496 (progn
497 (setq frame-initial-geometry-arguments
498 (cons (car (cdr tail)) frame-initial-geometry-arguments))
499 (setcdr tail (cdr (cdr tail))))
58bf6042 500 (setq tail (cdr tail)))))
e11c3dc2
KH
501 (setq frame-initial-geometry-arguments
502 (nreverse frame-initial-geometry-arguments))
58bf6042
JB
503 (cdr param-list))
504
505
0ac18fb0
RS
506(defcustom focus-follows-mouse t
507 "*Non-nil if window system changes focus when you move the mouse."
508 :type 'boolean
cd32a7ba
DN
509 :group 'frames
510 :version "20.3")
26ef026d 511
ceab6935 512(defun other-frame (arg)
a569dbc3 513 "Select the ARG'th different visible frame, and raise it.
ceab6935
RM
514All frames are arranged in a cyclic order.
515This command selects the frame ARG steps away in that order.
516A negative ARG moves in the opposite order."
517 (interactive "p")
518 (let ((frame (selected-frame)))
519 (while (> arg 0)
a569dbc3
RM
520 (setq frame (next-frame frame))
521 (while (not (eq (frame-visible-p frame) t))
522 (setq frame (next-frame frame)))
523 (setq arg (1- arg)))
ceab6935 524 (while (< arg 0)
a569dbc3
RM
525 (setq frame (previous-frame frame))
526 (while (not (eq (frame-visible-p frame) t))
527 (setq frame (previous-frame frame)))
915cfd1f 528 (setq arg (1+ arg)))
ceab6935 529 (raise-frame frame)
699213bc 530 (select-frame frame)
9d702ce6
GV
531 ;; Ensure, if possible, that frame gets input focus.
532 (if (eq window-system 'w32)
533 (w32-focus-frame frame)
5265a842 534 (when focus-follows-mouse
26ef026d 535 (set-mouse-position (selected-frame) (1- (frame-width)) 0)))))
845cde06
EZ
536
537(defun make-frame-names-alist ()
538 (let* ((current-frame (selected-frame))
539 (falist
540 (cons
541 (cons (frame-parameter current-frame 'name) current-frame) nil))
542 (frame (next-frame nil t)))
543 (while (not (eq frame current-frame))
544 (progn
545 (setq falist (cons (cons (frame-parameter frame 'name) frame) falist))
546 (setq frame (next-frame frame t))))
547 falist))
548
549(defvar frame-name-history nil)
845cde06
EZ
550(defun select-frame-by-name (name)
551 "Select the frame whose name is NAME and raise it.
552If there is no frame by that name, signal an error."
553 (interactive
716ff1c9
RS
554 (let* ((frame-names-alist (make-frame-names-alist))
555 (default (car (car frame-names-alist)))
556 (input (completing-read
557 (format "Select Frame (default %s): " default)
558 frame-names-alist nil t nil 'frame-name-history)))
845cde06
EZ
559 (if (= (length input) 0)
560 (list default)
561 (list input))))
716ff1c9
RS
562 (let* ((frame-names-alist (make-frame-names-alist))
563 (frame (cdr (assoc name frame-names-alist))))
845cde06
EZ
564 (or frame
565 (error "There is no frame named `%s'" name))
566 (make-frame-visible frame)
567 (raise-frame frame)
568 (select-frame frame)))
64c669bc 569\f
dc6d9681
JB
570;;;; Frame configurations
571
572(defun current-frame-configuration ()
573 "Return a list describing the positions and states of all frames.
376a7584
JB
574Its car is `frame-configuration'.
575Each element of the cdr is a list of the form (FRAME ALIST WINDOW-CONFIG),
576where
577 FRAME is a frame object,
578 ALIST is an association list specifying some of FRAME's parameters, and
579 WINDOW-CONFIG is a window configuration object for FRAME."
580 (cons 'frame-configuration
581 (mapcar (function
582 (lambda (frame)
583 (list frame
584 (frame-parameters frame)
585 (current-window-configuration frame))))
586 (frame-list))))
dc6d9681 587
68cd265f 588(defun set-frame-configuration (configuration &optional nodelete)
dc6d9681
JB
589 "Restore the frames to the state described by CONFIGURATION.
590Each frame listed in CONFIGURATION has its position, size, window
68cd265f 591configuration, and other parameters set as specified in CONFIGURATION.
a78db71c
RS
592Ordinarily, this function deletes all existing frames not
593listed in CONFIGURATION. But if optional second argument NODELETE
5da841d2 594is given and non-nil, the unwanted frames are iconified instead."
376a7584
JB
595 (or (frame-configuration-p configuration)
596 (signal 'wrong-type-argument
597 (list 'frame-configuration-p configuration)))
598 (let ((config-alist (cdr configuration))
599 frames-to-delete)
06b1a5ef 600 (mapcar (function
dc6d9681 601 (lambda (frame)
376a7584 602 (let ((parameters (assq frame config-alist)))
06b1a5ef
JB
603 (if parameters
604 (progn
98e9d14b
JB
605 (modify-frame-parameters
606 frame
85557d7e 607 ;; Since we can't set a frame's minibuffer status,
98e9d14b
JB
608 ;; we might as well omit the parameter altogether.
609 (let* ((parms (nth 1 parameters))
610 (mini (assq 'minibuffer parms)))
611 (if mini (setq parms (delq mini parms)))
612 parms))
06b1a5ef 613 (set-window-configuration (nth 2 parameters)))
dc6d9681
JB
614 (setq frames-to-delete (cons frame frames-to-delete))))))
615 (frame-list))
a78db71c 616 (if nodelete
5da841d2
RS
617 ;; Note: making frames invisible here was tried
618 ;; but led to some strange behavior--each time the frame
619 ;; was made visible again, the window manager asked afresh
620 ;; for where to put it.
621 (mapcar 'iconify-frame frames-to-delete)
a78db71c 622 (mapcar 'delete-frame frames-to-delete))))
64c669bc 623\f
dc6d9681
JB
624;;;; Convenience functions for accessing and interactively changing
625;;;; frame parameters.
64c669bc 626
b5d48854 627(defun frame-parameter (frame parameter)
3196c5a7 628 "Return FRAME's value for parameter PARAMETER.
ed956b81 629If FRAME is nil, describe the currently selected frame."
3196c5a7 630 (cdr (assq parameter (frame-parameters frame))))
b5d48854 631
151bdc83 632(defun frame-height (&optional frame)
dc6d9681
JB
633 "Return number of lines available for display on FRAME.
634If FRAME is omitted, describe the currently selected frame."
151bdc83 635 (cdr (assq 'height (frame-parameters frame))))
dc6d9681
JB
636
637(defun frame-width (&optional frame)
638 "Return number of columns available for display on FRAME.
639If FRAME is omitted, describe the currently selected frame."
151bdc83 640 (cdr (assq 'width (frame-parameters frame))))
dc6d9681 641
889611f0
RS
642(defalias 'set-default-font 'set-frame-font)
643(defun set-frame-font (font-name)
7eadab74 644 "Set the font of the selected frame to FONT.
61298010
RS
645When called interactively, prompt for the name of the font to use.
646To get the frame's current default font, use `frame-parameters'."
64c669bc 647 (interactive "sFont name: ")
dc6d9681 648 (modify-frame-parameters (selected-frame)
4033f46b
RS
649 (list (cons 'font font-name)))
650 ;; Update faces that want a bold or italic version of the default font.
651 (frame-update-faces (selected-frame)))
64c669bc 652
8290babd 653(defun set-background-color (color-name)
7eadab74 654 "Set the background color of the selected frame to COLOR.
61298010
RS
655When called interactively, prompt for the name of the color to use.
656To get the frame's current background color, use `frame-parameters'."
64c669bc 657 (interactive "sColor: ")
dc6d9681 658 (modify-frame-parameters (selected-frame)
1787c22a
RS
659 (list (cons 'background-color color-name)))
660 (frame-update-face-colors (selected-frame)))
64c669bc 661
8290babd 662(defun set-foreground-color (color-name)
7eadab74 663 "Set the foreground color of the selected frame to COLOR.
61298010
RS
664When called interactively, prompt for the name of the color to use.
665To get the frame's current foreground color, use `frame-parameters'."
64c669bc 666 (interactive "sColor: ")
dc6d9681 667 (modify-frame-parameters (selected-frame)
1787c22a
RS
668 (list (cons 'foreground-color color-name)))
669 (frame-update-face-colors (selected-frame)))
64c669bc
JB
670
671(defun set-cursor-color (color-name)
7eadab74 672 "Set the text cursor color of the selected frame to COLOR.
61298010
RS
673When called interactively, prompt for the name of the color to use.
674To get the frame's current cursor color, use `frame-parameters'."
64c669bc 675 (interactive "sColor: ")
dc6d9681 676 (modify-frame-parameters (selected-frame)
7eadab74 677 (list (cons 'cursor-color color-name))))
64c669bc 678
eaa974e1 679(defun set-mouse-color (color-name)
7eadab74 680 "Set the color of the mouse pointer of the selected frame to COLOR.
61298010
RS
681When called interactively, prompt for the name of the color to use.
682To get the frame's current mouse color, use `frame-parameters'."
64c669bc 683 (interactive "sColor: ")
dc6d9681 684 (modify-frame-parameters (selected-frame)
7eadab74
JB
685 (list (cons 'mouse-color color-name))))
686
eaa974e1
JB
687(defun set-border-color (color-name)
688 "Set the color of the border of the selected frame to COLOR.
61298010
RS
689When called interactively, prompt for the name of the color to use.
690To get the frame's current border color, use `frame-parameters'."
eaa974e1
JB
691 (interactive "sColor: ")
692 (modify-frame-parameters (selected-frame)
693 (list (cons 'border-color color-name))))
694
695(defun auto-raise-mode (arg)
7eadab74 696 "Toggle whether or not the selected frame should auto-raise.
79e6ae33
RS
697With arg, turn auto-raise mode on if and only if arg is positive.
698Note that this controls Emacs's own auto-raise feature.
699Some window managers allow you to enable auto-raise for certain windows.
700You can use that for Emacs windows if you wish, but if you do,
701that is beyond the control of Emacs and this command has no effect on it."
7eadab74
JB
702 (interactive "P")
703 (if (null arg)
704 (setq arg
705 (if (cdr (assq 'auto-raise (frame-parameters (selected-frame))))
706 -1 1)))
dc6d9681 707 (modify-frame-parameters (selected-frame)
7eadab74
JB
708 (list (cons 'auto-raise (> arg 0)))))
709
eaa974e1 710(defun auto-lower-mode (arg)
7eadab74 711 "Toggle whether or not the selected frame should auto-lower.
79e6ae33
RS
712With arg, turn auto-lower mode on if and only if arg is positive.
713Note that this controls Emacs's own auto-lower feature.
714Some window managers allow you to enable auto-lower for certain windows.
715You can use that for Emacs windows if you wish, but if you do,
716that is beyond the control of Emacs and this command has no effect on it."
7eadab74
JB
717 (interactive "P")
718 (if (null arg)
719 (setq arg
720 (if (cdr (assq 'auto-lower (frame-parameters (selected-frame))))
721 -1 1)))
dc6d9681 722 (modify-frame-parameters (selected-frame)
7eadab74 723 (list (cons 'auto-lower (> arg 0)))))
7777d03b
EZ
724(defun set-frame-name (name)
725 "Set the name of the selected frame to NAME.
726When called interactively, prompt for the name of the frame.
727The frame name is displayed on the modeline if the terminal displays only
728one frame, otherwise the name is displayed on the frame's caption bar."
729 (interactive "sFrame name: ")
730 (modify-frame-parameters (selected-frame)
731 (list (cons 'name name))))
64c669bc 732\f
dc6d9681 733;;;; Aliases for backward compatibility with Emacs 18.
31e1d920
ER
734(defalias 'screen-height 'frame-height)
735(defalias 'screen-width 'frame-width)
9e2b097b
JB
736
737(defun set-screen-width (cols &optional pretend)
738 "Obsolete function to change the size of the screen to COLS columns.\n\
739Optional second arg non-nil means that redisplay should use COLS columns\n\
740but that the idea of the actual width of the frame should not be changed.\n\
741This function is provided only for compatibility with Emacs 18; new code\n\
fe668515 742should use `set-frame-width instead'."
9e2b097b
JB
743 (set-frame-width (selected-frame) cols pretend))
744
745(defun set-screen-height (lines &optional pretend)
746 "Obsolete function to change the height of the screen to LINES lines.\n\
747Optional second arg non-nil means that redisplay should use LINES lines\n\
748but that the idea of the actual height of the screen should not be changed.\n\
749This function is provided only for compatibility with Emacs 18; new code\n\
ef5a6345 750should use `set-frame-height' instead."
9e2b097b
JB
751 (set-frame-height (selected-frame) lines pretend))
752
753(make-obsolete 'screen-height 'frame-height)
754(make-obsolete 'screen-width 'frame-width)
755(make-obsolete 'set-screen-width 'set-frame-width)
756(make-obsolete 'set-screen-height 'set-frame-height)
dc6d9681
JB
757
758\f
64c669bc 759;;;; Key bindings
64c669bc 760
6238bfaf 761(define-key ctl-x-5-map "2" 'make-frame-command)
dc6d9681 762(define-key ctl-x-5-map "0" 'delete-frame)
ceab6935 763(define-key ctl-x-5-map "o" 'other-frame)
49116ac0 764
dc6d9681 765(provide 'frame)
c88ab9ce 766
dc6d9681 767;;; frame.el ends here