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