(Fx_focus_frame): Rename from Fw32_focus_frame.
[bpt/emacs.git] / lisp / frame.el
CommitLineData
55535639 1;;; frame.el --- multi-frame management independent of window systems
c88ab9ce 2
0d30b337 3;; Copyright (C) 1993, 1994, 1996, 1997, 2000, 2001, 2002, 2003,
d7a0267c 4;; 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
3a801d0c 5
fc68affa 6;; Maintainer: FSF
fd7fa35a 7;; Keywords: internal
fc68affa 8
b578f267
EN
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
b4aa6026 13;; the Free Software Foundation; either version 3, or (at your option)
b578f267
EN
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
64c669bc 25
55535639
PJ
26;;; Commentary:
27
fc68affa
ER
28;;; Code:
29
2246281f
KL
30(defvar frame-creation-function-alist
31 (list (cons nil
32 (if (fboundp 'tty-create-frame-with-faces)
33 'tty-create-frame-with-faces
34 (function
35 (lambda (parameters)
36 (error "Can't create multiple frames without a window system"))))))
37 "Alist of window-system dependent functions to call to create a new frame.
38The window system startup file should add its frame creation
39function to this list, which should take an alist of parameters
40as its argument.")
114a8b8c 41
bf1ba39f
KL
42(defvar window-system-default-frame-alist nil
43 "Alist of window-system dependent default frame parameters.
095fe281 44You can set this in your `.emacs' file; for example,
bf1ba39f
KL
45
46 ;; Disable menubar and toolbar on the console, but enable them under X.
47 (setq window-system-default-frame-alist
48 '((x (menu-bar-lines . 1) (tool-bar-lines . 1))
49 (nil (menu-bar-lines . 0) (tool-bar-lines . 0))))
50
095fe281 51Parameters specified here supersede the values given in `default-frame-alist'.")
bf1ba39f 52
7e573c4a
SM
53;; The initial value given here used to ask for a minibuffer.
54;; But that's not necessary, because the default is to have one.
55;; By not specifying it here, we let an X resource specify it.
0c966f88 56(defcustom initial-frame-alist nil
0b9d32af 57 "*Alist of frame parameters for creating the initial X window frame.
eaa974e1 58You can set this in your `.emacs' file; for example,
dc6d9681 59 (setq initial-frame-alist '((top . 1) (left . 1) (width . 80) (height . 55)))
de17da15
RS
60Parameters specified here supersede the values given in `default-frame-alist'.
61
62If the value calls for a frame without a minibuffer, and you have not created
63a minibuffer frame on your own, one is created according to
7eadab74 64`minibuffer-frame-alist'.
de17da15
RS
65
66You can specify geometry-related options for just the initial frame
67by setting this variable in your `.emacs' file; however, they won't
85557d7e 68take effect until Emacs reads `.emacs', which happens after first creating
de17da15
RS
69the frame. If you want the frame to have the proper geometry as soon
70as it appears, you need to use this three-step process:
71* Specify X resources to give the geometry you want.
72* Set `default-frame-alist' to override these options so that they
73 don't affect subsequent frames.
74* Set `initial-frame-alist' in a way that matches the X resources,
0c966f88
RS
75 to override what you put in `default-frame-alist'."
76 :type '(repeat (cons :format "%v"
77 (symbol :tag "Parameter")
78 (sexp :tag "Value")))
79 :group 'frames)
64c669bc 80
0b9d32af
RS
81(defcustom minibuffer-frame-alist '((width . 80) (height . 2))
82 "*Alist of frame parameters for initially creating a minibuffer frame.
eaa974e1 83You can set this in your `.emacs' file; for example,
dc6d9681 84 (setq minibuffer-frame-alist
7eadab74 85 '((top . 1) (left . 1) (width . 80) (height . 2)))
eaa974e1 86Parameters specified here supersede the values given in
0b9d32af
RS
87`default-frame-alist', for a minibuffer frame."
88 :type '(repeat (cons :format "%v"
89 (symbol :tag "Parameter")
90 (sexp :tag "Value")))
91 :group 'frames)
64c669bc 92
0b9d32af
RS
93(defcustom pop-up-frame-alist nil
94 "*Alist of frame parameters used when creating pop-up frames.
dc6d9681 95Pop-up frames are used for completions, help, and the like.
64c669bc 96This variable can be set in your init file, like this:
dc6d9681 97 (setq pop-up-frame-alist '((width . 80) (height . 20)))
0b9d32af
RS
98These supersede the values given in `default-frame-alist',
99for pop-up frames."
100 :type '(repeat (cons :format "%v"
101 (symbol :tag "Parameter")
102 (sexp :tag "Value")))
103 :group 'frames)
64c669bc 104
dc6d9681 105(setq pop-up-frame-function
7f18ce22
RS
106 ;; Using `function' here caused some sort of problem.
107 '(lambda ()
108 (make-frame pop-up-frame-alist)))
64c669bc 109
30e19aee 110(defcustom special-display-frame-alist
8a9e86e6
RS
111 '((height . 14) (width . 80) (unsplittable . t))
112 "*Alist of frame parameters used when creating special frames.
113Special frames are used for buffers whose names are in
114`special-display-buffer-names' and for buffers whose names match
115one of the regular expressions in `special-display-regexps'.
116This variable can be set in your init file, like this:
117 (setq special-display-frame-alist '((width . 80) (height . 20)))
30e19aee
RS
118These supersede the values given in `default-frame-alist'."
119 :type '(repeat (cons :format "%v"
120 (symbol :tag "Parameter")
121 (sexp :tag "Value")))
122 :group 'frames)
8a9e86e6 123
95e6cf39 124(defun special-display-popup-frame (buffer &optional args)
40d34803
DL
125 "Display BUFFER in its own frame, reusing an existing window if any.
126Return the window chosen.
127Currently we do not insist on selecting the window within its frame.
128If ARGS is an alist, use it as a list of frame parameter specs.
129If ARGS is a list whose car is a symbol,
130use (car ARGS) as a function to do the work.
131Pass it BUFFER as first arg, and (cdr ARGS) gives the rest of the args."
95e6cf39
RS
132 (if (and args (symbolp (car args)))
133 (apply (car args) buffer (cdr args))
bb2c34b1 134 (let ((window (get-buffer-window buffer 0)))
7e573c4a
SM
135 (or
136 ;; If we have a window already, make it visible.
137 (when window
138 (let ((frame (window-frame window)))
139 (make-frame-visible frame)
140 (raise-frame frame)
141 window))
142 ;; Reuse the current window if the user requested it.
143 (when (cdr (assq 'same-window args))
144 (condition-case nil
145 (progn (switch-to-buffer buffer) (selected-window))
146 (error nil)))
147 ;; Stay on the same frame if requested.
148 (when (or (cdr (assq 'same-frame args)) (cdr (assq 'same-window args)))
149 (let* ((pop-up-frames nil) (pop-up-windows t)
150 special-display-regexps special-display-buffer-names
151 (window (display-buffer buffer)))
bb2c34b1 152 ;; Only do it if this is a new window:
7e573c4a
SM
153 ;; (set-window-dedicated-p window t)
154 window))
155 ;; If no window yet, make one in a new frame.
bf247b6e 156 (let ((frame
86bd482f
RS
157 (with-current-buffer buffer
158 (make-frame (append args special-display-frame-alist)))))
7e573c4a
SM
159 (set-window-buffer (frame-selected-window frame) buffer)
160 (set-window-dedicated-p (frame-selected-window frame) t)
161 (frame-selected-window frame))))))
8a9e86e6 162
924be53a 163(defun handle-delete-frame (event)
40d34803 164 "Handle delete-frame events from the X server."
924be53a
RS
165 (interactive "e")
166 (let ((frame (posn-window (event-start event)))
167 (i 0)
168 (tail (frame-list)))
169 (while tail
170 (and (frame-visible-p (car tail))
171 (not (eq (car tail) frame))
172 (setq i (1+ i)))
173 (setq tail (cdr tail)))
174 (if (> i 0)
175 (delete-frame frame t)
a9562d19
RS
176 ;; Gildea@x.org says it is ok to ask questions before terminating.
177 (save-buffers-kill-emacs))))
64c669bc 178\f
dc6d9681 179;;;; Arrangement of frames at startup
64c669bc 180
7e573c4a
SM
181;; 1) Load the window system startup file from the lisp library and read the
182;; high-priority arguments (-q and the like). The window system startup
183;; file should create any frames specified in the window system defaults.
184;;
185;; 2) If no frames have been opened, we open an initial text frame.
186;;
187;; 3) Once the init file is done, we apply any newly set parameters
188;; in initial-frame-alist to the frame.
64c669bc 189
85557d7e 190;; These are now called explicitly at the proper times,
fc4d4afb
RS
191;; since that is easier to understand.
192;; Actually using hooks within Emacs is bad for future maintenance. --rms.
193;; (add-hook 'before-init-hook 'frame-initialize)
194;; (add-hook 'window-setup-hook 'frame-notice-user-settings)
64c669bc 195
7e573c4a 196;; If we create the initial frame, this is it.
dc6d9681 197(defvar frame-initial-frame nil)
64c669bc 198
3f2f8c83
RS
199;; Record the parameters used in frame-initialize to make the initial frame.
200(defvar frame-initial-frame-alist)
201
791e09d8
RS
202(defvar frame-initial-geometry-arguments nil)
203
7e573c4a
SM
204;; startup.el calls this function before loading the user's init
205;; file - if there is no frame with a minibuffer open now, create
206;; one to display messages while loading the init file.
dc6d9681 207(defun frame-initialize ()
ff2a1c79 208 "Create an initial frame if necessary."
64c669bc 209 ;; Are we actually running under a window system at all?
2246281f
KL
210 (if (and initial-window-system
211 (not noninteractive)
212 (not (eq initial-window-system 'pc)))
7eadab74 213 (progn
62642af9
RS
214 ;; Turn on special-display processing only if there's a window system.
215 (setq special-display-function 'special-display-popup-frame)
216
7eadab74
JB
217 ;; If there is no frame with a minibuffer besides the terminal
218 ;; frame, then we need to create the opening frame. Make sure
219 ;; it has a minibuffer, but let initial-frame-alist omit the
220 ;; minibuffer spec.
221 (or (delq terminal-frame (minibuffer-frame-list))
36fc9c9f 222 (progn
3f2f8c83 223 (setq frame-initial-frame-alist
9688894d 224 (append initial-frame-alist default-frame-alist nil))
a8bb5882
RS
225 (or (assq 'horizontal-scroll-bars frame-initial-frame-alist)
226 (setq frame-initial-frame-alist
227 (cons '(horizontal-scroll-bars . t)
228 frame-initial-frame-alist)))
2246281f
KL
229 (setq frame-initial-frame-alist
230 (cons (cons 'window-system initial-window-system)
231 frame-initial-frame-alist))
36fc9c9f
RS
232 (setq default-minibuffer-frame
233 (setq frame-initial-frame
96a84970 234 (make-frame frame-initial-frame-alist)))
11281034
RS
235 ;; Delete any specifications for window geometry parameters
236 ;; so that we won't reapply them in frame-notice-user-settings.
237 ;; It would be wrong to reapply them then,
238 ;; because that would override explicit user resizing.
58bf6042 239 (setq initial-frame-alist
c2079f0a 240 (frame-remove-geometry-params initial-frame-alist))))
a13f8f50
KL
241 ;; Copy the environment of the Emacs process into the new frame.
242 (set-frame-parameter frame-initial-frame 'environment
243 (frame-parameter terminal-frame 'environment))
de87fb59
DN
244 (set-frame-parameter frame-initial-frame 'display-environment-variable
245 (getenv "DISPLAY"))
85557d7e 246 ;; At this point, we know that we have a frame open, so we
dc6d9681
JB
247 ;; can delete the terminal frame.
248 (delete-frame terminal-frame)
2246281f 249 (setq terminal-frame nil))))
85557d7e 250
8f2a992b
GM
251(defvar frame-notice-user-settings t
252 "Non-nil means function `frame-notice-user-settings' wasn't run yet.")
253
7e573c4a
SM
254;; startup.el calls this function after loading the user's init
255;; file. Now default-frame-alist and initial-frame-alist contain
256;; information to which we must react; do what needs to be done.
dc6d9681 257(defun frame-notice-user-settings ()
40d34803 258 "Act on user's init file settings of frame parameters.
0e1bfc78
KL
259React to settings of `initial-frame-alist',
260`window-system-default-frame-alist' and `default-frame-alist'
261there (in decreasing order of priority)."
2ffa6186 262 ;; Make menu-bar-mode and default-frame-alist consistent.
61f2bcd7
GM
263 (when (boundp 'menu-bar-mode)
264 (let ((default (assq 'menu-bar-lines default-frame-alist)))
265 (if default
266 (setq menu-bar-mode (not (eq (cdr default) 0)))
267 (setq default-frame-alist
268 (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0))
269 default-frame-alist)))))
f39784dd 270
33a33c06
GM
271 ;; Make tool-bar-mode and default-frame-alist consistent. Don't do
272 ;; it in batch mode since that would leave a tool-bar-lines
273 ;; parameter in default-frame-alist in a dumped Emacs, which is not
9688894d 274 ;; what we want.
33a33c06 275 (when (and (boundp 'tool-bar-mode)
095fe281 276 (not noninteractive))
61f2bcd7
GM
277 (let ((default (assq 'tool-bar-lines default-frame-alist)))
278 (if default
095fe281
KL
279 (setq tool-bar-mode (not (eq (cdr default) 0)))
280 ;; If Emacs was started on a tty, changing default-frame-alist
281 ;; would disable the toolbar on X frames created later. We
282 ;; want to keep the default of showing a toolbar under X even
283 ;; in this case.
284 ;;
285 ;; If the user explicitly called `tool-bar-mode' in .emacs,
286 ;; then default-frame-alist is already changed anyway.
287 (when initial-window-system
288 (setq default-frame-alist
289 (cons (cons 'tool-bar-lines (if tool-bar-mode 1 0))
290 default-frame-alist))))))
2ffa6186 291
7eadab74
JB
292 ;; Creating and deleting frames may shift the selected frame around,
293 ;; and thus the current buffer. Protect against that. We don't
294 ;; want to use save-excursion here, because that may also try to set
295 ;; the buffer of the selected window, which fails when the selected
296 ;; window is the minibuffer.
0e1bfc78
KL
297 (let ((old-buffer (current-buffer))
298 (window-system-frame-alist (cdr (assq initial-window-system
299 window-system-default-frame-alist))))
7eadab74 300
8f2a992b
GM
301 (when (and frame-notice-user-settings
302 (null frame-initial-frame))
42002db5
EZ
303 ;; This case happens when we don't have a window system, and
304 ;; also for MS-DOS frames.
8f2a992b
GM
305 (let ((parms (frame-parameters frame-initial-frame)))
306 ;; Don't change the frame names.
307 (setq parms (delq (assq 'name parms) parms))
308 ;; Can't modify the minibuffer parameter, so don't try.
309 (setq parms (delq (assq 'minibuffer parms) parms))
310 (modify-frame-parameters nil
2246281f 311 (if (null initial-window-system)
42002db5 312 (append initial-frame-alist
0e1bfc78 313 window-system-frame-alist
42002db5
EZ
314 default-frame-alist
315 parms
316 nil)
317 ;; initial-frame-alist and
318 ;; default-frame-alist were already
319 ;; applied in pc-win.el.
320 parms))
2246281f 321 (if (null initial-window-system) ;; MS-DOS does this differently in pc-win.el
42002db5
EZ
322 (let ((newparms (frame-parameters))
323 (frame (selected-frame)))
324 (tty-handle-reverse-video frame newparms)
325 ;; If we changed the background color, we need to update
326 ;; the background-mode parameter, and maybe some faces,
327 ;; too.
328 (when (assq 'background-color newparms)
329 (unless (or (assq 'background-mode initial-frame-alist)
330 (assq 'background-mode default-frame-alist))
331 (frame-set-background-mode frame))
332 (face-set-after-frame-default frame))))))
8f2a992b 333
7eadab74
JB
334 ;; If the initial frame is still around, apply initial-frame-alist
335 ;; and default-frame-alist to it.
9688894d
GM
336 (when (frame-live-p frame-initial-frame)
337
338 ;; When tool-bar has been switched off, correct the frame size
339 ;; by the lines added in x-create-frame for the tool-bar and
340 ;; switch `tool-bar-mode' off.
085ef9b3
GM
341 (when (display-graphic-p)
342 (let ((tool-bar-lines (or (assq 'tool-bar-lines initial-frame-alist)
0e1bfc78 343 (assq 'tool-bar-lines window-system-frame-alist)
085ef9b3 344 (assq 'tool-bar-lines default-frame-alist))))
5c39a60f
JR
345 (when (and tool-bar-originally-present
346 (or (null tool-bar-lines)
347 (null (cdr tool-bar-lines))
348 (eq 0 (cdr tool-bar-lines))))
085ef9b3 349 (let* ((char-height (frame-char-height frame-initial-frame))
5c39a60f 350 (image-height tool-bar-images-pixel-height)
085ef9b3
GM
351 (margin (cond ((and (consp tool-bar-button-margin)
352 (integerp (cdr tool-bar-button-margin))
353 (> tool-bar-button-margin 0))
354 (cdr tool-bar-button-margin))
355 ((and (integerp tool-bar-button-margin)
356 (> tool-bar-button-margin 0))
357 tool-bar-button-margin)
358 (t 0)))
359 (relief (if (and (integerp tool-bar-button-relief)
360 (> tool-bar-button-relief 0))
361 tool-bar-button-relief 3))
f39784dd 362 (lines (/ (+ image-height
085ef9b3
GM
363 (* 2 margin)
364 (* 2 relief)
365 (1- char-height))
366 char-height))
367 (height (frame-parameter frame-initial-frame 'height))
368 (newparms (list (cons 'height (- height lines))))
f39784dd 369 (initial-top (cdr (assq 'top
085ef9b3
GM
370 frame-initial-geometry-arguments)))
371 (top (frame-parameter frame-initial-frame 'top)))
372 (when (and (consp initial-top) (eq '- (car initial-top)))
7f18ce22
RS
373 (let ((adjusted-top
374 (cond ((and (consp top)
375 (eq '+ (car top)))
376 (list '+
377 (+ (cadr top)
378 (* lines char-height))))
379 ((and (consp top)
380 (eq '- (car top)))
381 (list '-
382 (- (cadr top)
383 (* lines char-height))))
384 (t (+ top (* lines char-height))))))
385 (setq newparms
386 (append newparms
387 `((top . ,adjusted-top))
388 nil))))
085ef9b3
GM
389 (modify-frame-parameters frame-initial-frame newparms)
390 (tool-bar-mode -1)))))
9688894d
GM
391
392 ;; The initial frame we create above always has a minibuffer.
393 ;; If the user wants to remove it, or make it a minibuffer-only
394 ;; frame, then we'll have to delete the current frame and make a
395 ;; new one; you can't remove or add a root window to/from an
396 ;; existing frame.
397 ;;
398 ;; NOTE: default-frame-alist was nil when we created the
399 ;; existing frame. We need to explicitly include
400 ;; default-frame-alist in the parameters of the screen we
401 ;; create here, so that its new value, gleaned from the user's
402 ;; .emacs file, will be applied to the existing screen.
de2f5dbe 403 (if (not (eq (cdr (or (assq 'minibuffer initial-frame-alist)
0e1bfc78 404 (assq 'minibuffer window-system-frame-alist)
de2f5dbe
GM
405 (assq 'minibuffer default-frame-alist)
406 '(minibuffer . t)))
407 t))
408 ;; Create the new frame.
409 (let (parms new)
410 ;; If the frame isn't visible yet, wait till it is.
411 ;; If the user has to position the window,
412 ;; Emacs doesn't know its real position until
413 ;; the frame is seen to be visible.
414 (while (not (cdr (assq 'visibility
415 (frame-parameters frame-initial-frame))))
416 (sleep-for 1))
417 (setq parms (frame-parameters frame-initial-frame))
9688894d 418
f39784dd 419 ;; Get rid of `name' unless it was specified explicitly before.
de2f5dbe
GM
420 (or (assq 'name frame-initial-frame-alist)
421 (setq parms (delq (assq 'name parms) parms)))
422
423 (setq parms (append initial-frame-alist
0e1bfc78 424 window-system-frame-alist
de2f5dbe
GM
425 default-frame-alist
426 parms
427 nil))
428
429 ;; Get rid of `reverse', because that was handled
430 ;; when we first made the frame.
431 (setq parms (cons '(reverse) (delq (assq 'reverse parms) parms)))
432
433 (if (assq 'height frame-initial-geometry-arguments)
434 (setq parms (assq-delete-all 'height parms)))
435 (if (assq 'width frame-initial-geometry-arguments)
436 (setq parms (assq-delete-all 'width parms)))
437 (if (assq 'left frame-initial-geometry-arguments)
438 (setq parms (assq-delete-all 'left parms)))
439 (if (assq 'top frame-initial-geometry-arguments)
440 (setq parms (assq-delete-all 'top parms)))
441 (setq new
442 (make-frame
443 ;; Use the geometry args that created the existing
444 ;; frame, rather than the parms we get for it.
445 (append frame-initial-geometry-arguments
446 '((user-size . t) (user-position . t))
447 parms)))
448 ;; The initial frame, which we are about to delete, may be
449 ;; the only frame with a minibuffer. If it is, create a
450 ;; new one.
451 (or (delq frame-initial-frame (minibuffer-frame-list))
452 (make-initial-minibuffer-frame nil))
453
454 ;; If the initial frame is serving as a surrogate
455 ;; minibuffer frame for any frames, we need to wean them
456 ;; onto a new frame. The default-minibuffer-frame
457 ;; variable must be handled similarly.
458 (let ((users-of-initial
459 (filtered-frame-list
b7de6024
SM
460 (lambda (frame)
461 (and (not (eq frame frame-initial-frame))
462 (eq (window-frame
463 (minibuffer-window frame))
464 frame-initial-frame))))))
465 (if (or users-of-initial
de2f5dbe
GM
466 (eq default-minibuffer-frame frame-initial-frame))
467
468 ;; Choose an appropriate frame. Prefer frames which
469 ;; are only minibuffers.
470 (let* ((new-surrogate
471 (car
472 (or (filtered-frame-list
b7de6024
SM
473 (lambda (frame)
474 (eq (cdr (assq 'minibuffer
475 (frame-parameters frame)))
476 'only)))
de2f5dbe
GM
477 (minibuffer-frame-list))))
478 (new-minibuffer (minibuffer-window new-surrogate)))
479
480 (if (eq default-minibuffer-frame frame-initial-frame)
481 (setq default-minibuffer-frame new-surrogate))
482
483 ;; Wean the frames using frame-initial-frame as
484 ;; their minibuffer frame.
b7de6024
SM
485 (dolist (frame users-of-initial)
486 (modify-frame-parameters
487 frame (list (cons 'minibuffer new-minibuffer)))))))
488
489 ;; Redirect events enqueued at this frame to the new frame.
de2f5dbe
GM
490 ;; Is this a good idea?
491 (redirect-frame-focus frame-initial-frame new)
492
493 ;; Finally, get rid of the old frame.
494 (delete-frame frame-initial-frame t))
9688894d
GM
495
496 ;; Otherwise, we don't need all that rigamarole; just apply
497 ;; the new parameters.
498 (let (newparms allparms tail)
499 (setq allparms (append initial-frame-alist
0e1bfc78 500 window-system-frame-alist
9688894d
GM
501 default-frame-alist nil))
502 (if (assq 'height frame-initial-geometry-arguments)
503 (setq allparms (assq-delete-all 'height allparms)))
504 (if (assq 'width frame-initial-geometry-arguments)
505 (setq allparms (assq-delete-all 'width allparms)))
506 (if (assq 'left frame-initial-geometry-arguments)
507 (setq allparms (assq-delete-all 'left allparms)))
508 (if (assq 'top frame-initial-geometry-arguments)
509 (setq allparms (assq-delete-all 'top allparms)))
510 (setq tail allparms)
511 ;; Find just the parms that have changed since we first
512 ;; made this frame. Those are the ones actually set by
f39784dd 513 ;; the init file. For those parms whose values we already knew
9688894d
GM
514 ;; (such as those spec'd by command line options)
515 ;; it is undesirable to specify the parm again
f39784dd 516 ;; once the user has seen the frame and been able to alter it
9688894d
GM
517 ;; manually.
518 (while tail
519 (let (newval oldval)
520 (setq oldval (assq (car (car tail))
521 frame-initial-frame-alist))
522 (setq newval (cdr (assq (car (car tail)) allparms)))
523 (or (and oldval (eq (cdr oldval) newval))
524 (setq newparms
525 (cons (cons (car (car tail)) newval) newparms))))
526 (setq tail (cdr tail)))
527 (setq newparms (nreverse newparms))
528 (modify-frame-parameters frame-initial-frame
529 newparms)
530 ;; If we changed the background color,
531 ;; we need to update the background-mode parameter
532 ;; and maybe some faces too.
533 (when (assq 'background-color newparms)
534 (unless (assq 'background-mode newparms)
535 (frame-set-background-mode frame-initial-frame))
536 (face-set-after-frame-default frame-initial-frame)))))
64c669bc 537
7eadab74
JB
538 ;; Restore the original buffer.
539 (set-buffer old-buffer)
540
541 ;; Make sure the initial frame can be GC'd if it is ever deleted.
d202f1f2 542 ;; Make sure frame-notice-user-settings does nothing if called twice.
8f2a992b 543 (setq frame-notice-user-settings nil)
d202f1f2 544 (setq frame-initial-frame nil)))
64c669bc 545
746bd265
KH
546(defun make-initial-minibuffer-frame (display)
547 (let ((parms (append minibuffer-frame-alist '((minibuffer . only)))))
548 (if display
549 (make-frame-on-display display parms)
550 (make-frame parms))))
551
7eadab74 552;;;; Creation of additional frames, and other frame miscellanea
dc6d9681 553
63019bad 554(defun modify-all-frames-parameters (alist)
d398de43 555 "Modify all current and future frames' parameters according to ALIST.
63019bad 556This changes `default-frame-alist' and possibly `initial-frame-alist'.
095fe281
KL
557Furthermore, this function removes all parameters in ALIST from
558`window-system-default-frame-alist'.
63019bad 559See help of `modify-frame-parameters' for more information."
095fe281
KL
560 (dolist (frame (frame-list))
561 (modify-frame-parameters frame alist))
562
563 (dolist (pair alist) ;; conses to add/replace
564 ;; initial-frame-alist needs setting only when
565 ;; frame-notice-user-settings is true.
566 (and frame-notice-user-settings
567 (setq initial-frame-alist
568 (assq-delete-all (car pair) initial-frame-alist)))
569 (setq default-frame-alist
570 (assq-delete-all (car pair) default-frame-alist))
571 ;; Remove any similar settings from the window-system specific
572 ;; parameters---they would override default-frame-alist.
573 (dolist (w window-system-default-frame-alist)
574 (setcdr w (assq-delete-all (car pair) (cdr w)))))
575
63019bad
JB
576 (and frame-notice-user-settings
577 (setq initial-frame-alist (append initial-frame-alist alist)))
578 (setq default-frame-alist (append default-frame-alist alist)))
579
7253d8e0 580(defun get-other-frame ()
40d34803
DL
581 "Return some frame other than the current frame.
582Create one if necessary. Note that the minibuffer frame, if separate,
583is not considered (see `next-frame')."
dc6d9681 584 (let ((s (if (equal (next-frame (selected-frame)) (selected-frame))
6eb018ba 585 (make-frame)
dc6d9681 586 (next-frame (selected-frame)))))
64c669bc
JB
587 s))
588
dc6d9681
JB
589(defun next-multiframe-window ()
590 "Select the next window, regardless of which frame it is on."
64c669bc
JB
591 (interactive)
592 (select-window (next-window (selected-window)
593 (> (minibuffer-depth) 0)
bb2c34b1 594 0))
030f4537 595 (select-frame-set-input-focus (selected-frame)))
64c669bc 596
dc6d9681
JB
597(defun previous-multiframe-window ()
598 "Select the previous window, regardless of which frame it is on."
64c669bc
JB
599 (interactive)
600 (select-window (previous-window (selected-window)
601 (> (minibuffer-depth) 0)
bb2c34b1 602 0))
030f4537 603 (select-frame-set-input-focus (selected-frame)))
64c669bc 604
6d73e337 605(defun make-frame-on-display (display &optional parameters)
b6660415 606 "Make a frame on X display DISPLAY.
6d73e337
RS
607The optional second argument PARAMETERS specifies additional frame parameters."
608 (interactive "sMake frame on display: ")
a9ca74cd
RS
609 (or (string-match "\\`[^:]*:[0-9]+\\(\\.[0-9]+\\)?\\'" display)
610 (error "Invalid display, not HOST:SERVER or HOST:SERVER.SCREEN"))
6afdd335 611 (when (and (boundp 'x-initialized) (not x-initialized))
e9cda827
KL
612 (setq x-display-name display)
613 (x-initialize-window-system))
2246281f 614 (make-frame `((window-system . x) (display . ,display) . ,parameters)))
6d73e337 615
f35ca2fe
KL
616(defun make-frame-on-tty (tty type &optional parameters)
617 "Make a frame on terminal device TTY.
618TTY should be the file name of the tty device to use. TYPE
619should be the terminal type string of TTY, for example \"xterm\"
620or \"vt100\". The optional third argument PARAMETERS specifies
621additional frame parameters."
428a555e 622 (interactive "fOpen frame on tty device: \nsTerminal type of %s: ")
f35ca2fe 623 (unless tty
fca177d4
KL
624 (error "Invalid terminal device"))
625 (unless type
626 (error "Invalid terminal type"))
f35ca2fe 627 (make-frame `((window-system . nil) (tty . ,tty) (tty-type . ,type) . ,parameters)))
fca177d4 628
b7de6024
SM
629(defun close-display-connection (display)
630 "Close the connection to a display, deleting all its associated frames.
631For DISPLAY, specify either a frame or a display name (a string).
632If DISPLAY is nil, that stands for the selected frame's display."
633 (interactive
634 (list
635 (let* ((default (frame-parameter nil 'display))
636 (display (completing-read
637 (format "Close display (default %s): " default)
638 (delete-dups
639 (mapcar (lambda (frame)
640 (frame-parameter frame 'display))
641 (frame-list)))
642 nil t nil nil
643 default)))
644 (if (zerop (length display)) default display))))
645 (let ((frames (delq nil
646 (mapcar (lambda (frame)
647 (if (equal display
648 (frame-parameter frame 'display))
649 frame))
650 (frame-list)))))
651 (if (and (consp frames)
652 (not (y-or-n-p (if (cdr frames)
653 (format "Delete %s frames? " (length frames))
654 (format "Delete %s ? " (car frames))))))
655 (error "Abort!")
656 (mapc 'delete-frame frames)
657 (x-close-connection display))))
658
6238bfaf
RS
659(defun make-frame-command ()
660 "Make a new frame, and select it if the terminal displays only one frame."
661 (interactive)
c1b1350b 662 (if (and window-system (not (eq window-system 'pc)))
6238bfaf
RS
663 (make-frame)
664 (select-frame (make-frame))))
665
45c4fdeb
SM
666(defvar before-make-frame-hook nil
667 "Functions to run before a frame is created.")
668
669(defvar after-make-frame-functions nil
670 "Functions to run after a frame is created.
671The functions are run with one arg, the newly created frame.")
672
72bf1a8b 673(defvar after-setting-font-hook nil
81b99826
GM
674 "Functions to run after a frame's font has been changed.")
675
92e443b1 676;; Alias, kept temporarily.
84ed1560 677(define-obsolete-function-alias 'new-frame 'make-frame "22.1")
bc93c097 678
45c4fdeb
SM
679(defun make-frame (&optional parameters)
680 "Return a newly created frame displaying the current buffer.
681Optional argument PARAMETERS is an alist of parameters for the new frame.
682Each element of PARAMETERS should have the form (NAME . VALUE), for example:
a105105a 683
45c4fdeb 684 (name . STRING) The frame should be named STRING.
43a2e52c 685
45c4fdeb
SM
686 (width . NUMBER) The frame should be NUMBER characters in width.
687 (height . NUMBER) The frame should be NUMBER text lines high.
43a2e52c 688
45c4fdeb 689You cannot specify either `width' or `height', you must use neither or both.
43a2e52c 690
45c4fdeb
SM
691 (minibuffer . t) The frame should have a minibuffer.
692 (minibuffer . nil) The frame should have no minibuffer.
693 (minibuffer . only) The frame should contain only a minibuffer.
694 (minibuffer . WINDOW) The frame should use WINDOW as its minibuffer window.
43a2e52c 695
2246281f
KL
696 (window-system . nil) The frame should be displayed on a terminal device.
697 (window-system . x) The frame should be displayed in an X window.
698
6ed8eeff 699 (terminal . ID) The frame should use the terminal identified by ID.
b6660415 700
2246281f 701Before the frame is created (via `frame-creation-function-alist'), functions on the
45c4fdeb 702hook `before-make-frame-hook' are run. After the frame is created, functions
8de9d3f6
LT
703on `after-make-frame-functions' are run with one arg, the newly created frame.
704
705This function itself does not make the new frame the selected frame.
706The previously selected frame remains selected. However, the
707window system may select the new frame for its own reasons, for
708instance if the frame appears under the mouse pointer and your
709setup is for focus to follow the pointer."
64c669bc 710 (interactive)
b6660415 711 (let* ((w (cond
6ed8eeff
KL
712 ((assq 'terminal parameters)
713 (let ((type (terminal-live-p (cdr (assq 'terminal parameters)))))
b6660415
KL
714 (cond
715 ((eq type t) nil)
6ed8eeff 716 ((eq type nil) (error "Terminal %s does not exist" (cdr (assq 'terminal parameters))))
b6660415
KL
717 (t type))))
718 ((assq 'window-system parameters)
719 (cdr (assq 'window-system parameters)))
720 (t window-system)))
2246281f 721 (frame-creation-function (cdr (assq w frame-creation-function-alist)))
da8e8fc1 722 (oldframe (selected-frame))
2246281f
KL
723 frame)
724 (unless frame-creation-function
725 (error "Don't know how to create a frame on window system %s" w))
726 (run-hooks 'before-make-frame-hook)
e682e37e 727 (setq frame (funcall frame-creation-function (append parameters (cdr (assq w window-system-default-frame-alist)))))
30a2fded 728 (normal-erase-is-backspace-setup-frame frame)
d7a0175d
KL
729 ;; Inherit the 'environment and 'client parameters.
730 (let ((env (frame-parameter oldframe 'environment))
de87fb59 731 (client (frame-parameter oldframe 'client))
de87fb59 732 (displayenv (frame-parameter oldframe 'display-environment-variable)))
d7a0175d
KL
733 (if (not (framep env))
734 (setq env oldframe))
735 (if (and env (not (assq 'environment parameters)))
736 (set-frame-parameter frame 'environment env))
de87fb59
DN
737 (if (and displayenv (not (assq 'display-environment-variable parameters)))
738 (set-frame-parameter frame 'display-environment-variable displayenv))
d7a0175d
KL
739 (if (and client (not (assq 'client parameters)))
740 (set-frame-parameter frame 'client client)))
45c4fdeb
SM
741 (run-hook-with-args 'after-make-frame-functions frame)
742 frame))
64c669bc 743
7eadab74
JB
744(defun filtered-frame-list (predicate)
745 "Return a list of all live frames which satisfy PREDICATE."
047bc928
GM
746 (let* ((frames (frame-list))
747 (list frames))
7eadab74 748 (while (consp frames)
047bc928
GM
749 (unless (funcall predicate (car frames))
750 (setcar frames nil))
7eadab74 751 (setq frames (cdr frames)))
047bc928 752 (delq nil list)))
7eadab74
JB
753
754(defun minibuffer-frame-list ()
755 "Return a list of all frames with their own minibuffers."
756 (filtered-frame-list
b7de6024
SM
757 (lambda (frame)
758 (eq frame (window-frame (minibuffer-window frame))))))
7eadab74 759
9684e4c9
KL
760(defun frames-on-display-list (&optional terminal)
761 "Return a list of all frames on TERMINAL.
b6660415 762
9684e4c9
KL
763TERMINAL should be a terminal identifier (an integer), a frame,
764or a name of an X display (a string of the form
765HOST:SERVER.SCREEN).
b6660415 766
9684e4c9
KL
767If TERMINAL is omitted or nil, it defaults to the selected
768frame's terminal device."
769 (let* ((terminal (terminal-id terminal))
047bc928 770 (func #'(lambda (frame)
6ed8eeff 771 (eq (frame-terminal frame) terminal))))
9911648b
EZ
772 (filtered-frame-list func)))
773
f35ca2fe
KL
774(defun framep-on-display (&optional terminal)
775 "Return the type of frames on TERMINAL.
776TERMINAL may be a terminal id, a display name or a frame. If it
777is a frame, its type is returned. If TERMINAL is omitted or nil,
778it defaults to the selected frame's terminal device. All frames
779on a given display are of the same type."
6ed8eeff 780 (or (terminal-live-p terminal)
f35ca2fe
KL
781 (framep terminal)
782 (framep (car (frames-on-display-list terminal)))))
9911648b 783
58bf6042
JB
784(defun frame-remove-geometry-params (param-list)
785 "Return the parameter list PARAM-LIST, but with geometry specs removed.
786This deletes all bindings in PARAM-LIST for `top', `left', `width',
791e09d8 787`height', `user-size' and `user-position' parameters.
58bf6042
JB
788Emacs uses this to avoid overriding explicit moves and resizings from
789the user during startup."
790 (setq param-list (cons nil param-list))
791 (let ((tail param-list))
792 (while (consp (cdr tail))
793 (if (and (consp (car (cdr tail)))
791e09d8
RS
794 (memq (car (car (cdr tail)))
795 '(height width top left user-position user-size)))
796 (progn
797 (setq frame-initial-geometry-arguments
798 (cons (car (cdr tail)) frame-initial-geometry-arguments))
799 (setcdr tail (cdr (cdr tail))))
58bf6042 800 (setq tail (cdr tail)))))
e11c3dc2
KH
801 (setq frame-initial-geometry-arguments
802 (nreverse frame-initial-geometry-arguments))
58bf6042
JB
803 (cdr param-list))
804
2ff62eac 805(defcustom focus-follows-mouse (not (eq window-system 'mac))
6527c9b7
RS
806 "*Non-nil if window system changes focus when you move the mouse.
807You should set this variable to tell Emacs how your window manager
808handles focus, since there is no way in general for Emacs to find out
38938eeb
EZ
809automatically.
810
811This variable does not have any effect on MS-Windows."
0ac18fb0 812 :type 'boolean
cd32a7ba
DN
813 :group 'frames
814 :version "20.3")
26ef026d 815
030f4537
GM
816(defun select-frame-set-input-focus (frame)
817 "Select FRAME, raise it, and set input focus, if possible."
818 (select-frame frame)
819 (raise-frame frame)
820 ;; Ensure, if possible, that frame gets input focus.
1913a2b3 821 (cond ((memq (window-system frame) '(x max))
80c39c38 822 (x-focus-frame frame))
114a8b8c 823 ((eq (window-system frame) 'w32)
80c39c38 824 (w32-focus-frame frame)))
030f4537 825 (cond (focus-follows-mouse
80c39c38 826 (set-mouse-position (selected-frame) (1- (frame-width)) 0))))
030f4537 827
ceab6935 828(defun other-frame (arg)
c2c93894 829 "Select the ARGth different visible frame on current display, and raise it.
ceab6935
RM
830All frames are arranged in a cyclic order.
831This command selects the frame ARG steps away in that order.
bb578a72
RS
832A negative ARG moves in the opposite order.
833
834To make this command work properly, you must tell Emacs
835how the system (or the window manager) generally handles
836focus-switching between windows. If moving the mouse onto a window
837selects it (gives it focus), set `focus-follows-mouse' to t.
838Otherwise, that variable should be nil."
ceab6935
RM
839 (interactive "p")
840 (let ((frame (selected-frame)))
841 (while (> arg 0)
a569dbc3
RM
842 (setq frame (next-frame frame))
843 (while (not (eq (frame-visible-p frame) t))
844 (setq frame (next-frame frame)))
845 (setq arg (1- arg)))
ceab6935 846 (while (< arg 0)
a569dbc3
RM
847 (setq frame (previous-frame frame))
848 (while (not (eq (frame-visible-p frame) t))
849 (setq frame (previous-frame frame)))
915cfd1f 850 (setq arg (1+ arg)))
030f4537 851 (select-frame-set-input-focus frame)))
845cde06 852
3db7df06
SM
853(defun iconify-or-deiconify-frame ()
854 "Iconify the selected frame, or deiconify if it's currently an icon."
855 (interactive)
856 (if (eq (cdr (assq 'visibility (frame-parameters))) t)
857 (iconify-frame)
858 (make-frame-visible)))
859
0b0d3e0b
KL
860(defun suspend-frame ()
861 "Do whatever is right to suspend the current frame.
f35ca2fe
KL
862Calls `suspend-emacs' if invoked from the controlling tty device,
863`suspend-tty' from a secondary tty device, and
0b0d3e0b
KL
864`iconify-or-deiconify-frame' from an X frame."
865 (interactive)
866 (let ((type (framep (selected-frame))))
867 (cond
868 ((eq type 'x) (iconify-or-deiconify-frame))
869 ((eq type t)
6ed8eeff 870 (if (controlling-tty-p)
4a933ef8
KL
871 (suspend-emacs)
872 (suspend-tty)))
0b0d3e0b
KL
873 (t (suspend-emacs)))))
874
845cde06
EZ
875(defun make-frame-names-alist ()
876 (let* ((current-frame (selected-frame))
877 (falist
878 (cons
879 (cons (frame-parameter current-frame 'name) current-frame) nil))
880 (frame (next-frame nil t)))
881 (while (not (eq frame current-frame))
882 (progn
883 (setq falist (cons (cons (frame-parameter frame 'name) frame) falist))
884 (setq frame (next-frame frame t))))
885 falist))
886
887(defvar frame-name-history nil)
845cde06 888(defun select-frame-by-name (name)
563283b1 889 "Select the frame on the current terminal whose name is NAME and raise it.
845cde06
EZ
890If there is no frame by that name, signal an error."
891 (interactive
716ff1c9
RS
892 (let* ((frame-names-alist (make-frame-names-alist))
893 (default (car (car frame-names-alist)))
894 (input (completing-read
895 (format "Select Frame (default %s): " default)
896 frame-names-alist nil t nil 'frame-name-history)))
845cde06
EZ
897 (if (= (length input) 0)
898 (list default)
899 (list input))))
716ff1c9
RS
900 (let* ((frame-names-alist (make-frame-names-alist))
901 (frame (cdr (assoc name frame-names-alist))))
845cde06
EZ
902 (or frame
903 (error "There is no frame named `%s'" name))
904 (make-frame-visible frame)
905 (raise-frame frame)
880a9ae1
AI
906 (select-frame frame)
907 ;; Ensure, if possible, that frame gets input focus.
2246281f 908 (cond ((eq (window-system frame) 'x)
80c39c38 909 (x-focus-frame frame))
2246281f 910 ((eq (window-system frame) 'w32)
80c39c38
RS
911 (w32-focus-frame frame)))
912 (when focus-follows-mouse
913 (set-mouse-position frame (1- (frame-width frame)) 0))))
64c669bc 914\f
dc6d9681
JB
915;;;; Frame configurations
916
917(defun current-frame-configuration ()
918 "Return a list describing the positions and states of all frames.
376a7584
JB
919Its car is `frame-configuration'.
920Each element of the cdr is a list of the form (FRAME ALIST WINDOW-CONFIG),
921where
922 FRAME is a frame object,
923 ALIST is an association list specifying some of FRAME's parameters, and
924 WINDOW-CONFIG is a window configuration object for FRAME."
925 (cons 'frame-configuration
b7de6024
SM
926 (mapcar (lambda (frame)
927 (list frame
928 (frame-parameters frame)
929 (current-window-configuration frame)))
376a7584 930 (frame-list))))
dc6d9681 931
68cd265f 932(defun set-frame-configuration (configuration &optional nodelete)
dc6d9681
JB
933 "Restore the frames to the state described by CONFIGURATION.
934Each frame listed in CONFIGURATION has its position, size, window
68cd265f 935configuration, and other parameters set as specified in CONFIGURATION.
d398de43
LT
936However, this function does not restore deleted frames.
937
a78db71c
RS
938Ordinarily, this function deletes all existing frames not
939listed in CONFIGURATION. But if optional second argument NODELETE
5da841d2 940is given and non-nil, the unwanted frames are iconified instead."
376a7584
JB
941 (or (frame-configuration-p configuration)
942 (signal 'wrong-type-argument
943 (list 'frame-configuration-p configuration)))
944 (let ((config-alist (cdr configuration))
945 frames-to-delete)
b7de6024
SM
946 (dolist (frame (frame-list))
947 (let ((parameters (assq frame config-alist)))
948 (if parameters
949 (progn
950 (modify-frame-parameters
951 frame
952 ;; Since we can't set a frame's minibuffer status,
953 ;; we might as well omit the parameter altogether.
954 (let* ((parms (nth 1 parameters))
955 (mini (assq 'minibuffer parms)))
956 (if mini (setq parms (delq mini parms)))
957 parms))
958 (set-window-configuration (nth 2 parameters)))
959 (setq frames-to-delete (cons frame frames-to-delete)))))
960 (mapc (if nodelete
961 ;; Note: making frames invisible here was tried
962 ;; but led to some strange behavior--each time the frame
963 ;; was made visible again, the window manager asked afresh
964 ;; for where to put it.
965 'iconify-frame
966 'delete-frame)
967 frames-to-delete)))
64c669bc 968\f
dc6d9681
JB
969;;;; Convenience functions for accessing and interactively changing
970;;;; frame parameters.
64c669bc 971
151bdc83 972(defun frame-height (&optional frame)
dc6d9681
JB
973 "Return number of lines available for display on FRAME.
974If FRAME is omitted, describe the currently selected frame."
151bdc83 975 (cdr (assq 'height (frame-parameters frame))))
dc6d9681
JB
976
977(defun frame-width (&optional frame)
978 "Return number of columns available for display on FRAME.
979If FRAME is omitted, describe the currently selected frame."
151bdc83 980 (cdr (assq 'width (frame-parameters frame))))
dc6d9681 981
889611f0 982(defalias 'set-default-font 'set-frame-font)
1cef6541 983(defun set-frame-font (font-name &optional keep-size)
40d34803 984 "Set the font of the selected frame to FONT-NAME.
61298010 985When called interactively, prompt for the name of the font to use.
1cef6541
JB
986To get the frame's current default font, use `frame-parameters'.
987
988The default behavior is to keep the numbers of lines and columns in
84ed1560 989the frame, thus may change its pixel size. If optional KEEP-SIZE is
1cef6541
JB
990non-nil (interactively, prefix argument) the current frame size (in
991pixels) is kept by adjusting the numbers of the lines and columns."
f39784dd 992 (interactive
1cef6541
JB
993 (let* ((completion-ignore-case t)
994 (font (completing-read "Font name: "
1cef6541
JB
995 ;; x-list-fonts will fail with an error
996 ;; if this frame doesn't support fonts.
b7de6024
SM
997 (x-list-fonts "*" nil (selected-frame))
998 nil nil nil nil
999 (frame-parameter nil 'font))))
1cef6541
JB
1000 (list font current-prefix-arg)))
1001 (let (fht fwd)
1002 (if keep-size
1003 (setq fht (* (frame-parameter nil 'height) (frame-char-height))
1004 fwd (* (frame-parameter nil 'width) (frame-char-width))))
1005 (modify-frame-parameters (selected-frame)
1006 (list (cons 'font font-name)))
1007 (if keep-size
1008 (modify-frame-parameters
1009 (selected-frame)
1010 (list (cons 'height (round fht (frame-char-height)))
1011 (cons 'width (round fwd (frame-char-width)))))))
72bf1a8b 1012 (run-hooks 'after-setting-font-hook 'after-setting-font-hooks))
64c669bc 1013
7e573c4a 1014(defun set-frame-parameter (frame parameter value)
0dac35b8
KL
1015 "Set frame parameter PARAMETER to VALUE on FRAME.
1016If FRAME is nil, it defaults to the selected frame.
c2c93894 1017See `modify-frame-parameters'."
7e573c4a
SM
1018 (modify-frame-parameters frame (list (cons parameter value))))
1019
8290babd 1020(defun set-background-color (color-name)
40d34803 1021 "Set the background color of the selected frame to COLOR-NAME.
61298010
RS
1022When called interactively, prompt for the name of the color to use.
1023To get the frame's current background color, use `frame-parameters'."
63ec4f94 1024 (interactive (list (facemenu-read-color "Background color: ")))
dc6d9681 1025 (modify-frame-parameters (selected-frame)
528e1416
EZ
1026 (list (cons 'background-color color-name)))
1027 (or window-system
1028 (face-set-after-frame-default (selected-frame))))
64c669bc 1029
8290babd 1030(defun set-foreground-color (color-name)
40d34803 1031 "Set the foreground color of the selected frame to COLOR-NAME.
61298010
RS
1032When called interactively, prompt for the name of the color to use.
1033To get the frame's current foreground color, use `frame-parameters'."
63ec4f94 1034 (interactive (list (facemenu-read-color "Foreground color: ")))
dc6d9681 1035 (modify-frame-parameters (selected-frame)
528e1416
EZ
1036 (list (cons 'foreground-color color-name)))
1037 (or window-system
1038 (face-set-after-frame-default (selected-frame))))
64c669bc
JB
1039
1040(defun set-cursor-color (color-name)
40d34803 1041 "Set the text cursor color of the selected frame to COLOR-NAME.
61298010
RS
1042When called interactively, prompt for the name of the color to use.
1043To get the frame's current cursor color, use `frame-parameters'."
63ec4f94 1044 (interactive (list (facemenu-read-color "Cursor color: ")))
dc6d9681 1045 (modify-frame-parameters (selected-frame)
7eadab74 1046 (list (cons 'cursor-color color-name))))
64c669bc 1047
eaa974e1 1048(defun set-mouse-color (color-name)
40d34803 1049 "Set the color of the mouse pointer of the selected frame to COLOR-NAME.
61298010
RS
1050When called interactively, prompt for the name of the color to use.
1051To get the frame's current mouse color, use `frame-parameters'."
63ec4f94 1052 (interactive (list (facemenu-read-color "Mouse color: ")))
dc6d9681 1053 (modify-frame-parameters (selected-frame)
ec6d4463
KH
1054 (list (cons 'mouse-color
1055 (or color-name
1056 (cdr (assq 'mouse-color
1057 (frame-parameters))))))))
7eadab74 1058
eaa974e1 1059(defun set-border-color (color-name)
40d34803 1060 "Set the color of the border of the selected frame to COLOR-NAME.
61298010
RS
1061When called interactively, prompt for the name of the color to use.
1062To get the frame's current border color, use `frame-parameters'."
63ec4f94 1063 (interactive (list (facemenu-read-color "Border color: ")))
eaa974e1
JB
1064 (modify-frame-parameters (selected-frame)
1065 (list (cons 'border-color color-name))))
1066
1067(defun auto-raise-mode (arg)
7eadab74 1068 "Toggle whether or not the selected frame should auto-raise.
79e6ae33
RS
1069With arg, turn auto-raise mode on if and only if arg is positive.
1070Note that this controls Emacs's own auto-raise feature.
1071Some window managers allow you to enable auto-raise for certain windows.
1072You can use that for Emacs windows if you wish, but if you do,
1073that is beyond the control of Emacs and this command has no effect on it."
7eadab74
JB
1074 (interactive "P")
1075 (if (null arg)
1076 (setq arg
1077 (if (cdr (assq 'auto-raise (frame-parameters (selected-frame))))
1078 -1 1)))
9626b928
GM
1079 (if (> arg 0)
1080 (raise-frame (selected-frame)))
dc6d9681 1081 (modify-frame-parameters (selected-frame)
7eadab74
JB
1082 (list (cons 'auto-raise (> arg 0)))))
1083
eaa974e1 1084(defun auto-lower-mode (arg)
7eadab74 1085 "Toggle whether or not the selected frame should auto-lower.
79e6ae33
RS
1086With arg, turn auto-lower mode on if and only if arg is positive.
1087Note that this controls Emacs's own auto-lower feature.
1088Some window managers allow you to enable auto-lower for certain windows.
1089You can use that for Emacs windows if you wish, but if you do,
1090that is beyond the control of Emacs and this command has no effect on it."
7eadab74
JB
1091 (interactive "P")
1092 (if (null arg)
1093 (setq arg
1094 (if (cdr (assq 'auto-lower (frame-parameters (selected-frame))))
1095 -1 1)))
dc6d9681 1096 (modify-frame-parameters (selected-frame)
7eadab74 1097 (list (cons 'auto-lower (> arg 0)))))
7777d03b
EZ
1098(defun set-frame-name (name)
1099 "Set the name of the selected frame to NAME.
1100When called interactively, prompt for the name of the frame.
1101The frame name is displayed on the modeline if the terminal displays only
1102one frame, otherwise the name is displayed on the frame's caption bar."
1103 (interactive "sFrame name: ")
1104 (modify-frame-parameters (selected-frame)
1105 (list (cons 'name name))))
56cfea72
KS
1106
1107(defun frame-current-scroll-bars (&optional frame)
1108 "Return the current scroll-bar settings in frame FRAME.
1cecf04d 1109Value is a cons (VERTICAL . HORIZ0NTAL) where VERTICAL specifies the
56cfea72 1110current location of the vertical scroll-bars (left, right, or nil),
1cecf04d 1111and HORIZONTAL specifies the current location of the horizontal scroll
56cfea72
KS
1112bars (top, bottom, or nil)."
1113 (let ((vert (frame-parameter frame 'vertical-scroll-bars))
1114 (hor nil))
1115 (unless (memq vert '(left right nil))
1116 (setq vert default-frame-scroll-bars))
1117 (cons vert hor)))
64c669bc 1118\f
9911648b 1119;;;; Frame/display capabilities.
6ed8eeff
KL
1120(defun selected-terminal ()
1121 "Return the terminal that is now selected."
1122 (frame-terminal (selected-frame)))
b6660415 1123
9911648b
EZ
1124(defun display-mouse-p (&optional display)
1125 "Return non-nil if DISPLAY has a mouse available.
1126DISPLAY can be a display name, a frame, or nil (meaning the selected
1127frame's display)."
1128 (let ((frame-type (framep-on-display display)))
1129 (cond
1130 ((eq frame-type 'pc)
1131 (msdos-mouse-p))
1132 ((eq system-type 'windows-nt)
9fa5bb32
RS
1133 (with-no-warnings
1134 (> w32-num-mouse-buttons 0)))
9911648b
EZ
1135 ((memq frame-type '(x mac))
1136 t) ;; We assume X and Mac *always* have a pointing device
1137 (t
7565ee93
DL
1138 (or (and (featurep 'xt-mouse)
1139 xterm-mouse-mode)
1140 ;; t-mouse is distributed with the GPM package. It doesn't have
1141 ;; a toggle.
1142 (featurep 't-mouse))))))
9911648b
EZ
1143
1144(defun display-popup-menus-p (&optional display)
1145 "Return non-nil if popup menus are supported on DISPLAY.
1146DISPLAY can be a display name, a frame, or nil (meaning the selected
1147frame's display).
1148Support for popup menus requires that the mouse be available."
1149 (and
1150 (let ((frame-type (framep-on-display display)))
1151 (memq frame-type '(x w32 pc mac)))
1152 (display-mouse-p display)))
1153
1154(defun display-graphic-p (&optional display)
1155 "Return non-nil if DISPLAY is a graphic display.
1156Graphical displays are those which are capable of displaying several
1157frames and several different fonts at once. This is true for displays
1158that use a window system such as X, and false for text-only terminals.
1159DISPLAY can be a display name, a frame, or nil (meaning the selected
1160frame's display)."
1161 (not (null (memq (framep-on-display display) '(x w32 mac)))))
1162
ddc456e4
EZ
1163(defun display-images-p (&optional display)
1164 "Return non-nil if DISPLAY can display images.
1165
1166DISPLAY can be a display name, a frame, or nil (meaning the selected
1167frame's display)."
1168 (and (display-graphic-p display)
1169 (fboundp 'image-mask-p)
fcc6f5cc 1170 (fboundp 'image-size)))
ddc456e4 1171
6693b279
EZ
1172(defalias 'display-multi-frame-p 'display-graphic-p)
1173(defalias 'display-multi-font-p 'display-graphic-p)
1174
9911648b
EZ
1175(defun display-selections-p (&optional display)
1176 "Return non-nil if DISPLAY supports selections.
1177A selection is a way to transfer text or other data between programs
1178via special system buffers called `selection' or `cut buffer' or
1179`clipboard'.
1180DISPLAY can be a display name, a frame, or nil (meaning the selected
1181frame's display)."
1182 (let ((frame-type (framep-on-display display)))
1183 (cond
1184 ((eq frame-type 'pc)
1185 ;; MS-DOG frames support selections when Emacs runs inside
1186 ;; the Windows' DOS Box.
9fa5bb32
RS
1187 (with-no-warnings
1188 (not (null dos-windows-version))))
9911648b
EZ
1189 ((memq frame-type '(x w32 mac))
1190 t) ;; FIXME?
1191 (t
1192 nil))))
1193
1194(defun display-screens (&optional display)
1195 "Return the number of screens associated with DISPLAY."
1196 (let ((frame-type (framep-on-display display)))
1197 (cond
bb9404d6 1198 ((memq frame-type '(x w32 mac))
9911648b 1199 (x-display-screens display))
bb9404d6 1200 (t
9911648b
EZ
1201 1))))
1202
1203(defun display-pixel-height (&optional display)
1204 "Return the height of DISPLAY's screen in pixels.
1205For character terminals, each character counts as a single pixel."
1206 (let ((frame-type (framep-on-display display)))
1207 (cond
1208 ((memq frame-type '(x w32 mac))
1209 (x-display-pixel-height display))
1210 (t
1211 (frame-height (if (framep display) display (selected-frame)))))))
1212
1213(defun display-pixel-width (&optional display)
1214 "Return the width of DISPLAY's screen in pixels.
1215For character terminals, each character counts as a single pixel."
1216 (let ((frame-type (framep-on-display display)))
1217 (cond
1218 ((memq frame-type '(x w32 mac))
1219 (x-display-pixel-width display))
1220 (t
1221 (frame-width (if (framep display) display (selected-frame)))))))
1222
51e39dfc
KS
1223(defcustom display-mm-dimensions-alist nil
1224 "Alist for specifying screen dimensions in millimeters.
1225The dimensions will be used for `display-mm-height' and
1226`display-mm-width' if defined for the respective display.
1227
1228Each element of the alist has the form (display . (width . height)),
1229e.g. (\":0.0\" . (287 . 215)).
1230
1231If `display' equals t, it specifies dimensions for all graphical
1232displays not explicitely specified."
1233 :version "22.1"
1234 :type '(alist :key-type (choice (string :tag "Display name")
1235 (const :tag "Default" t))
1236 :value-type (cons :tag "Dimensions"
1237 (integer :tag "Width")
1238 (integer :tag "Height")))
1239 :group 'frames)
1240
9911648b
EZ
1241(defun display-mm-height (&optional display)
1242 "Return the height of DISPLAY's screen in millimeters.
51e39dfc 1243System values can be overriden by `display-mm-dimensions-alist'.
9911648b
EZ
1244If the information is unavailable, value is nil."
1245 (and (memq (framep-on-display display) '(x w32 mac))
51e39dfc
KS
1246 (or (cddr (assoc (or display (frame-parameter nil 'display))
1247 display-mm-dimensions-alist))
1248 (cddr (assoc t display-mm-dimensions-alist))
1249 (x-display-mm-height display))))
9911648b
EZ
1250
1251(defun display-mm-width (&optional display)
1252 "Return the width of DISPLAY's screen in millimeters.
51e39dfc 1253System values can be overriden by `display-mm-dimensions-alist'.
9911648b
EZ
1254If the information is unavailable, value is nil."
1255 (and (memq (framep-on-display display) '(x w32 mac))
51e39dfc
KS
1256 (or (cadr (assoc (or display (frame-parameter nil 'display))
1257 display-mm-dimensions-alist))
1258 (cadr (assoc t display-mm-dimensions-alist))
1259 (x-display-mm-width display))))
9911648b
EZ
1260
1261(defun display-backing-store (&optional display)
1262 "Return the backing store capability of DISPLAY's screen.
1263The value may be `always', `when-mapped', `not-useful', or nil if
1264the question is inapplicable to a certain kind of display."
1265 (let ((frame-type (framep-on-display display)))
1266 (cond
1267 ((memq frame-type '(x w32 mac))
1268 (x-display-backing-store display))
1269 (t
1270 'not-useful))))
1271
1272(defun display-save-under (&optional display)
1273 "Return non-nil if DISPLAY's screen supports the SaveUnder feature."
1274 (let ((frame-type (framep-on-display display)))
1275 (cond
1276 ((memq frame-type '(x w32 mac))
1277 (x-display-save-under display))
1278 (t
1279 'not-useful))))
1280
1281(defun display-planes (&optional display)
1282 "Return the number of planes supported by DISPLAY."
1283 (let ((frame-type (framep-on-display display)))
1284 (cond
1285 ((memq frame-type '(x w32 mac))
1286 (x-display-planes display))
1287 ((eq frame-type 'pc)
1288 4)
1289 (t
1290 (truncate (log (length (tty-color-alist)) 2))))))
1291
1292(defun display-color-cells (&optional display)
1293 "Return the number of color cells supported by DISPLAY."
1294 (let ((frame-type (framep-on-display display)))
1295 (cond
1296 ((memq frame-type '(x w32 mac))
1297 (x-display-color-cells display))
1298 ((eq frame-type 'pc)
1299 16)
1300 (t
3224dac1 1301 (tty-display-color-cells display)))))
9911648b
EZ
1302
1303(defun display-visual-class (&optional display)
1304 "Returns the visual class of DISPLAY.
1305The value is one of the symbols `static-gray', `gray-scale',
1306`static-color', `pseudo-color', `true-color', or `direct-color'."
1307 (let ((frame-type (framep-on-display display)))
1308 (cond
1309 ((memq frame-type '(x w32 mac))
1310 (x-display-visual-class display))
1311 ((and (memq frame-type '(pc t))
1312 (tty-display-color-p display))
1313 'static-color)
1314 (t
1315 'static-gray))))
1316
1317\f
dc6d9681 1318;;;; Aliases for backward compatibility with Emacs 18.
84ed1560
JB
1319(define-obsolete-function-alias 'screen-height 'frame-height) ;before 19.15
1320(define-obsolete-function-alias 'screen-width 'frame-width) ;before 19.15
9e2b097b
JB
1321
1322(defun set-screen-width (cols &optional pretend)
84ed1560 1323 "Change the size of the screen to COLS columns.
40d34803
DL
1324Optional second arg non-nil means that redisplay should use COLS columns
1325but that the idea of the actual width of the frame should not be changed.
84ed1560 1326This function is provided only for compatibility with Emacs 18."
9e2b097b
JB
1327 (set-frame-width (selected-frame) cols pretend))
1328
1329(defun set-screen-height (lines &optional pretend)
84ed1560 1330 "Change the height of the screen to LINES lines.
40d34803
DL
1331Optional second arg non-nil means that redisplay should use LINES lines
1332but that the idea of the actual height of the screen should not be changed.
84ed1560 1333This function is provided only for compatibility with Emacs 18."
9e2b097b
JB
1334 (set-frame-height (selected-frame) lines pretend))
1335
154a757e
GM
1336(defun delete-other-frames (&optional frame)
1337 "Delete all frames except FRAME.
a2125918
GM
1338If FRAME uses another frame's minibuffer, the minibuffer frame is
1339left untouched. FRAME nil or omitted means use the selected frame."
154a757e
GM
1340 (interactive)
1341 (unless frame
1342 (setq frame (selected-frame)))
a2125918
GM
1343 (let* ((mini-frame (window-frame (minibuffer-window frame)))
1344 (frames (delq mini-frame (delq frame (frame-list)))))
1345 ;; Delete mon-minibuffer-only frames first, because `delete-frame'
1346 ;; signals an error when trying to delete a mini-frame that's
1347 ;; still in use by another frame.
1348 (dolist (frame frames)
1349 (unless (eq (frame-parameter frame 'minibuffer) 'only)
1350 (delete-frame frame)))
1351 ;; Delete minibuffer-only frames.
1352 (dolist (frame frames)
1353 (when (eq (frame-parameter frame 'minibuffer) 'only)
1354 (delete-frame frame)))))
154a757e 1355
2598a293
SM
1356(make-obsolete 'set-screen-width 'set-frame-width) ;before 19.15
1357(make-obsolete 'set-screen-height 'set-frame-height) ;before 19.15
dc6d9681 1358
da7829a3 1359;; miscellaneous obsolescence declarations
84ed1560
JB
1360(define-obsolete-variable-alias 'delete-frame-hook
1361 'delete-frame-functions "22.1")
da7829a3 1362
dc6d9681 1363\f
7e573c4a 1364;; Highlighting trailing whitespace.
81b99826
GM
1365
1366(make-variable-buffer-local 'show-trailing-whitespace)
1367
1368(defcustom show-trailing-whitespace nil
e6c45c29
LT
1369 "*Non-nil means highlight trailing whitespace.
1370This is done in the face `trailing-whitespace'."
81b99826 1371 :type 'boolean
b4aefb19 1372 :group 'whitespace-faces)
81b99826
GM
1373
1374
1375\f
7e573c4a 1376;; Scrolling
79b14b94
GM
1377
1378(defgroup scrolling nil
1379 "Scrolling windows."
1380 :version "21.1"
1381 :group 'frames)
1382
968ecc28 1383(defcustom auto-hscroll-mode t
310e9a21 1384 "*Allow or disallow automatic scrolling windows horizontally.
9ea60c46 1385If non-nil, windows are automatically scrolled horizontally to make
79b14b94
GM
1386point visible."
1387 :version "21.1"
1388 :type 'boolean
1389 :group 'scrolling)
968ecc28 1390(defvaralias 'automatic-hscrolling 'auto-hscroll-mode)
79b14b94
GM
1391
1392\f
7e573c4a 1393;; Blinking cursor
81b99826
GM
1394
1395(defgroup cursor nil
ca693be8 1396 "Displaying text cursors."
40d34803 1397 :version "21.1"
81b99826
GM
1398 :group 'frames)
1399
1400(defcustom blink-cursor-delay 0.5
ca693be8 1401 "*Seconds of idle time after which cursor starts to blink."
81b99826
GM
1402 :type 'number
1403 :group 'cursor)
1404
1405(defcustom blink-cursor-interval 0.5
1406 "*Length of cursor blink interval in seconds."
81b99826
GM
1407 :type 'number
1408 :group 'cursor)
1409
1410(defvar blink-cursor-idle-timer nil
ca693be8
GM
1411 "Timer started after `blink-cursor-delay' seconds of Emacs idle time.
1412The function `blink-cursor-start' is called when the timer fires.")
81b99826
GM
1413
1414(defvar blink-cursor-timer nil
ca693be8 1415 "Timer started from `blink-cursor-start'.
a795e09a
LT
1416This timer calls `blink-cursor-timer-function' every
1417`blink-cursor-interval' seconds.")
1418
81b99826 1419(defun blink-cursor-start ()
ca693be8
GM
1420 "Timer function called from the timer `blink-cursor-idle-timer'.
1421This starts the timer `blink-cursor-timer', which makes the cursor blink
1422if appropriate. It also arranges to cancel that timer when the next
1423command starts, by installing a pre-command hook."
81b99826 1424 (when (null blink-cursor-timer)
f9ac92c5
CY
1425 ;; Set up the timer first, so that if this signals an error,
1426 ;; blink-cursor-end is not added to pre-command-hook.
81b99826
GM
1427 (setq blink-cursor-timer
1428 (run-with-timer blink-cursor-interval blink-cursor-interval
f9ac92c5
CY
1429 'blink-cursor-timer-function))
1430 (add-hook 'pre-command-hook 'blink-cursor-end)
1431 (internal-show-cursor nil nil)))
0a5ebe4b
GM
1432
1433(defun blink-cursor-timer-function ()
1434 "Timer function of timer `blink-cursor-timer'."
1435 (internal-show-cursor nil (not (internal-show-cursor-p))))
81b99826
GM
1436
1437(defun blink-cursor-end ()
1438 "Stop cursor blinking.
ca693be8 1439This is installed as a pre-command hook by `blink-cursor-start'.
f39784dd 1440When run, it cancels the timer `blink-cursor-timer' and removes
ca693be8 1441itself as a pre-command hook."
81b99826 1442 (remove-hook 'pre-command-hook 'blink-cursor-end)
0a5ebe4b 1443 (internal-show-cursor nil t)
8ec94c16
SM
1444 (when blink-cursor-timer
1445 (cancel-timer blink-cursor-timer)
1446 (setq blink-cursor-timer nil)))
81b99826 1447
8ec94c16
SM
1448(define-minor-mode blink-cursor-mode
1449 "Toggle blinking cursor mode.
4837b516
GM
1450With a numeric argument, turn blinking cursor mode on if ARG is positive,
1451otherwise turn it off. When blinking cursor mode is enabled, the
1452cursor of the selected window blinks.
81b99826 1453
8ec94c16
SM
1454Note that this command is effective only when Emacs
1455displays through a window system, because then Emacs does its own
1456cursor display. On a text-only terminal, this is not implemented."
1457 :init-value (not (or noninteractive
1458 no-blinking-cursor
1459 (eq system-type 'ms-dos)
1460 (not (memq window-system '(x w32 mac)))))
1461 :initialize 'custom-initialize-safe-default
1462 :group 'cursor
1463 :global t
1464 (if blink-cursor-idle-timer (cancel-timer blink-cursor-idle-timer))
1465 (setq blink-cursor-idle-timer nil)
1466 (blink-cursor-end)
1467 (when blink-cursor-mode
1468 ;; Hide the cursor.
1469 ;;(internal-show-cursor nil nil)
1470 (setq blink-cursor-idle-timer
1471 (run-with-idle-timer blink-cursor-delay
1472 blink-cursor-delay
1473 'blink-cursor-start))))
81b99826 1474
8ec94c16 1475(define-obsolete-variable-alias 'blink-cursor 'blink-cursor-mode "22.1")
81b99826 1476\f
7e573c4a 1477;; Hourglass pointer
81b99826 1478
a6c411ff 1479(defcustom display-hourglass t
088e75cc
RS
1480 "*Non-nil means show an hourglass pointer, when Emacs is busy.
1481This feature only works when on a window system that can change
1482cursor shapes."
81b99826 1483 :type 'boolean
a6c411ff 1484 :group 'cursor)
81b99826 1485
a6c411ff 1486(defcustom hourglass-delay 1
63ec4f94 1487 "*Seconds to wait before displaying an hourglass pointer when Emacs is busy."
0a86e779 1488 :type 'number
a6c411ff 1489 :group 'cursor)
1d0a3ef4 1490
5b2c5477 1491\f
c0eb3c10 1492(defcustom cursor-in-non-selected-windows t
84ed1560 1493 "*Non-nil means show a hollow box cursor in non-selected windows.
e053c60f 1494If nil, don't show a cursor except in the selected window.
f39784dd 1495Use Custom to set this variable to get the display updated."
63ec4f94 1496 :tag "Cursor In Non-selected Windows"
5b2c5477
GM
1497 :type 'boolean
1498 :group 'cursor
5b2c5477
GM
1499 :set #'(lambda (symbol value)
1500 (set-default symbol value)
5b2c5477 1501 (force-mode-line-update t)))
f39784dd 1502
81b99826 1503\f
64c669bc 1504;;;; Key bindings
64c669bc 1505
6238bfaf 1506(define-key ctl-x-5-map "2" 'make-frame-command)
154a757e 1507(define-key ctl-x-5-map "1" 'delete-other-frames)
dc6d9681 1508(define-key ctl-x-5-map "0" 'delete-frame)
ceab6935 1509(define-key ctl-x-5-map "o" 'other-frame)
49116ac0 1510
dc6d9681 1511(provide 'frame)
c88ab9ce 1512
485464c4 1513;; arch-tag: 82979c70-b8f2-4306-b2ad-ddbd6b328b56
dc6d9681 1514;;; frame.el ends here