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