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