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