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