Replace "Maintainer: FSF" with the emacs-devel mailing address
[bpt/emacs.git] / lisp / frame.el
CommitLineData
55535639 1;;; frame.el --- multi-frame management independent of window systems
c88ab9ce 2
ba318903 3;; Copyright (C) 1993-1994, 1996-1997, 2000-2014 Free Software
ab422c4d 4;; Foundation, Inc.
3a801d0c 5
34dc21db 6;; Maintainer: emacs-devel@gnu.org
fd7fa35a 7;; Keywords: internal
bd78fa1d 8;; Package: emacs
fc68affa 9
b578f267
EN
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
b578f267 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
b578f267
EN
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
64c669bc 24
55535639
PJ
25;;; Commentary:
26
fc68affa 27;;; Code:
efc3dd3c
DC
28(eval-when-compile (require 'cl-lib))
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
06b60517 34 (lambda (_parameters)
371fed4e 35 (error "Can't create multiple frames without a window system")))))
2246281f
KL
36 "Alist of window-system dependent functions to call to create a new frame.
37The window system startup file should add its frame creation
38function to this list, which should take an alist of parameters
39as its argument.")
114a8b8c 40
bf1ba39f 41(defvar window-system-default-frame-alist nil
5b95ee8a
CY
42 "Window-system dependent default frame parameters.
43The value should be an alist of elements (WINDOW-SYSTEM . ALIST),
44where WINDOW-SYSTEM is a window system symbol (see `window-system')
45and ALIST is a frame parameter alist like `default-frame-alist'.
46Then, for frames on WINDOW-SYSTEM, any parameters specified in
47ALIST supersede the corresponding parameters specified in
dca6c418 48`default-frame-alist'.")
bf1ba39f 49
efc3dd3c
DC
50(defvar display-format-alist nil
51 "Alist of patterns to decode display names.
52The car of each entry is a regular expression matching a display
53name string. The cdr is a symbol giving the window-system that
54handles the corresponding kind of display.")
55
7e573c4a
SM
56;; The initial value given here used to ask for a minibuffer.
57;; But that's not necessary, because the default is to have one.
58;; By not specifying it here, we let an X resource specify it.
fac580eb 59(defcustom initial-frame-alist nil
dca6c418
MR
60 "Alist of parameters for the initial X window frame.
61You can set this in your init file; for example,
62
63 (setq initial-frame-alist
64 '((top . 1) (left . 1) (width . 80) (height . 55)))
65
66Parameters specified here supersede the values given in
67`default-frame-alist'.
68
69If the value calls for a frame without a minibuffer, and you have
70not created a minibuffer frame on your own, a minibuffer frame is
71created according to `minibuffer-frame-alist'.
72
73You can specify geometry-related options for just the initial
74frame by setting this variable in your init file; however, they
75won't take effect until Emacs reads your init file, which happens
76after creating the initial frame. If you want the initial frame
77to have the proper geometry as soon as it appears, you need to
78use this three-step process:
de17da15
RS
79* Specify X resources to give the geometry you want.
80* Set `default-frame-alist' to override these options so that they
81 don't affect subsequent frames.
82* Set `initial-frame-alist' in a way that matches the X resources,
0c966f88
RS
83 to override what you put in `default-frame-alist'."
84 :type '(repeat (cons :format "%v"
85 (symbol :tag "Parameter")
86 (sexp :tag "Value")))
87 :group 'frames)
64c669bc 88
0b9d32af 89(defcustom minibuffer-frame-alist '((width . 80) (height . 2))
92e02072
CY
90 "Alist of parameters for the initial minibuffer frame.
91This is the minibuffer frame created if `initial-frame-alist'
92calls for a frame without a minibuffer. The parameters specified
93here supersede those given in `default-frame-alist', for the
94initial minibuffer frame.
95
dca6c418
MR
96You can set this in your init file; for example,
97
dc6d9681 98 (setq minibuffer-frame-alist
dca6c418
MR
99 '((top . 1) (left . 1) (width . 80) (height . 2)))
100
92e02072
CY
101It is not necessary to include (minibuffer . only); that is
102appended when the minibuffer frame is created."
0b9d32af
RS
103 :type '(repeat (cons :format "%v"
104 (symbol :tag "Parameter")
105 (sexp :tag "Value")))
106 :group 'frames)
64c669bc 107
924be53a 108(defun handle-delete-frame (event)
40d34803 109 "Handle delete-frame events from the X server."
924be53a
RS
110 (interactive "e")
111 (let ((frame (posn-window (event-start event)))
112 (i 0)
113 (tail (frame-list)))
114 (while tail
115 (and (frame-visible-p (car tail))
116 (not (eq (car tail) frame))
117 (setq i (1+ i)))
118 (setq tail (cdr tail)))
119 (if (> i 0)
120 (delete-frame frame t)
a9562d19
RS
121 ;; Gildea@x.org says it is ok to ask questions before terminating.
122 (save-buffers-kill-emacs))))
511fa0d3
SM
123
124(defun handle-focus-in (_event)
125 "Handle a focus-in event.
126Focus-in events are usually bound to this function.
127Focus-in events occur when a frame has focus, but a switch-frame event
128is not generated.
129This function runs the hook `focus-in-hook'."
623891e5 130 (interactive "e")
511fa0d3
SM
131 (run-hooks 'focus-in-hook))
132
133(defun handle-focus-out (_event)
134 "Handle a focus-out event.
135Focus-out events are usually bound to this function.
136Focus-out events occur when no frame has focus.
137This function runs the hook `focus-out-hook'."
623891e5 138 (interactive "e")
511fa0d3 139 (run-hooks 'focus-out-hook))
64c669bc 140\f
dc6d9681 141;;;; Arrangement of frames at startup
64c669bc 142
7e573c4a
SM
143;; 1) Load the window system startup file from the lisp library and read the
144;; high-priority arguments (-q and the like). The window system startup
145;; file should create any frames specified in the window system defaults.
146;;
147;; 2) If no frames have been opened, we open an initial text frame.
148;;
149;; 3) Once the init file is done, we apply any newly set parameters
150;; in initial-frame-alist to the frame.
64c669bc 151
85557d7e 152;; These are now called explicitly at the proper times,
fc4d4afb
RS
153;; since that is easier to understand.
154;; Actually using hooks within Emacs is bad for future maintenance. --rms.
155;; (add-hook 'before-init-hook 'frame-initialize)
156;; (add-hook 'window-setup-hook 'frame-notice-user-settings)
64c669bc 157
7e573c4a 158;; If we create the initial frame, this is it.
dc6d9681 159(defvar frame-initial-frame nil)
64c669bc 160
3f2f8c83
RS
161;; Record the parameters used in frame-initialize to make the initial frame.
162(defvar frame-initial-frame-alist)
163
791e09d8
RS
164(defvar frame-initial-geometry-arguments nil)
165
7e573c4a
SM
166;; startup.el calls this function before loading the user's init
167;; file - if there is no frame with a minibuffer open now, create
168;; one to display messages while loading the init file.
dc6d9681 169(defun frame-initialize ()
ff2a1c79 170 "Create an initial frame if necessary."
64c669bc 171 ;; Are we actually running under a window system at all?
2246281f
KL
172 (if (and initial-window-system
173 (not noninteractive)
174 (not (eq initial-window-system 'pc)))
7eadab74
JB
175 (progn
176 ;; If there is no frame with a minibuffer besides the terminal
177 ;; frame, then we need to create the opening frame. Make sure
178 ;; it has a minibuffer, but let initial-frame-alist omit the
179 ;; minibuffer spec.
180 (or (delq terminal-frame (minibuffer-frame-list))
36fc9c9f 181 (progn
3f2f8c83 182 (setq frame-initial-frame-alist
9688894d 183 (append initial-frame-alist default-frame-alist nil))
a8bb5882
RS
184 (or (assq 'horizontal-scroll-bars frame-initial-frame-alist)
185 (setq frame-initial-frame-alist
186 (cons '(horizontal-scroll-bars . t)
187 frame-initial-frame-alist)))
2246281f
KL
188 (setq frame-initial-frame-alist
189 (cons (cons 'window-system initial-window-system)
190 frame-initial-frame-alist))
36fc9c9f
RS
191 (setq default-minibuffer-frame
192 (setq frame-initial-frame
96a84970 193 (make-frame frame-initial-frame-alist)))
11281034
RS
194 ;; Delete any specifications for window geometry parameters
195 ;; so that we won't reapply them in frame-notice-user-settings.
196 ;; It would be wrong to reapply them then,
197 ;; because that would override explicit user resizing.
58bf6042 198 (setq initial-frame-alist
c2079f0a 199 (frame-remove-geometry-params initial-frame-alist))))
a13f8f50
KL
200 ;; Copy the environment of the Emacs process into the new frame.
201 (set-frame-parameter frame-initial-frame 'environment
202 (frame-parameter terminal-frame 'environment))
85557d7e 203 ;; At this point, we know that we have a frame open, so we
dc6d9681
JB
204 ;; can delete the terminal frame.
205 (delete-frame terminal-frame)
2246281f 206 (setq terminal-frame nil))))
85557d7e 207
8f2a992b
GM
208(defvar frame-notice-user-settings t
209 "Non-nil means function `frame-notice-user-settings' wasn't run yet.")
210
aa360da1
GM
211(declare-function tool-bar-mode "tool-bar" (&optional arg))
212
880e6158
MR
213(defalias 'tool-bar-lines-needed 'tool-bar-height)
214
7e573c4a
SM
215;; startup.el calls this function after loading the user's init
216;; file. Now default-frame-alist and initial-frame-alist contain
217;; information to which we must react; do what needs to be done.
dc6d9681 218(defun frame-notice-user-settings ()
40d34803 219 "Act on user's init file settings of frame parameters.
0e1bfc78
KL
220React to settings of `initial-frame-alist',
221`window-system-default-frame-alist' and `default-frame-alist'
222there (in decreasing order of priority)."
7eadab74
JB
223 ;; Creating and deleting frames may shift the selected frame around,
224 ;; and thus the current buffer. Protect against that. We don't
225 ;; want to use save-excursion here, because that may also try to set
226 ;; the buffer of the selected window, which fails when the selected
227 ;; window is the minibuffer.
0e1bfc78 228 (let ((old-buffer (current-buffer))
cb01ce3f
SM
229 (window-system-frame-alist
230 (cdr (assq initial-window-system
231 window-system-default-frame-alist))))
7eadab74 232
8f2a992b
GM
233 (when (and frame-notice-user-settings
234 (null frame-initial-frame))
42002db5
EZ
235 ;; This case happens when we don't have a window system, and
236 ;; also for MS-DOS frames.
0216b738 237 (let ((parms (frame-parameters)))
8f2a992b
GM
238 ;; Don't change the frame names.
239 (setq parms (delq (assq 'name parms) parms))
240 ;; Can't modify the minibuffer parameter, so don't try.
241 (setq parms (delq (assq 'minibuffer parms) parms))
0216b738
CY
242 (modify-frame-parameters
243 nil
244 (if initial-window-system
245 parms
246 ;; initial-frame-alist and default-frame-alist were already
247 ;; applied in pc-win.el.
248 (append initial-frame-alist window-system-frame-alist
249 default-frame-alist parms nil)))
2246281f 250 (if (null initial-window-system) ;; MS-DOS does this differently in pc-win.el
42002db5
EZ
251 (let ((newparms (frame-parameters))
252 (frame (selected-frame)))
253 (tty-handle-reverse-video frame newparms)
254 ;; If we changed the background color, we need to update
255 ;; the background-mode parameter, and maybe some faces,
256 ;; too.
257 (when (assq 'background-color newparms)
258 (unless (or (assq 'background-mode initial-frame-alist)
259 (assq 'background-mode default-frame-alist))
260 (frame-set-background-mode frame))
261 (face-set-after-frame-default frame))))))
8f2a992b 262
7eadab74
JB
263 ;; If the initial frame is still around, apply initial-frame-alist
264 ;; and default-frame-alist to it.
9688894d
GM
265 (when (frame-live-p frame-initial-frame)
266
267 ;; When tool-bar has been switched off, correct the frame size
268 ;; by the lines added in x-create-frame for the tool-bar and
269 ;; switch `tool-bar-mode' off.
085ef9b3
GM
270 (when (display-graphic-p)
271 (let ((tool-bar-lines (or (assq 'tool-bar-lines initial-frame-alist)
0e1bfc78 272 (assq 'tool-bar-lines window-system-frame-alist)
085ef9b3 273 (assq 'tool-bar-lines default-frame-alist))))
5c39a60f
JR
274 (when (and tool-bar-originally-present
275 (or (null tool-bar-lines)
276 (null (cdr tool-bar-lines))
277 (eq 0 (cdr tool-bar-lines))))
085ef9b3 278 (let* ((char-height (frame-char-height frame-initial-frame))
5c39a60f 279 (image-height tool-bar-images-pixel-height)
085ef9b3
GM
280 (margin (cond ((and (consp tool-bar-button-margin)
281 (integerp (cdr tool-bar-button-margin))
282 (> tool-bar-button-margin 0))
283 (cdr tool-bar-button-margin))
284 ((and (integerp tool-bar-button-margin)
285 (> tool-bar-button-margin 0))
286 tool-bar-button-margin)
287 (t 0)))
288 (relief (if (and (integerp tool-bar-button-relief)
289 (> tool-bar-button-relief 0))
290 tool-bar-button-relief 3))
f39784dd 291 (lines (/ (+ image-height
085ef9b3
GM
292 (* 2 margin)
293 (* 2 relief)
294 (1- char-height))
295 char-height))
296 (height (frame-parameter frame-initial-frame 'height))
297 (newparms (list (cons 'height (- height lines))))
f39784dd 298 (initial-top (cdr (assq 'top
085ef9b3
GM
299 frame-initial-geometry-arguments)))
300 (top (frame-parameter frame-initial-frame 'top)))
301 (when (and (consp initial-top) (eq '- (car initial-top)))
7f18ce22
RS
302 (let ((adjusted-top
303 (cond ((and (consp top)
304 (eq '+ (car top)))
305 (list '+
306 (+ (cadr top)
307 (* lines char-height))))
308 ((and (consp top)
309 (eq '- (car top)))
310 (list '-
311 (- (cadr top)
312 (* lines char-height))))
313 (t (+ top (* lines char-height))))))
314 (setq newparms
315 (append newparms
316 `((top . ,adjusted-top))
317 nil))))
085ef9b3
GM
318 (modify-frame-parameters frame-initial-frame newparms)
319 (tool-bar-mode -1)))))
9688894d
GM
320
321 ;; The initial frame we create above always has a minibuffer.
322 ;; If the user wants to remove it, or make it a minibuffer-only
323 ;; frame, then we'll have to delete the current frame and make a
324 ;; new one; you can't remove or add a root window to/from an
325 ;; existing frame.
326 ;;
327 ;; NOTE: default-frame-alist was nil when we created the
328 ;; existing frame. We need to explicitly include
329 ;; default-frame-alist in the parameters of the screen we
330 ;; create here, so that its new value, gleaned from the user's
865fe16f 331 ;; init file, will be applied to the existing screen.
de2f5dbe 332 (if (not (eq (cdr (or (assq 'minibuffer initial-frame-alist)
0e1bfc78 333 (assq 'minibuffer window-system-frame-alist)
de2f5dbe
GM
334 (assq 'minibuffer default-frame-alist)
335 '(minibuffer . t)))
336 t))
337 ;; Create the new frame.
338 (let (parms new)
35a89bdd
EZ
339 ;; MS-Windows needs this to avoid inflooping below.
340 (if (eq system-type 'windows-nt)
341 (sit-for 0 t))
de2f5dbe
GM
342 ;; If the frame isn't visible yet, wait till it is.
343 ;; If the user has to position the window,
344 ;; Emacs doesn't know its real position until
345 ;; the frame is seen to be visible.
346 (while (not (cdr (assq 'visibility
347 (frame-parameters frame-initial-frame))))
348 (sleep-for 1))
349 (setq parms (frame-parameters frame-initial-frame))
9688894d 350
f39784dd 351 ;; Get rid of `name' unless it was specified explicitly before.
de2f5dbe
GM
352 (or (assq 'name frame-initial-frame-alist)
353 (setq parms (delq (assq 'name parms) parms)))
2f5bb432
SM
354 ;; An explicit parent-id is a request to XEmbed the frame.
355 (or (assq 'parent-id frame-initial-frame-alist)
356 (setq parms (delq (assq 'parent-id parms) parms)))
de2f5dbe
GM
357
358 (setq parms (append initial-frame-alist
0e1bfc78 359 window-system-frame-alist
de2f5dbe
GM
360 default-frame-alist
361 parms
362 nil))
363
364 ;; Get rid of `reverse', because that was handled
365 ;; when we first made the frame.
366 (setq parms (cons '(reverse) (delq (assq 'reverse parms) parms)))
367
368 (if (assq 'height frame-initial-geometry-arguments)
369 (setq parms (assq-delete-all 'height parms)))
370 (if (assq 'width frame-initial-geometry-arguments)
371 (setq parms (assq-delete-all 'width parms)))
372 (if (assq 'left frame-initial-geometry-arguments)
373 (setq parms (assq-delete-all 'left parms)))
374 (if (assq 'top frame-initial-geometry-arguments)
375 (setq parms (assq-delete-all 'top parms)))
376 (setq new
377 (make-frame
378 ;; Use the geometry args that created the existing
379 ;; frame, rather than the parms we get for it.
380 (append frame-initial-geometry-arguments
381 '((user-size . t) (user-position . t))
382 parms)))
383 ;; The initial frame, which we are about to delete, may be
384 ;; the only frame with a minibuffer. If it is, create a
385 ;; new one.
386 (or (delq frame-initial-frame (minibuffer-frame-list))
387 (make-initial-minibuffer-frame nil))
388
389 ;; If the initial frame is serving as a surrogate
390 ;; minibuffer frame for any frames, we need to wean them
391 ;; onto a new frame. The default-minibuffer-frame
392 ;; variable must be handled similarly.
393 (let ((users-of-initial
394 (filtered-frame-list
b7de6024
SM
395 (lambda (frame)
396 (and (not (eq frame frame-initial-frame))
397 (eq (window-frame
398 (minibuffer-window frame))
399 frame-initial-frame))))))
400 (if (or users-of-initial
de2f5dbe
GM
401 (eq default-minibuffer-frame frame-initial-frame))
402
403 ;; Choose an appropriate frame. Prefer frames which
404 ;; are only minibuffers.
405 (let* ((new-surrogate
406 (car
407 (or (filtered-frame-list
b7de6024
SM
408 (lambda (frame)
409 (eq (cdr (assq 'minibuffer
410 (frame-parameters frame)))
411 'only)))
de2f5dbe
GM
412 (minibuffer-frame-list))))
413 (new-minibuffer (minibuffer-window new-surrogate)))
414
415 (if (eq default-minibuffer-frame frame-initial-frame)
416 (setq default-minibuffer-frame new-surrogate))
417
418 ;; Wean the frames using frame-initial-frame as
419 ;; their minibuffer frame.
b7de6024
SM
420 (dolist (frame users-of-initial)
421 (modify-frame-parameters
422 frame (list (cons 'minibuffer new-minibuffer)))))))
423
424 ;; Redirect events enqueued at this frame to the new frame.
de2f5dbe
GM
425 ;; Is this a good idea?
426 (redirect-frame-focus frame-initial-frame new)
427
428 ;; Finally, get rid of the old frame.
429 (delete-frame frame-initial-frame t))
9688894d 430
9858f6c3 431 ;; Otherwise, we don't need all that rigmarole; just apply
9688894d
GM
432 ;; the new parameters.
433 (let (newparms allparms tail)
434 (setq allparms (append initial-frame-alist
0e1bfc78 435 window-system-frame-alist
9688894d
GM
436 default-frame-alist nil))
437 (if (assq 'height frame-initial-geometry-arguments)
438 (setq allparms (assq-delete-all 'height allparms)))
439 (if (assq 'width frame-initial-geometry-arguments)
440 (setq allparms (assq-delete-all 'width allparms)))
441 (if (assq 'left frame-initial-geometry-arguments)
442 (setq allparms (assq-delete-all 'left allparms)))
443 (if (assq 'top frame-initial-geometry-arguments)
444 (setq allparms (assq-delete-all 'top allparms)))
445 (setq tail allparms)
446 ;; Find just the parms that have changed since we first
447 ;; made this frame. Those are the ones actually set by
f39784dd 448 ;; the init file. For those parms whose values we already knew
9688894d
GM
449 ;; (such as those spec'd by command line options)
450 ;; it is undesirable to specify the parm again
f39784dd 451 ;; once the user has seen the frame and been able to alter it
9688894d 452 ;; manually.
0216b738
CY
453 (let (newval oldval)
454 (dolist (entry tail)
455 (setq oldval (assq (car entry) frame-initial-frame-alist))
456 (setq newval (cdr (assq (car entry) allparms)))
9688894d
GM
457 (or (and oldval (eq (cdr oldval) newval))
458 (setq newparms
0216b738 459 (cons (cons (car entry) newval) newparms)))))
9688894d 460 (setq newparms (nreverse newparms))
0216b738
CY
461
462 (let ((new-bg (assq 'background-color newparms)))
463 ;; If the `background-color' parameter is changed, apply
464 ;; it first, then make sure that the `background-mode'
465 ;; parameter and other faces are updated, before applying
466 ;; the other parameters.
467 (when new-bg
468 (modify-frame-parameters frame-initial-frame
469 (list new-bg))
470 (unless (assq 'background-mode newparms)
471 (frame-set-background-mode frame-initial-frame))
472 (face-set-after-frame-default frame-initial-frame)
473 (setq newparms (delq new-bg newparms)))
474 (modify-frame-parameters frame-initial-frame newparms)))))
64c669bc 475
7eadab74
JB
476 ;; Restore the original buffer.
477 (set-buffer old-buffer)
478
479 ;; Make sure the initial frame can be GC'd if it is ever deleted.
d202f1f2 480 ;; Make sure frame-notice-user-settings does nothing if called twice.
8f2a992b 481 (setq frame-notice-user-settings nil)
d202f1f2 482 (setq frame-initial-frame nil)))
64c669bc 483
746bd265
KH
484(defun make-initial-minibuffer-frame (display)
485 (let ((parms (append minibuffer-frame-alist '((minibuffer . only)))))
486 (if display
487 (make-frame-on-display display parms)
488 (make-frame parms))))
489
7eadab74 490;;;; Creation of additional frames, and other frame miscellanea
dc6d9681 491
63019bad 492(defun modify-all-frames-parameters (alist)
d398de43 493 "Modify all current and future frames' parameters according to ALIST.
63019bad 494This changes `default-frame-alist' and possibly `initial-frame-alist'.
095fe281
KL
495Furthermore, this function removes all parameters in ALIST from
496`window-system-default-frame-alist'.
63019bad 497See help of `modify-frame-parameters' for more information."
095fe281
KL
498 (dolist (frame (frame-list))
499 (modify-frame-parameters frame alist))
500
501 (dolist (pair alist) ;; conses to add/replace
502 ;; initial-frame-alist needs setting only when
503 ;; frame-notice-user-settings is true.
504 (and frame-notice-user-settings
505 (setq initial-frame-alist
506 (assq-delete-all (car pair) initial-frame-alist)))
507 (setq default-frame-alist
508 (assq-delete-all (car pair) default-frame-alist))
509 ;; Remove any similar settings from the window-system specific
510 ;; parameters---they would override default-frame-alist.
511 (dolist (w window-system-default-frame-alist)
512 (setcdr w (assq-delete-all (car pair) (cdr w)))))
513
63019bad
JB
514 (and frame-notice-user-settings
515 (setq initial-frame-alist (append initial-frame-alist alist)))
516 (setq default-frame-alist (append default-frame-alist alist)))
517
7253d8e0 518(defun get-other-frame ()
40d34803
DL
519 "Return some frame other than the current frame.
520Create one if necessary. Note that the minibuffer frame, if separate,
521is not considered (see `next-frame')."
c8c2aca8 522 (if (equal (next-frame) (selected-frame)) (make-frame) (next-frame)))
64c669bc 523
dc6d9681
JB
524(defun next-multiframe-window ()
525 "Select the next window, regardless of which frame it is on."
64c669bc
JB
526 (interactive)
527 (select-window (next-window (selected-window)
528 (> (minibuffer-depth) 0)
bb2c34b1 529 0))
030f4537 530 (select-frame-set-input-focus (selected-frame)))
64c669bc 531
dc6d9681
JB
532(defun previous-multiframe-window ()
533 "Select the previous window, regardless of which frame it is on."
64c669bc
JB
534 (interactive)
535 (select-window (previous-window (selected-window)
536 (> (minibuffer-depth) 0)
bb2c34b1 537 0))
030f4537 538 (select-frame-set-input-focus (selected-frame)))
64c669bc 539
efc3dd3c
DC
540(defun window-system-for-display (display)
541 "Return the window system for DISPLAY.
542Return nil if we don't know how to interpret DISPLAY."
bf7bea5d
EZ
543 ;; MS-Windows doesn't know how to create a GUI frame in a -nw session.
544 (if (and (eq system-type 'windows-nt)
545 (null (window-system)))
546 nil
547 (cl-loop for descriptor in display-format-alist
548 for pattern = (car descriptor)
549 for system = (cdr descriptor)
550 when (string-match-p pattern display) return system)))
bbf6ef44 551
6d73e337 552(defun make-frame-on-display (display &optional parameters)
c5a8bc01
CY
553 "Make a frame on display DISPLAY.
554The optional argument PARAMETERS specifies additional frame parameters."
6d73e337 555 (interactive "sMake frame on display: ")
efc3dd3c 556 (make-frame (cons (cons 'display display) parameters)))
6d73e337 557
aa360da1
GM
558(declare-function x-close-connection "xfns.c" (terminal))
559
b7de6024
SM
560(defun close-display-connection (display)
561 "Close the connection to a display, deleting all its associated frames.
562For DISPLAY, specify either a frame or a display name (a string).
563If DISPLAY is nil, that stands for the selected frame's display."
564 (interactive
565 (list
566 (let* ((default (frame-parameter nil 'display))
567 (display (completing-read
568 (format "Close display (default %s): " default)
569 (delete-dups
570 (mapcar (lambda (frame)
571 (frame-parameter frame 'display))
572 (frame-list)))
573 nil t nil nil
574 default)))
575 (if (zerop (length display)) default display))))
576 (let ((frames (delq nil
577 (mapcar (lambda (frame)
578 (if (equal display
579 (frame-parameter frame 'display))
580 frame))
581 (frame-list)))))
582 (if (and (consp frames)
583 (not (y-or-n-p (if (cdr frames)
584 (format "Delete %s frames? " (length frames))
585 (format "Delete %s ? " (car frames))))))
586 (error "Abort!")
587 (mapc 'delete-frame frames)
588 (x-close-connection display))))
589
6238bfaf 590(defun make-frame-command ()
e9e6aee8
CY
591 "Make a new frame, on the same terminal as the selected frame.
592If the terminal is a text-only terminal, this also selects the
593new frame."
6238bfaf 594 (interactive)
e9e6aee8 595 (if (display-graphic-p)
6238bfaf
RS
596 (make-frame)
597 (select-frame (make-frame))))
598
45c4fdeb
SM
599(defvar before-make-frame-hook nil
600 "Functions to run before a frame is created.")
601
602(defvar after-make-frame-functions nil
603 "Functions to run after a frame is created.
604The functions are run with one arg, the newly created frame.")
605
72bf1a8b 606(defvar after-setting-font-hook nil
81b99826
GM
607 "Functions to run after a frame's font has been changed.")
608
92e443b1 609;; Alias, kept temporarily.
84ed1560 610(define-obsolete-function-alias 'new-frame 'make-frame "22.1")
bc93c097 611
9e483377 612(defvar frame-inherited-parameters '()
ab6198b2
SM
613 ;; FIXME: Shouldn't we add `font' here as well?
614 "Parameters `make-frame' copies from the `selected-frame' to the new frame.")
615
4bd4c0af
GM
616(defvar x-display-name)
617
45c4fdeb
SM
618(defun make-frame (&optional parameters)
619 "Return a newly created frame displaying the current buffer.
c1ef4455
CY
620Optional argument PARAMETERS is an alist of frame parameters for
621the new frame. Each element of PARAMETERS should have the
622form (NAME . VALUE), for example:
a105105a 623
45c4fdeb 624 (name . STRING) The frame should be named STRING.
43a2e52c 625
45c4fdeb
SM
626 (width . NUMBER) The frame should be NUMBER characters in width.
627 (height . NUMBER) The frame should be NUMBER text lines high.
43a2e52c 628
c1ef4455
CY
629You cannot specify either `width' or `height', you must specify
630neither or both.
43a2e52c 631
45c4fdeb
SM
632 (minibuffer . t) The frame should have a minibuffer.
633 (minibuffer . nil) The frame should have no minibuffer.
634 (minibuffer . only) The frame should contain only a minibuffer.
635 (minibuffer . WINDOW) The frame should use WINDOW as its minibuffer window.
43a2e52c 636
2246281f
KL
637 (window-system . nil) The frame should be displayed on a terminal device.
638 (window-system . x) The frame should be displayed in an X window.
639
efc3dd3c
DC
640 (display . \":0\") The frame should appear on display :0.
641
d51f6378 642 (terminal . TERMINAL) The frame should use the terminal object TERMINAL.
b6660415 643
c1ef4455
CY
644In addition, any parameter specified in `default-frame-alist',
645but not present in PARAMETERS, is applied.
8de9d3f6 646
c1ef4455
CY
647Before creating the frame (via `frame-creation-function-alist'),
648this function runs the hook `before-make-frame-hook'. After
649creating the frame, it runs the hook `after-make-frame-functions'
650with one arg, the newly created frame.
651
efc3dd3c
DC
652If a display parameter is supplied and a window-system is not,
653guess the window-system from the display.
654
c1ef4455
CY
655On graphical displays, this function does not itself make the new
656frame the selected frame. However, the window system may select
657the new frame according to its own rules."
64c669bc 658 (interactive)
efc3dd3c
DC
659 (let* ((display (cdr (assq 'display parameters)))
660 (w (cond
6ed8eeff
KL
661 ((assq 'terminal parameters)
662 (let ((type (terminal-live-p (cdr (assq 'terminal parameters)))))
b6660415
KL
663 (cond
664 ((eq type t) nil)
cb01ce3f
SM
665 ((eq type nil) (error "Terminal %s does not exist"
666 (cdr (assq 'terminal parameters))))
b6660415
KL
667 (t type))))
668 ((assq 'window-system parameters)
669 (cdr (assq 'window-system parameters)))
efc3dd3c
DC
670 (display
671 (or (window-system-for-display display)
672 (error "Don't know how to interpret display \"%S\""
673 display)))
b6660415 674 (t window-system)))
2246281f 675 (frame-creation-function (cdr (assq w frame-creation-function-alist)))
da8e8fc1 676 (oldframe (selected-frame))
c1ef4455 677 (params parameters)
2246281f
KL
678 frame)
679 (unless frame-creation-function
680 (error "Don't know how to create a frame on window system %s" w))
efc3dd3c
DC
681
682 (unless (get w 'window-system-initialized)
ebb19708
DA
683 (funcall (cdr (assq w window-system-initialization-alist)) display)
684 (setq x-display-name display)
efc3dd3c
DC
685 (put w 'window-system-initialized t))
686
c1ef4455
CY
687 ;; Add parameters from `window-system-default-frame-alist'.
688 (dolist (p (cdr (assq w window-system-default-frame-alist)))
21bd02a6 689 (unless (assq (car p) params)
c1ef4455
CY
690 (push p params)))
691 ;; Add parameters from `default-frame-alist'.
692 (dolist (p default-frame-alist)
21bd02a6 693 (unless (assq (car p) params)
c1ef4455
CY
694 (push p params)))
695 ;; Now make the frame.
2246281f 696 (run-hooks 'before-make-frame-hook)
c1ef4455 697 (setq frame (funcall frame-creation-function params))
30a2fded 698 (normal-erase-is-backspace-setup-frame frame)
ab6198b2
SM
699 ;; Inherit the original frame's parameters.
700 (dolist (param frame-inherited-parameters)
701 (unless (assq param parameters) ;Overridden by explicit parameters.
702 (let ((val (frame-parameter oldframe param)))
703 (when val (set-frame-parameter frame param val)))))
45c4fdeb
SM
704 (run-hook-with-args 'after-make-frame-functions frame)
705 frame))
64c669bc 706
7eadab74
JB
707(defun filtered-frame-list (predicate)
708 "Return a list of all live frames which satisfy PREDICATE."
047bc928
GM
709 (let* ((frames (frame-list))
710 (list frames))
7eadab74 711 (while (consp frames)
047bc928
GM
712 (unless (funcall predicate (car frames))
713 (setcar frames nil))
7eadab74 714 (setq frames (cdr frames)))
047bc928 715 (delq nil list)))
7eadab74
JB
716
717(defun minibuffer-frame-list ()
718 "Return a list of all frames with their own minibuffers."
719 (filtered-frame-list
b7de6024
SM
720 (lambda (frame)
721 (eq frame (window-frame (minibuffer-window frame))))))
7eadab74 722
371fed4e
SM
723;; Used to be called `terminal-id' in termdev.el.
724(defun get-device-terminal (device)
725 "Return the terminal corresponding to DEVICE.
726DEVICE can be a terminal, a frame, nil (meaning the selected frame's terminal),
727the name of an X display device (HOST.SERVER.SCREEN) or a tty device file."
728 (cond
729 ((or (null device) (framep device))
730 (frame-terminal device))
731 ((stringp device)
732 (let ((f (car (filtered-frame-list
733 (lambda (frame)
734 (or (equal (frame-parameter frame 'display) device)
735 (equal (frame-parameter frame 'tty) device)))))))
736 (or f (error "Display %s does not exist" device))
737 (frame-terminal f)))
738 ((terminal-live-p device) device)
739 (t
740 (error "Invalid argument %s in `get-device-terminal'" device))))
741
742(defun frames-on-display-list (&optional device)
743 "Return a list of all frames on DEVICE.
744
745DEVICE should be a terminal, a frame,
746or a name of an X display or tty (a string of the form
9684e4c9 747HOST:SERVER.SCREEN).
b6660415 748
371fed4e 749If DEVICE is omitted or nil, it defaults to the selected
9684e4c9 750frame's terminal device."
371fed4e 751 (let* ((terminal (get-device-terminal device))
047bc928 752 (func #'(lambda (frame)
6ed8eeff 753 (eq (frame-terminal frame) terminal))))
9911648b
EZ
754 (filtered-frame-list func)))
755
f35ca2fe
KL
756(defun framep-on-display (&optional terminal)
757 "Return the type of frames on TERMINAL.
758TERMINAL may be a terminal id, a display name or a frame. If it
759is a frame, its type is returned. If TERMINAL is omitted or nil,
760it defaults to the selected frame's terminal device. All frames
761on a given display are of the same type."
6ed8eeff 762 (or (terminal-live-p terminal)
f35ca2fe
KL
763 (framep terminal)
764 (framep (car (frames-on-display-list terminal)))))
9911648b 765
58bf6042
JB
766(defun frame-remove-geometry-params (param-list)
767 "Return the parameter list PARAM-LIST, but with geometry specs removed.
768This deletes all bindings in PARAM-LIST for `top', `left', `width',
791e09d8 769`height', `user-size' and `user-position' parameters.
58bf6042
JB
770Emacs uses this to avoid overriding explicit moves and resizings from
771the user during startup."
772 (setq param-list (cons nil param-list))
773 (let ((tail param-list))
774 (while (consp (cdr tail))
775 (if (and (consp (car (cdr tail)))
791e09d8
RS
776 (memq (car (car (cdr tail)))
777 '(height width top left user-position user-size)))
778 (progn
779 (setq frame-initial-geometry-arguments
780 (cons (car (cdr tail)) frame-initial-geometry-arguments))
781 (setcdr tail (cdr (cdr tail))))
58bf6042 782 (setq tail (cdr tail)))))
e11c3dc2
KH
783 (setq frame-initial-geometry-arguments
784 (nreverse frame-initial-geometry-arguments))
58bf6042
JB
785 (cdr param-list))
786
fcd42c11 787(declare-function x-focus-frame "frame.c" (frame))
aa360da1 788
be39b8cc 789(defun select-frame-set-input-focus (frame &optional norecord)
bbca16d8 790 "Select FRAME, raise it, and set input focus, if possible.
e343066f 791If `mouse-autoselect-window' is non-nil, also move mouse pointer
bbca16d8 792to FRAME's selected window. Otherwise, if `focus-follows-mouse'
be39b8cc
MR
793is non-nil, move mouse cursor to FRAME.
794
795Optional argument NORECORD means to neither change the order of
796recently selected windows nor the buffer list."
797 (select-frame frame norecord)
b0c7121c 798 (raise-frame frame)
bbca16d8 799 ;; Ensure, if possible, that FRAME gets input focus.
b0c7121c
MR
800 (when (memq (window-system frame) '(x w32 ns))
801 (x-focus-frame frame))
bbca16d8
MR
802 ;; Move mouse cursor if necessary.
803 (cond
804 (mouse-autoselect-window
805 (let ((edges (window-inside-edges (frame-selected-window frame))))
806 ;; Move mouse cursor into FRAME's selected window to avoid that
807 ;; Emacs mouse-autoselects another window.
808 (set-mouse-position frame (nth 2 edges) (nth 1 edges))))
809 (focus-follows-mouse
810 ;; Move mouse cursor into FRAME to avoid that another frame gets
811 ;; selected by the window manager.
812 (set-mouse-position frame (1- (frame-width frame)) 0))))
030f4537 813
ceab6935 814(defun other-frame (arg)
c2c93894 815 "Select the ARGth different visible frame on current display, and raise it.
ceab6935
RM
816All frames are arranged in a cyclic order.
817This command selects the frame ARG steps away in that order.
bb578a72
RS
818A negative ARG moves in the opposite order.
819
820To make this command work properly, you must tell Emacs
821how the system (or the window manager) generally handles
822focus-switching between windows. If moving the mouse onto a window
823selects it (gives it focus), set `focus-follows-mouse' to t.
824Otherwise, that variable should be nil."
ceab6935
RM
825 (interactive "p")
826 (let ((frame (selected-frame)))
827 (while (> arg 0)
a569dbc3
RM
828 (setq frame (next-frame frame))
829 (while (not (eq (frame-visible-p frame) t))
830 (setq frame (next-frame frame)))
831 (setq arg (1- arg)))
ceab6935 832 (while (< arg 0)
a569dbc3
RM
833 (setq frame (previous-frame frame))
834 (while (not (eq (frame-visible-p frame) t))
835 (setq frame (previous-frame frame)))
915cfd1f 836 (setq arg (1+ arg)))
030f4537 837 (select-frame-set-input-focus frame)))
845cde06 838
3db7df06
SM
839(defun iconify-or-deiconify-frame ()
840 "Iconify the selected frame, or deiconify if it's currently an icon."
841 (interactive)
842 (if (eq (cdr (assq 'visibility (frame-parameters))) t)
843 (iconify-frame)
844 (make-frame-visible)))
845
0b0d3e0b
KL
846(defun suspend-frame ()
847 "Do whatever is right to suspend the current frame.
f35ca2fe
KL
848Calls `suspend-emacs' if invoked from the controlling tty device,
849`suspend-tty' from a secondary tty device, and
0b0d3e0b
KL
850`iconify-or-deiconify-frame' from an X frame."
851 (interactive)
852 (let ((type (framep (selected-frame))))
853 (cond
edfda783 854 ((memq type '(x ns w32)) (iconify-or-deiconify-frame))
0b0d3e0b 855 ((eq type t)
6ed8eeff 856 (if (controlling-tty-p)
4a933ef8
KL
857 (suspend-emacs)
858 (suspend-tty)))
0b0d3e0b
KL
859 (t (suspend-emacs)))))
860
845cde06 861(defun make-frame-names-alist ()
e2c3f530 862 ;; Only consider the frames on the same display.
845cde06
EZ
863 (let* ((current-frame (selected-frame))
864 (falist
865 (cons
866 (cons (frame-parameter current-frame 'name) current-frame) nil))
e2c3f530 867 (frame (next-frame nil 0)))
845cde06
EZ
868 (while (not (eq frame current-frame))
869 (progn
e2c3f530
SM
870 (push (cons (frame-parameter frame 'name) frame) falist)
871 (setq frame (next-frame frame 0))))
845cde06
EZ
872 falist))
873
874(defvar frame-name-history nil)
845cde06 875(defun select-frame-by-name (name)
563283b1 876 "Select the frame on the current terminal whose name is NAME and raise it.
845cde06
EZ
877If there is no frame by that name, signal an error."
878 (interactive
716ff1c9
RS
879 (let* ((frame-names-alist (make-frame-names-alist))
880 (default (car (car frame-names-alist)))
881 (input (completing-read
882 (format "Select Frame (default %s): " default)
883 frame-names-alist nil t nil 'frame-name-history)))
845cde06
EZ
884 (if (= (length input) 0)
885 (list default)
886 (list input))))
716ff1c9
RS
887 (let* ((frame-names-alist (make-frame-names-alist))
888 (frame (cdr (assoc name frame-names-alist))))
bbca16d8
MR
889 (if frame
890 (select-frame-set-input-focus frame)
891 (error "There is no frame named `%s'" name))))
1485f4c0
CY
892
893\f
894;;;; Background mode.
895
896(defcustom frame-background-mode nil
897 "The brightness of the background.
898Set this to the symbol `dark' if your background color is dark,
899`light' if your background is light, or nil (automatic by default)
e73c3a0d
GM
900if you want Emacs to examine the brightness for you.
901
79a15a36 902If you change this without using customize, you should use
2e4a0a90
GM
903`frame-set-background-mode' to update existing frames;
904e.g. (mapc 'frame-set-background-mode (frame-list))."
1485f4c0
CY
905 :group 'faces
906 :set #'(lambda (var value)
907 (set-default var value)
908 (mapc 'frame-set-background-mode (frame-list)))
909 :initialize 'custom-initialize-changed
910 :type '(choice (const dark)
911 (const light)
912 (const :tag "automatic" nil)))
913
914(declare-function x-get-resource "frame.c"
915 (attribute class &optional component subclass))
916
e740f9d2
GM
917;; Only used if window-system is not null.
918(declare-function x-display-grayscale-p "xfns.c" (&optional terminal))
919
1485f4c0
CY
920(defvar inhibit-frame-set-background-mode nil)
921
922(defun frame-set-background-mode (frame &optional keep-face-specs)
923 "Set up display-dependent faces on FRAME.
924Display-dependent faces are those which have different definitions
925according to the `background-mode' and `display-type' frame parameters.
926
927If optional arg KEEP-FACE-SPECS is non-nil, don't recalculate
928face specs for the new background mode."
929 (unless inhibit-frame-set-background-mode
930 (let* ((frame-default-bg-mode (frame-terminal-default-bg-mode frame))
931 (bg-color (frame-parameter frame 'background-color))
932 (tty-type (tty-type frame))
933 (default-bg-mode
934 (if (or (window-system frame)
935 (and tty-type
936 (string-match "^\\(xterm\\|\\rxvt\\|dtterm\\|eterm\\)"
937 tty-type)))
938 'light
939 'dark))
940 (non-default-bg-mode (if (eq default-bg-mode 'light) 'dark 'light))
941 (bg-mode
942 (cond (frame-default-bg-mode)
943 ((equal bg-color "unspecified-fg") ; inverted colors
944 non-default-bg-mode)
945 ((not (color-values bg-color frame))
946 default-bg-mode)
947 ((>= (apply '+ (color-values bg-color frame))
948 ;; Just looking at the screen, colors whose
949 ;; values add up to .6 of the white total
950 ;; still look dark to me.
951 (* (apply '+ (color-values "white" frame)) .6))
952 'light)
953 (t 'dark)))
954 (display-type
955 (cond ((null (window-system frame))
956 (if (tty-display-color-p frame) 'color 'mono))
957 ((display-color-p frame)
958 'color)
959 ((x-display-grayscale-p frame)
960 'grayscale)
961 (t 'mono)))
962 (old-bg-mode
963 (frame-parameter frame 'background-mode))
964 (old-display-type
965 (frame-parameter frame 'display-type)))
966
967 (unless (and (eq bg-mode old-bg-mode) (eq display-type old-display-type))
968 (let ((locally-modified-faces nil)
969 ;; Prevent face-spec-recalc from calling this function
970 ;; again, resulting in a loop (bug#911).
971 (inhibit-frame-set-background-mode t)
972 (params (list (cons 'background-mode bg-mode)
973 (cons 'display-type display-type))))
974 (if keep-face-specs
975 (modify-frame-parameters frame params)
976 ;; If we are recomputing face specs, first collect a list
977 ;; of faces that don't match their face-specs. These are
978 ;; the faces modified on FRAME, and we avoid changing them
979 ;; below. Use a negative list to avoid consing (we assume
980 ;; most faces are unmodified).
981 (dolist (face (face-list))
982 (and (not (get face 'face-override-spec))
983 (not (face-spec-match-p face
984 (face-user-default-spec face)
985 (selected-frame)))
986 (push face locally-modified-faces)))
987 ;; Now change to the new frame parameters
988 (modify-frame-parameters frame params)
989 ;; For all unmodified named faces, choose face specs
990 ;; matching the new frame parameters.
991 (dolist (face (face-list))
992 (unless (memq face locally-modified-faces)
993 (face-spec-recalc face frame)))))))))
994
995(defun frame-terminal-default-bg-mode (frame)
996 "Return the default background mode of FRAME.
997This checks the `frame-background-mode' variable, the X resource
998named \"backgroundMode\" (if FRAME is an X frame), and finally
999the `background-mode' terminal parameter."
1000 (or frame-background-mode
1001 (let ((bg-resource
1002 (and (window-system frame)
1003 (x-get-resource "backgroundMode" "BackgroundMode"))))
1004 (if bg-resource
1005 (intern (downcase bg-resource))))
1006 (terminal-parameter frame 'background-mode)))
1007
64c669bc 1008\f
dc6d9681
JB
1009;;;; Frame configurations
1010
1011(defun current-frame-configuration ()
1012 "Return a list describing the positions and states of all frames.
376a7584
JB
1013Its car is `frame-configuration'.
1014Each element of the cdr is a list of the form (FRAME ALIST WINDOW-CONFIG),
1015where
1016 FRAME is a frame object,
1017 ALIST is an association list specifying some of FRAME's parameters, and
1018 WINDOW-CONFIG is a window configuration object for FRAME."
1019 (cons 'frame-configuration
b7de6024
SM
1020 (mapcar (lambda (frame)
1021 (list frame
1022 (frame-parameters frame)
1023 (current-window-configuration frame)))
376a7584 1024 (frame-list))))
dc6d9681 1025
68cd265f 1026(defun set-frame-configuration (configuration &optional nodelete)
dc6d9681
JB
1027 "Restore the frames to the state described by CONFIGURATION.
1028Each frame listed in CONFIGURATION has its position, size, window
68cd265f 1029configuration, and other parameters set as specified in CONFIGURATION.
d398de43
LT
1030However, this function does not restore deleted frames.
1031
a78db71c
RS
1032Ordinarily, this function deletes all existing frames not
1033listed in CONFIGURATION. But if optional second argument NODELETE
5da841d2 1034is given and non-nil, the unwanted frames are iconified instead."
376a7584
JB
1035 (or (frame-configuration-p configuration)
1036 (signal 'wrong-type-argument
1037 (list 'frame-configuration-p configuration)))
1038 (let ((config-alist (cdr configuration))
1039 frames-to-delete)
b7de6024
SM
1040 (dolist (frame (frame-list))
1041 (let ((parameters (assq frame config-alist)))
1042 (if parameters
1043 (progn
1044 (modify-frame-parameters
1045 frame
1046 ;; Since we can't set a frame's minibuffer status,
1047 ;; we might as well omit the parameter altogether.
1048 (let* ((parms (nth 1 parameters))
b2529d56
MB
1049 (mini (assq 'minibuffer parms))
1050 (name (assq 'name parms))
1051 (explicit-name (cdr (assq 'explicit-name parms))))
1052 (when mini (setq parms (delq mini parms)))
1053 ;; Leave name in iff it was set explicitly.
1054 ;; This should fix the behavior reported in
1055 ;; http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg01632.html
1056 (when (and name (not explicit-name))
1057 (setq parms (delq name parms)))
b7de6024
SM
1058 parms))
1059 (set-window-configuration (nth 2 parameters)))
1060 (setq frames-to-delete (cons frame frames-to-delete)))))
1061 (mapc (if nodelete
1062 ;; Note: making frames invisible here was tried
1063 ;; but led to some strange behavior--each time the frame
1064 ;; was made visible again, the window manager asked afresh
1065 ;; for where to put it.
1066 'iconify-frame
1067 'delete-frame)
1068 frames-to-delete)))
64c669bc 1069\f
dc6d9681
JB
1070;;;; Convenience functions for accessing and interactively changing
1071;;;; frame parameters.
64c669bc 1072
151bdc83 1073(defun frame-height (&optional frame)
dc6d9681 1074 "Return number of lines available for display on FRAME.
4619ff5e
GM
1075If FRAME is omitted, describe the currently selected frame.
1076Exactly what is included in the return value depends on the
1077window-system and toolkit in use - see `frame-pixel-height' for
1078more details. The lines are in units of the default font height.
1079
1080The result is roughly related to the frame pixel height via
1081height in pixels = height in lines * `frame-char-height'.
1082However, this is only approximate, and is complicated e.g. by the
1083fact that individual window lines and menu bar lines can have
1084differing font heights."
151bdc83 1085 (cdr (assq 'height (frame-parameters frame))))
dc6d9681
JB
1086
1087(defun frame-width (&optional frame)
1088 "Return number of columns available for display on FRAME.
1089If FRAME is omitted, describe the currently selected frame."
151bdc83 1090 (cdr (assq 'width (frame-parameters frame))))
dc6d9681 1091
aa360da1
GM
1092(declare-function x-list-fonts "xfaces.c"
1093 (pattern &optional face frame maximum width))
1094
52ef9375 1095(define-obsolete-function-alias 'set-default-font 'set-frame-font "23.1")
fce3fdeb 1096
f2045622
CY
1097(defun set-frame-font (font &optional keep-size frames)
1098 "Set the default font to FONT.
fce3fdeb 1099When called interactively, prompt for the name of a font, and use
f2045622
CY
1100that font on the selected frame. When called from Lisp, FONT
1101should be a font name (a string), a font object, font entity, or
1102font spec.
fce3fdeb
CY
1103
1104If KEEP-SIZE is nil, keep the number of frame lines and columns
1105fixed. If KEEP-SIZE is non-nil (or with a prefix argument), try
1106to keep the current frame size fixed (in pixels) by adjusting the
1107number of lines and columns.
1108
9f562668
CY
1109If FRAMES is nil, apply the font to the selected frame only.
1110If FRAMES is non-nil, it should be a list of frames to act upon,
1111or t meaning all graphical frames. Also, if FRAME is non-nil,
1112alter the user's Customization settings as though the
fce3fdeb
CY
1113font-related attributes of the `default' face had been \"set in
1114this session\", so that the font is applied to future frames."
f39784dd 1115 (interactive
1cef6541
JB
1116 (let* ((completion-ignore-case t)
1117 (font (completing-read "Font name: "
1cef6541
JB
1118 ;; x-list-fonts will fail with an error
1119 ;; if this frame doesn't support fonts.
b7de6024
SM
1120 (x-list-fonts "*" nil (selected-frame))
1121 nil nil nil nil
1122 (frame-parameter nil 'font))))
fce3fdeb 1123 (list font current-prefix-arg nil)))
f2045622 1124 (when (or (stringp font) (fontp font))
fce3fdeb 1125 (let* ((this-frame (selected-frame))
9f562668
CY
1126 ;; FRAMES nil means affect the selected frame.
1127 (frame-list (cond ((null frames)
1128 (list this-frame))
1129 ((eq frames t)
1130 (frame-list))
1131 (t frames)))
fce3fdeb 1132 height width)
9f562668 1133 (dolist (f frame-list)
fce3fdeb
CY
1134 (when (display-multi-font-p f)
1135 (if keep-size
1136 (setq height (* (frame-parameter f 'height)
1137 (frame-char-height f))
1138 width (* (frame-parameter f 'width)
1139 (frame-char-width f))))
1140 ;; When set-face-attribute is called for :font, Emacs
1141 ;; guesses the best font according to other face attributes
1142 ;; (:width, :weight, etc.) so reset them too (Bug#2476).
1143 (set-face-attribute 'default f
1144 :width 'normal :weight 'normal
f2045622 1145 :slant 'normal :font font)
fce3fdeb
CY
1146 (if keep-size
1147 (modify-frame-parameters
1148 f
1149 (list (cons 'height (round height (frame-char-height f)))
1150 (cons 'width (round width (frame-char-width f))))))))
9f562668 1151 (when frames
fce3fdeb
CY
1152 ;; Alter the user's Custom setting of the `default' face, but
1153 ;; only for font-related attributes.
1154 (let ((specs (cadr (assq 'user (get 'default 'theme-face))))
1155 (attrs '(:family :foundry :slant :weight :height :width))
1156 (new-specs nil))
1157 (if (null specs) (setq specs '((t nil))))
1158 (dolist (spec specs)
1159 ;; Each SPEC has the form (DISPLAY ATTRIBUTE-PLIST)
1160 (let ((display (nth 0 spec))
1161 (plist (copy-tree (nth 1 spec))))
1162 ;; Alter only DISPLAY conditions matching this frame.
1163 (when (or (memq display '(t default))
1164 (face-spec-set-match-display display this-frame))
1165 (dolist (attr attrs)
1166 (setq plist (plist-put plist attr
1167 (face-attribute 'default attr)))))
1168 (push (list display plist) new-specs)))
1169 (setq new-specs (nreverse new-specs))
1170 (put 'default 'customized-face new-specs)
1171 (custom-push-theme 'theme-face 'default 'user 'set new-specs)
1172 (put 'default 'face-modified nil))))
1173 (run-hooks 'after-setting-font-hook 'after-setting-font-hooks)))
64c669bc 1174
7e573c4a 1175(defun set-frame-parameter (frame parameter value)
0dac35b8
KL
1176 "Set frame parameter PARAMETER to VALUE on FRAME.
1177If FRAME is nil, it defaults to the selected frame.
c2c93894 1178See `modify-frame-parameters'."
7e573c4a
SM
1179 (modify-frame-parameters frame (list (cons parameter value))))
1180
8290babd 1181(defun set-background-color (color-name)
40d34803 1182 "Set the background color of the selected frame to COLOR-NAME.
61298010
RS
1183When called interactively, prompt for the name of the color to use.
1184To get the frame's current background color, use `frame-parameters'."
9317e499 1185 (interactive (list (read-color "Background color: ")))
dc6d9681 1186 (modify-frame-parameters (selected-frame)
528e1416
EZ
1187 (list (cons 'background-color color-name)))
1188 (or window-system
1189 (face-set-after-frame-default (selected-frame))))
64c669bc 1190
8290babd 1191(defun set-foreground-color (color-name)
40d34803 1192 "Set the foreground color of the selected frame to COLOR-NAME.
61298010
RS
1193When called interactively, prompt for the name of the color to use.
1194To get the frame's current foreground color, use `frame-parameters'."
9317e499 1195 (interactive (list (read-color "Foreground color: ")))
dc6d9681 1196 (modify-frame-parameters (selected-frame)
528e1416
EZ
1197 (list (cons 'foreground-color color-name)))
1198 (or window-system
1199 (face-set-after-frame-default (selected-frame))))
64c669bc
JB
1200
1201(defun set-cursor-color (color-name)
40d34803 1202 "Set the text cursor color of the selected frame to COLOR-NAME.
61298010 1203When called interactively, prompt for the name of the color to use.
2680c309
CY
1204This works by setting the `cursor-color' frame parameter on the
1205selected frame.
1206
1207You can also set the text cursor color, for all frames, by
1208customizing the `cursor' face."
9317e499 1209 (interactive (list (read-color "Cursor color: ")))
dc6d9681 1210 (modify-frame-parameters (selected-frame)
7eadab74 1211 (list (cons 'cursor-color color-name))))
64c669bc 1212
eaa974e1 1213(defun set-mouse-color (color-name)
40d34803 1214 "Set the color of the mouse pointer of the selected frame to COLOR-NAME.
61298010
RS
1215When called interactively, prompt for the name of the color to use.
1216To get the frame's current mouse color, use `frame-parameters'."
9317e499 1217 (interactive (list (read-color "Mouse color: ")))
dc6d9681 1218 (modify-frame-parameters (selected-frame)
ec6d4463
KH
1219 (list (cons 'mouse-color
1220 (or color-name
1221 (cdr (assq 'mouse-color
1222 (frame-parameters))))))))
7eadab74 1223
eaa974e1 1224(defun set-border-color (color-name)
40d34803 1225 "Set the color of the border of the selected frame to COLOR-NAME.
61298010
RS
1226When called interactively, prompt for the name of the color to use.
1227To get the frame's current border color, use `frame-parameters'."
9317e499 1228 (interactive (list (read-color "Border color: ")))
eaa974e1
JB
1229 (modify-frame-parameters (selected-frame)
1230 (list (cons 'border-color color-name))))
1231
f44379e7 1232(define-minor-mode auto-raise-mode
ed472be9 1233 "Toggle whether or not selected frames should auto-raise.
06e21633
CY
1234With a prefix argument ARG, enable Auto Raise mode if ARG is
1235positive, and disable it otherwise. If called from Lisp, enable
1236the mode if ARG is omitted or nil.
1237
ed472be9
CY
1238Auto Raise mode does nothing under most window managers, which
1239switch focus on mouse clicks. It only has an effect if your
1240window manager switches focus on mouse movement (in which case
1241you should also change `focus-follows-mouse' to t). Then,
1242enabling Auto Raise mode causes any graphical Emacs frame which
1243acquires focus to be automatically raised.
1244
1245Note that this minor mode controls Emacs's own auto-raise
1246feature. Window managers that switch focus on mouse movement
1247often have their own auto-raise feature."
f44379e7
SM
1248 :variable (frame-parameter nil 'auto-raise)
1249 (if (frame-parameter nil 'auto-raise)
1250 (raise-frame)))
7eadab74 1251
f44379e7 1252(define-minor-mode auto-lower-mode
7eadab74 1253 "Toggle whether or not the selected frame should auto-lower.
06e21633
CY
1254With a prefix argument ARG, enable Auto Lower mode if ARG is
1255positive, and disable it otherwise. If called from Lisp, enable
1256the mode if ARG is omitted or nil.
1257
ed472be9
CY
1258Auto Lower mode does nothing under most window managers, which
1259switch focus on mouse clicks. It only has an effect if your
1260window manager switches focus on mouse movement (in which case
1261you should also change `focus-follows-mouse' to t). Then,
1262enabling Auto Lower Mode causes any graphical Emacs frame which
1263loses focus to be automatically lowered.
1264
1265Note that this minor mode controls Emacs's own auto-lower
1266feature. Window managers that switch focus on mouse movement
1267often have their own features for raising or lowering frames."
f44379e7
SM
1268 :variable (frame-parameter nil 'auto-lower))
1269
7777d03b
EZ
1270(defun set-frame-name (name)
1271 "Set the name of the selected frame to NAME.
1272When called interactively, prompt for the name of the frame.
37269466
CY
1273On text terminals, the frame name is displayed on the mode line.
1274On graphical displays, it is displayed on the frame's title bar."
7777d03b
EZ
1275 (interactive "sFrame name: ")
1276 (modify-frame-parameters (selected-frame)
1277 (list (cons 'name name))))
56cfea72
KS
1278
1279(defun frame-current-scroll-bars (&optional frame)
1280 "Return the current scroll-bar settings in frame FRAME.
1cecf04d 1281Value is a cons (VERTICAL . HORIZ0NTAL) where VERTICAL specifies the
56cfea72 1282current location of the vertical scroll-bars (left, right, or nil),
1cecf04d 1283and HORIZONTAL specifies the current location of the horizontal scroll
56cfea72
KS
1284bars (top, bottom, or nil)."
1285 (let ((vert (frame-parameter frame 'vertical-scroll-bars))
1286 (hor nil))
1287 (unless (memq vert '(left right nil))
1288 (setq vert default-frame-scroll-bars))
1289 (cons vert hor)))
4e3f9230
YM
1290
1291(defun frame-monitor-attributes (&optional frame)
1292 "Return the attributes of the physical monitor dominating FRAME.
1293If FRAME is omitted, describe the currently selected frame.
1294
1295A frame is dominated by a physical monitor when either the
1296largest area of the frame resides in the monitor, or the monitor
1297is the closest to the frame if the frame does not intersect any
1298physical monitors.
1299
1300See `display-monitor-attributes-list' for the list of attribute
1301keys and their meanings."
1302 (or frame (setq frame (selected-frame)))
1303 (cl-loop for attributes in (display-monitor-attributes-list frame)
1304 for frames = (cdr (assq 'frames attributes))
1305 if (memq frame frames) return attributes))
1306
64c669bc 1307\f
9911648b 1308;;;; Frame/display capabilities.
b6660415 1309
73e6adaa
DN
1310(declare-function msdos-mouse-p "dosfns.c")
1311
9911648b
EZ
1312(defun display-mouse-p (&optional display)
1313 "Return non-nil if DISPLAY has a mouse available.
1314DISPLAY can be a display name, a frame, or nil (meaning the selected
1315frame's display)."
1316 (let ((frame-type (framep-on-display display)))
1317 (cond
1318 ((eq frame-type 'pc)
1319 (msdos-mouse-p))
0fda9b75 1320 ((eq frame-type 'w32)
9fa5bb32
RS
1321 (with-no-warnings
1322 (> w32-num-mouse-buttons 0)))
9e2a2647
DN
1323 ((memq frame-type '(x ns))
1324 t) ;; We assume X and NeXTstep *always* have a pointing device
9911648b 1325 (t
7565ee93
DL
1326 (or (and (featurep 'xt-mouse)
1327 xterm-mouse-mode)
1328 ;; t-mouse is distributed with the GPM package. It doesn't have
1329 ;; a toggle.
ffe759eb
EZ
1330 (featurep 't-mouse)
1331 ;; No way to check whether a w32 console has a mouse, assume
1332 ;; it always does.
1333 (boundp 'w32-use-full-screen-buffer))))))
9911648b
EZ
1334
1335(defun display-popup-menus-p (&optional display)
1336 "Return non-nil if popup menus are supported on DISPLAY.
1337DISPLAY can be a display name, a frame, or nil (meaning the selected
1338frame's display).
1339Support for popup menus requires that the mouse be available."
ffe759eb 1340 (display-mouse-p display))
9911648b
EZ
1341
1342(defun display-graphic-p (&optional display)
1343 "Return non-nil if DISPLAY is a graphic display.
1344Graphical displays are those which are capable of displaying several
1345frames and several different fonts at once. This is true for displays
1346that use a window system such as X, and false for text-only terminals.
1347DISPLAY can be a display name, a frame, or nil (meaning the selected
1348frame's display)."
9e2a2647 1349 (not (null (memq (framep-on-display display) '(x w32 ns)))))
9911648b 1350
ddc456e4
EZ
1351(defun display-images-p (&optional display)
1352 "Return non-nil if DISPLAY can display images.
1353
1354DISPLAY can be a display name, a frame, or nil (meaning the selected
1355frame's display)."
1356 (and (display-graphic-p display)
1357 (fboundp 'image-mask-p)
fcc6f5cc 1358 (fboundp 'image-size)))
ddc456e4 1359
6693b279
EZ
1360(defalias 'display-multi-frame-p 'display-graphic-p)
1361(defalias 'display-multi-font-p 'display-graphic-p)
1362
9911648b
EZ
1363(defun display-selections-p (&optional display)
1364 "Return non-nil if DISPLAY supports selections.
1365A selection is a way to transfer text or other data between programs
45240125 1366via special system buffers called `selection' or `clipboard'.
9911648b
EZ
1367DISPLAY can be a display name, a frame, or nil (meaning the selected
1368frame's display)."
1369 (let ((frame-type (framep-on-display display)))
1370 (cond
1371 ((eq frame-type 'pc)
1372 ;; MS-DOG frames support selections when Emacs runs inside
1373 ;; the Windows' DOS Box.
9fa5bb32
RS
1374 (with-no-warnings
1375 (not (null dos-windows-version))))
9e2a2647 1376 ((memq frame-type '(x w32 ns))
9911648b
EZ
1377 t) ;; FIXME?
1378 (t
1379 nil))))
1380
aa360da1
GM
1381(declare-function x-display-screens "xfns.c" (&optional terminal))
1382
9911648b 1383(defun display-screens (&optional display)
d40a46d7
XF
1384 "Return the number of screens associated with DISPLAY.
1385If DISPLAY is omitted or nil, it defaults to the selected frame's display."
9911648b
EZ
1386 (let ((frame-type (framep-on-display display)))
1387 (cond
9e2a2647 1388 ((memq frame-type '(x w32 ns))
9911648b 1389 (x-display-screens display))
bb9404d6 1390 (t
9911648b
EZ
1391 1))))
1392
aa360da1
GM
1393(declare-function x-display-pixel-height "xfns.c" (&optional terminal))
1394
9911648b
EZ
1395(defun display-pixel-height (&optional display)
1396 "Return the height of DISPLAY's screen in pixels.
cf13177e
YM
1397For character terminals, each character counts as a single pixel.
1398For graphical terminals, note that on \"multi-monitor\" setups this
1399refers to the pixel height for all physical monitors associated
1400with DISPLAY. To get information for each physical monitor, use
d40a46d7
XF
1401`display-monitor-attributes-list'.
1402If DISPLAY is omitted or nil, it defaults to the selected frame's display."
9911648b
EZ
1403 (let ((frame-type (framep-on-display display)))
1404 (cond
9e2a2647 1405 ((memq frame-type '(x w32 ns))
9911648b
EZ
1406 (x-display-pixel-height display))
1407 (t
1408 (frame-height (if (framep display) display (selected-frame)))))))
1409
aa360da1
GM
1410(declare-function x-display-pixel-width "xfns.c" (&optional terminal))
1411
9911648b
EZ
1412(defun display-pixel-width (&optional display)
1413 "Return the width of DISPLAY's screen in pixels.
cf13177e
YM
1414For character terminals, each character counts as a single pixel.
1415For graphical terminals, note that on \"multi-monitor\" setups this
1416refers to the pixel width for all physical monitors associated
1417with DISPLAY. To get information for each physical monitor, use
d40a46d7
XF
1418`display-monitor-attributes-list'.
1419If DISPLAY is omitted or nil, it defaults to the selected frame's display."
9911648b
EZ
1420 (let ((frame-type (framep-on-display display)))
1421 (cond
9e2a2647 1422 ((memq frame-type '(x w32 ns))
9911648b
EZ
1423 (x-display-pixel-width display))
1424 (t
1425 (frame-width (if (framep display) display (selected-frame)))))))
1426
51e39dfc
KS
1427(defcustom display-mm-dimensions-alist nil
1428 "Alist for specifying screen dimensions in millimeters.
1429The dimensions will be used for `display-mm-height' and
1430`display-mm-width' if defined for the respective display.
1431
1432Each element of the alist has the form (display . (width . height)),
1433e.g. (\":0.0\" . (287 . 215)).
1434
1435If `display' equals t, it specifies dimensions for all graphical
c7015153 1436displays not explicitly specified."
51e39dfc
KS
1437 :version "22.1"
1438 :type '(alist :key-type (choice (string :tag "Display name")
1439 (const :tag "Default" t))
1440 :value-type (cons :tag "Dimensions"
1441 (integer :tag "Width")
1442 (integer :tag "Height")))
1443 :group 'frames)
1444
aa360da1
GM
1445(declare-function x-display-mm-height "xfns.c" (&optional terminal))
1446
9911648b
EZ
1447(defun display-mm-height (&optional display)
1448 "Return the height of DISPLAY's screen in millimeters.
d7cd2d8b 1449System values can be overridden by `display-mm-dimensions-alist'.
cf13177e
YM
1450If the information is unavailable, value is nil.
1451For graphical terminals, note that on \"multi-monitor\" setups this
1452refers to the height in millimeters for all physical monitors
1453associated with DISPLAY. To get information for each physical
d40a46d7
XF
1454monitor, use `display-monitor-attributes-list'.
1455If DISPLAY is omitted or nil, it defaults to the selected frame's display."
9e2a2647 1456 (and (memq (framep-on-display display) '(x w32 ns))
51e39dfc
KS
1457 (or (cddr (assoc (or display (frame-parameter nil 'display))
1458 display-mm-dimensions-alist))
1459 (cddr (assoc t display-mm-dimensions-alist))
1460 (x-display-mm-height display))))
9911648b 1461
aa360da1
GM
1462(declare-function x-display-mm-width "xfns.c" (&optional terminal))
1463
9911648b
EZ
1464(defun display-mm-width (&optional display)
1465 "Return the width of DISPLAY's screen in millimeters.
d7cd2d8b 1466System values can be overridden by `display-mm-dimensions-alist'.
cf13177e
YM
1467If the information is unavailable, value is nil.
1468For graphical terminals, note that on \"multi-monitor\" setups this
1469refers to the width in millimeters for all physical monitors
1470associated with DISPLAY. To get information for each physical
d40a46d7
XF
1471monitor, use `display-monitor-attributes-list'.
1472If DISPLAY is omitted or nil, it defaults to the selected frame's display."
9e2a2647 1473 (and (memq (framep-on-display display) '(x w32 ns))
51e39dfc
KS
1474 (or (cadr (assoc (or display (frame-parameter nil 'display))
1475 display-mm-dimensions-alist))
1476 (cadr (assoc t display-mm-dimensions-alist))
1477 (x-display-mm-width display))))
9911648b 1478
aa360da1
GM
1479(declare-function x-display-backing-store "xfns.c" (&optional terminal))
1480
916119ec
XF
1481;; In NS port, the return value may be `buffered', `retained', or
1482;; `non-retained'. See src/nsfns.m.
9911648b
EZ
1483(defun display-backing-store (&optional display)
1484 "Return the backing store capability of DISPLAY's screen.
1485The value may be `always', `when-mapped', `not-useful', or nil if
d40a46d7
XF
1486the question is inapplicable to a certain kind of display.
1487If DISPLAY is omitted or nil, it defaults to the selected frame's display."
9911648b
EZ
1488 (let ((frame-type (framep-on-display display)))
1489 (cond
9e2a2647 1490 ((memq frame-type '(x w32 ns))
9911648b
EZ
1491 (x-display-backing-store display))
1492 (t
1493 'not-useful))))
1494
aa360da1
GM
1495(declare-function x-display-save-under "xfns.c" (&optional terminal))
1496
9911648b 1497(defun display-save-under (&optional display)
d40a46d7
XF
1498 "Return non-nil if DISPLAY's screen supports the SaveUnder feature.
1499If DISPLAY is omitted or nil, it defaults to the selected frame's display."
9911648b
EZ
1500 (let ((frame-type (framep-on-display display)))
1501 (cond
9e2a2647 1502 ((memq frame-type '(x w32 ns))
9911648b
EZ
1503 (x-display-save-under display))
1504 (t
1505 'not-useful))))
1506
aa360da1
GM
1507(declare-function x-display-planes "xfns.c" (&optional terminal))
1508
9911648b 1509(defun display-planes (&optional display)
d40a46d7
XF
1510 "Return the number of planes supported by DISPLAY.
1511If DISPLAY is omitted or nil, it defaults to the selected frame's display."
9911648b
EZ
1512 (let ((frame-type (framep-on-display display)))
1513 (cond
9e2a2647 1514 ((memq frame-type '(x w32 ns))
9911648b
EZ
1515 (x-display-planes display))
1516 ((eq frame-type 'pc)
1517 4)
1518 (t
1519 (truncate (log (length (tty-color-alist)) 2))))))
1520
aa360da1
GM
1521(declare-function x-display-color-cells "xfns.c" (&optional terminal))
1522
9911648b 1523(defun display-color-cells (&optional display)
d40a46d7
XF
1524 "Return the number of color cells supported by DISPLAY.
1525If DISPLAY is omitted or nil, it defaults to the selected frame's display."
9911648b
EZ
1526 (let ((frame-type (framep-on-display display)))
1527 (cond
9e2a2647 1528 ((memq frame-type '(x w32 ns))
9911648b
EZ
1529 (x-display-color-cells display))
1530 ((eq frame-type 'pc)
1531 16)
1532 (t
3224dac1 1533 (tty-display-color-cells display)))))
9911648b 1534
aa360da1
GM
1535(declare-function x-display-visual-class "xfns.c" (&optional terminal))
1536
9911648b 1537(defun display-visual-class (&optional display)
6cda144f 1538 "Return the visual class of DISPLAY.
9911648b 1539The value is one of the symbols `static-gray', `gray-scale',
d40a46d7
XF
1540`static-color', `pseudo-color', `true-color', or `direct-color'.
1541If DISPLAY is omitted or nil, it defaults to the selected frame's display."
9911648b
EZ
1542 (let ((frame-type (framep-on-display display)))
1543 (cond
9e2a2647 1544 ((memq frame-type '(x w32 ns))
9911648b
EZ
1545 (x-display-visual-class display))
1546 ((and (memq frame-type '(pc t))
1547 (tty-display-color-p display))
1548 'static-color)
1549 (t
1550 'static-gray))))
1551
4e3f9230
YM
1552(declare-function x-display-monitor-attributes-list "xfns.c"
1553 (&optional terminal))
cf13177e
YM
1554(declare-function w32-display-monitor-attributes-list "w32fns.c"
1555 (&optional display))
f20def1f 1556(declare-function ns-display-monitor-attributes-list "nsfns.m"
4465bfb4 1557 (&optional terminal))
4e3f9230
YM
1558
1559(defun display-monitor-attributes-list (&optional display)
1560 "Return a list of physical monitor attributes on DISPLAY.
1561Each element of the list represents the attributes of each
1562physical monitor. The first element corresponds to the primary
1563monitor.
1564
1565Attributes for a physical monitor is represented as an alist of
1566attribute keys and values as follows:
1567
1568 geometry -- Position and size in pixels in the form of
1569 (X Y WIDTH HEIGHT)
1570 workarea -- Position and size of the workarea in pixels in the
1571 form of (X Y WIDTH HEIGHT)
1572 mm-size -- Width and height in millimeters in the form of
1573 (WIDTH HEIGHT)
1574 frames -- List of frames dominated by the physical monitor
1575 name (*) -- Name of the physical monitor as a string
1576
1577where X, Y, WIDTH, and HEIGHT are integers. Keys labeled
1578with (*) are optional.
1579
1580A frame is dominated by a physical monitor when either the
1581largest area of the frame resides in the monitor, or the monitor
1582is the closest to the frame if the frame does not intersect any
1583physical monitors. Every non-tip frame (including invisible one)
1584in a graphical display is dominated by exactly one physical
1585monitor at a time, though it can span multiple (or no) physical
d40a46d7
XF
1586monitors.
1587If DISPLAY is omitted or nil, it defaults to the selected frame's display."
4e3f9230
YM
1588 (let ((frame-type (framep-on-display display)))
1589 (cond
1590 ((eq frame-type 'x)
1591 (x-display-monitor-attributes-list display))
cf13177e
YM
1592 ((eq frame-type 'w32)
1593 (w32-display-monitor-attributes-list display))
4465bfb4
JD
1594 ((eq frame-type 'ns)
1595 (ns-display-monitor-attributes-list display))
4e3f9230
YM
1596 (t
1597 (let ((geometry (list 0 0 (display-pixel-width display)
1598 (display-pixel-height display))))
1599 `(((geometry . ,geometry)
1600 (workarea . ,geometry)
1601 (mm-size . (,(display-mm-width display)
1602 ,(display-mm-height display)))
1603 (frames . ,(frames-on-display-list display)))))))))
1604
9911648b 1605\f
a32c1804
RS
1606;;;; Frame geometry values
1607
1608(defun frame-geom-value-cons (type value &optional frame)
1609 "Return equivalent geometry value for FRAME as a cons with car `+'.
1610A geometry value equivalent to VALUE for FRAME is returned,
1611where the value is a cons with car `+', not numeric.
1612TYPE is the car of the original geometry spec (TYPE . VALUE).
1613 It is `top' or `left', depending on which edge VALUE is related to.
1614VALUE is the cdr of a frame geometry spec: (left/top . VALUE).
1615If VALUE is a number, then it is converted to a cons value, perhaps
1616 relative to the opposite frame edge from that in the original spec.
1617FRAME defaults to the selected frame.
1618
1619Examples (measures in pixels) -
1620 Assuming display height/width=1024, frame height/width=600:
1621 300 inside display edge: 300 => (+ 300)
1622 (+ 300) => (+ 300)
1623 300 inside opposite display edge: (- 300) => (+ 124)
1624 -300 => (+ 124)
1625 300 beyond display edge
1626 (= 724 inside opposite display edge): (+ -300) => (+ -300)
1627 300 beyond display edge
1628 (= 724 inside opposite display edge): (- -300) => (+ 724)
1629
1630In the 3rd, 4th, and 6th examples, the returned value is relative to
1631the opposite frame edge from the edge indicated in the input spec."
1632 (cond ((and (consp value) (eq '+ (car value))) ; e.g. (+ 300), (+ -300)
1633 value)
1634 ((natnump value) (list '+ value)) ; e.g. 300 => (+ 300)
1635 (t ; e.g. -300, (- 300), (- -300)
1636 (list '+ (- (if (eq 'left type) ; => (+ 124), (+ 124), (+ 724)
1637 (x-display-pixel-width)
1638 (x-display-pixel-height))
1639 (if (integerp value) (- value) (cadr value))
1640 (if (eq 'left type)
1641 (frame-pixel-width frame)
1642 (frame-pixel-height frame)))))))
1643
1644(defun frame-geom-spec-cons (spec &optional frame)
1645 "Return equivalent geometry spec for FRAME as a cons with car `+'.
1646A geometry specification equivalent to SPEC for FRAME is returned,
1647where the value is a cons with car `+', not numeric.
1648SPEC is a frame geometry spec: (left . VALUE) or (top . VALUE).
1649If VALUE is a number, then it is converted to a cons value, perhaps
1650 relative to the opposite frame edge from that in the original spec.
1651FRAME defaults to the selected frame.
1652
1653Examples (measures in pixels) -
1654 Assuming display height=1024, frame height=600:
1655 top 300 below display top: (top . 300) => (top + 300)
1656 (top + 300) => (top + 300)
1657 bottom 300 above display bottom: (top - 300) => (top + 124)
1658 (top . -300) => (top + 124)
1659 top 300 above display top
1660 (= bottom 724 above display bottom): (top + -300) => (top + -300)
1661 bottom 300 below display bottom
1662 (= top 724 below display top): (top - -300) => (top + 724)
1663
1664In the 3rd, 4th, and 6th examples, the returned value is relative to
1665the opposite frame edge from the edge indicated in the input spec."
06b60517 1666 (cons (car spec) (frame-geom-value-cons (car spec) (cdr spec) frame)))
a32c1804 1667\f
9e2b097b 1668
154a757e 1669(defun delete-other-frames (&optional frame)
4a592f66 1670 "Delete all frames on the current terminal, except FRAME.
a2125918
GM
1671If FRAME uses another frame's minibuffer, the minibuffer frame is
1672left untouched. FRAME nil or omitted means use the selected frame."
154a757e
GM
1673 (interactive)
1674 (unless frame
1675 (setq frame (selected-frame)))
a2125918
GM
1676 (let* ((mini-frame (window-frame (minibuffer-window frame)))
1677 (frames (delq mini-frame (delq frame (frame-list)))))
5e5ae184
SM
1678 ;; Only consider frames on the same terminal.
1679 (dolist (frame (prog1 frames (setq frames nil)))
1680 (if (eq (frame-terminal) (frame-terminal frame))
1681 (push frame frames)))
a2125918
GM
1682 ;; Delete mon-minibuffer-only frames first, because `delete-frame'
1683 ;; signals an error when trying to delete a mini-frame that's
1684 ;; still in use by another frame.
1685 (dolist (frame frames)
1686 (unless (eq (frame-parameter frame 'minibuffer) 'only)
1687 (delete-frame frame)))
1688 ;; Delete minibuffer-only frames.
1689 (dolist (frame frames)
1690 (when (eq (frame-parameter frame 'minibuffer) 'only)
1691 (delete-frame frame)))))
154a757e 1692
da7829a3 1693;; miscellaneous obsolescence declarations
84ed1560
JB
1694(define-obsolete-variable-alias 'delete-frame-hook
1695 'delete-frame-functions "22.1")
da7829a3 1696
dc6d9681 1697\f
7e573c4a 1698;; Blinking cursor
81b99826
GM
1699
1700(defgroup cursor nil
ca693be8 1701 "Displaying text cursors."
40d34803 1702 :version "21.1"
81b99826
GM
1703 :group 'frames)
1704
1705(defcustom blink-cursor-delay 0.5
6cda144f 1706 "Seconds of idle time after which cursor starts to blink."
81b99826
GM
1707 :type 'number
1708 :group 'cursor)
1709
1710(defcustom blink-cursor-interval 0.5
6cda144f 1711 "Length of cursor blink interval in seconds."
81b99826
GM
1712 :type 'number
1713 :group 'cursor)
1714
18c26d81
JD
1715(defcustom blink-cursor-blinks 10
1716 "How many times to blink before using a solid cursor on NS and X.
1717Use 0 or negative value to blink forever."
1718 :version "24.4"
1719 :type 'integer
1720 :group 'cursor)
1721
1722(defvar blink-cursor-blinks-done 1
1723 "Number of blinks done since we started blinking on NS and X")
1724
81b99826 1725(defvar blink-cursor-idle-timer nil
ca693be8
GM
1726 "Timer started after `blink-cursor-delay' seconds of Emacs idle time.
1727The function `blink-cursor-start' is called when the timer fires.")
81b99826
GM
1728
1729(defvar blink-cursor-timer nil
ca693be8 1730 "Timer started from `blink-cursor-start'.
a795e09a
LT
1731This timer calls `blink-cursor-timer-function' every
1732`blink-cursor-interval' seconds.")
1733
81b99826 1734(defun blink-cursor-start ()
ca693be8
GM
1735 "Timer function called from the timer `blink-cursor-idle-timer'.
1736This starts the timer `blink-cursor-timer', which makes the cursor blink
1737if appropriate. It also arranges to cancel that timer when the next
1738command starts, by installing a pre-command hook."
81b99826 1739 (when (null blink-cursor-timer)
f9ac92c5
CY
1740 ;; Set up the timer first, so that if this signals an error,
1741 ;; blink-cursor-end is not added to pre-command-hook.
18c26d81 1742 (setq blink-cursor-blinks-done 1)
81b99826
GM
1743 (setq blink-cursor-timer
1744 (run-with-timer blink-cursor-interval blink-cursor-interval
f9ac92c5
CY
1745 'blink-cursor-timer-function))
1746 (add-hook 'pre-command-hook 'blink-cursor-end)
1747 (internal-show-cursor nil nil)))
0a5ebe4b
GM
1748
1749(defun blink-cursor-timer-function ()
1750 "Timer function of timer `blink-cursor-timer'."
18c26d81
JD
1751 (internal-show-cursor nil (not (internal-show-cursor-p)))
1752 ;; Each blink is two calls to this function.
511fa0d3
SM
1753 (setq blink-cursor-blinks-done (1+ blink-cursor-blinks-done))
1754 (when (and (> blink-cursor-blinks 0)
1755 (<= (* 2 blink-cursor-blinks) blink-cursor-blinks-done))
1756 (blink-cursor-suspend)
1757 (add-hook 'post-command-hook 'blink-cursor-check)))
18c26d81 1758
81b99826
GM
1759
1760(defun blink-cursor-end ()
1761 "Stop cursor blinking.
ca693be8 1762This is installed as a pre-command hook by `blink-cursor-start'.
f39784dd 1763When run, it cancels the timer `blink-cursor-timer' and removes
ca693be8 1764itself as a pre-command hook."
81b99826 1765 (remove-hook 'pre-command-hook 'blink-cursor-end)
0a5ebe4b 1766 (internal-show-cursor nil t)
8ec94c16
SM
1767 (when blink-cursor-timer
1768 (cancel-timer blink-cursor-timer)
1769 (setq blink-cursor-timer nil)))
81b99826 1770
18c26d81 1771(defun blink-cursor-suspend ()
511fa0d3 1772 "Suspend cursor blinking.
18c26d81
JD
1773This is called when no frame has focus and timers can be suspended.
1774Timers are restarted by `blink-cursor-check', which is called when a
1775frame receives focus."
511fa0d3
SM
1776 (blink-cursor-end)
1777 (when blink-cursor-idle-timer
1778 (cancel-timer blink-cursor-idle-timer)
1779 (setq blink-cursor-idle-timer nil)))
18c26d81
JD
1780
1781(defun blink-cursor-check ()
02c66599 1782 "Check if cursor blinking shall be restarted.
18c26d81
JD
1783This is done when a frame gets focus. Blink timers may be stopped by
1784`blink-cursor-suspend'."
1785 (when (and blink-cursor-mode
1786 (not blink-cursor-idle-timer))
1787 (remove-hook 'post-command-hook 'blink-cursor-check)
1788 (setq blink-cursor-idle-timer
1789 (run-with-idle-timer blink-cursor-delay
1790 blink-cursor-delay
1791 'blink-cursor-start))))
1792
e5bd0a28
SM
1793(define-obsolete-variable-alias 'blink-cursor 'blink-cursor-mode "22.1")
1794
8ec94c16 1795(define-minor-mode blink-cursor-mode
06e21633
CY
1796 "Toggle cursor blinking (Blink Cursor mode).
1797With a prefix argument ARG, enable Blink Cursor mode if ARG is
1798positive, and disable it otherwise. If called from Lisp, enable
1799the mode if ARG is omitted or nil.
1800
1801This command is effective only on graphical frames. On text-only
1802terminals, cursor blinking is controlled by the terminal."
8ec94c16
SM
1803 :init-value (not (or noninteractive
1804 no-blinking-cursor
1805 (eq system-type 'ms-dos)
0afb6242 1806 (not (memq window-system '(x w32 ns)))))
adba8116 1807 :initialize 'custom-initialize-delay
8ec94c16
SM
1808 :group 'cursor
1809 :global t
1810 (if blink-cursor-idle-timer (cancel-timer blink-cursor-idle-timer))
1811 (setq blink-cursor-idle-timer nil)
1812 (blink-cursor-end)
511fa0d3
SM
1813 (remove-hook 'focus-in-hook #'blink-cursor-check)
1814 (remove-hook 'focus-out-hook #'blink-cursor-suspend)
8ec94c16 1815 (when blink-cursor-mode
511fa0d3
SM
1816 (add-hook 'focus-in-hook #'blink-cursor-check)
1817 (add-hook 'focus-out-hook #'blink-cursor-suspend)
8ec94c16
SM
1818 (setq blink-cursor-idle-timer
1819 (run-with-idle-timer blink-cursor-delay
1820 blink-cursor-delay
511fa0d3 1821 #'blink-cursor-start))))
81b99826 1822
81b99826 1823\f
a45b7647 1824;; Frame maximization/fullscreen
37f38bca
SS
1825
1826(defun toggle-frame-maximized ()
a45b7647
JL
1827 "Toggle maximization state of the selected frame.
1828Maximize the selected frame or un-maximize if it is already maximized.
1829Respect window manager screen decorations.
1830If the frame is in fullscreen mode, don't change its mode,
1831just toggle the temporary frame parameter `maximized',
1832so the frame will go to the right maximization state
1833after disabling fullscreen mode.
1834See also `toggle-frame-fullscreen'."
37f38bca 1835 (interactive)
64ced394 1836 (if (memq (frame-parameter nil 'fullscreen) '(fullscreen fullboth))
a45b7647
JL
1837 (modify-frame-parameters
1838 nil
1839 `((maximized
1840 . ,(unless (eq (frame-parameter nil 'maximized) 'maximized)
1841 'maximized))))
1842 (modify-frame-parameters
1843 nil
1844 `((fullscreen
1845 . ,(unless (eq (frame-parameter nil 'fullscreen) 'maximized)
1846 'maximized))))))
1847
1848(defun toggle-frame-fullscreen ()
1849 "Toggle fullscreen mode of the selected frame.
1850Enable fullscreen mode of the selected frame or disable if it is
1851already fullscreen. Ignore window manager screen decorations.
1852When turning on fullscreen mode, remember the previous value of the
1853maximization state in the temporary frame parameter `maximized'.
1854Restore the maximization state when turning off fullscreen mode.
37f38bca
SS
1855See also `toggle-frame-maximized'."
1856 (interactive)
1857 (modify-frame-parameters
a45b7647
JL
1858 nil
1859 `((maximized
64ced394 1860 . ,(unless (memq (frame-parameter nil 'fullscreen) '(fullscreen fullboth))
a45b7647
JL
1861 (frame-parameter nil 'fullscreen)))
1862 (fullscreen
64ced394 1863 . ,(if (memq (frame-parameter nil 'fullscreen) '(fullscreen fullboth))
a45b7647
JL
1864 (if (eq (frame-parameter nil 'maximized) 'maximized)
1865 'maximized)
1866 'fullscreen)))))
37f38bca
SS
1867
1868\f
64c669bc 1869;;;; Key bindings
64c669bc 1870
6238bfaf 1871(define-key ctl-x-5-map "2" 'make-frame-command)
154a757e 1872(define-key ctl-x-5-map "1" 'delete-other-frames)
dc6d9681 1873(define-key ctl-x-5-map "0" 'delete-frame)
ceab6935 1874(define-key ctl-x-5-map "o" 'other-frame)
a45b7647
JL
1875(define-key global-map [f11] 'toggle-frame-fullscreen)
1876(define-key global-map [(meta f10)] 'toggle-frame-maximized)
c84b0881 1877(define-key esc-map [f10] 'toggle-frame-maximized)
49116ac0 1878
7a76850c
CY
1879\f
1880;; Misc.
1881
2a1e2476 1882;; Only marked as obsolete in 24.3.
e5bd0a28
SM
1883(define-obsolete-variable-alias 'automatic-hscrolling
1884 'auto-hscroll-mode "22.1")
7a76850c
CY
1885
1886(make-variable-buffer-local 'show-trailing-whitespace)
1887
3424a4f6
CY
1888;; Defined in dispnew.c.
1889(make-obsolete-variable
1890 'window-system-version "it does not give useful information." "24.3")
1891
dc6d9681 1892(provide 'frame)
c88ab9ce 1893
dc6d9681 1894;;; frame.el ends here