]> code.delx.au - gnu-emacs/blob - lisp/frame.el
* lisp/descr-text.el (describe-char-unicode-data): Fix copy/paste errors.
[gnu-emacs] / lisp / frame.el
1 ;;; frame.el --- multi-frame management independent of window systems -*- lexical-binding:t -*-
2
3 ;; Copyright (C) 1993-1994, 1996-1997, 2000-2016 Free Software
4 ;; Foundation, Inc.
5
6 ;; Maintainer: emacs-devel@gnu.org
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 (cl-defgeneric frame-creation-function (params)
31 "Method for window-system dependent functions to create a new frame.
32 The window system startup file should add its frame creation
33 function to this method, which should take an alist of parameters
34 as its argument.")
35
36 (cl-generic-define-context-rewriter window-system (value)
37 ;; If `value' is a `consp', it's probably an old-style specializer,
38 ;; so just use it, and anyway `eql' isn't very useful on cons cells.
39 `(window-system ,(if (consp value) value `(eql ,value))))
40
41 (cl-defmethod frame-creation-function (params &context (window-system nil))
42 ;; It's tempting to get rid of tty-create-frame-with-faces and turn it into
43 ;; this method (i.e. move this method to faces.el), but faces.el is loaded
44 ;; much earlier from loadup.el (before cl-generic and even before
45 ;; cl-preloaded), so we'd first have to reorder that part.
46 (tty-create-frame-with-faces params))
47
48 (defvar window-system-default-frame-alist nil
49 "Window-system dependent default frame parameters.
50 The value should be an alist of elements (WINDOW-SYSTEM . ALIST),
51 where WINDOW-SYSTEM is a window system symbol (as returned by `framep')
52 and ALIST is a frame parameter alist like `default-frame-alist'.
53 Then, for frames on WINDOW-SYSTEM, any parameters specified in
54 ALIST supersede the corresponding parameters specified in
55 `default-frame-alist'.")
56
57 (defvar display-format-alist nil
58 "Alist of patterns to decode display names.
59 The car of each entry is a regular expression matching a display
60 name string. The cdr is a symbol giving the window-system that
61 handles the corresponding kind of display.")
62
63 ;; The initial value given here used to ask for a minibuffer.
64 ;; But that's not necessary, because the default is to have one.
65 ;; By not specifying it here, we let an X resource specify it.
66 (defcustom initial-frame-alist nil
67 "Alist of parameters for the initial X window frame.
68 You can set this in your init file; for example,
69
70 (setq initial-frame-alist
71 \\='((top . 1) (left . 1) (width . 80) (height . 55)))
72
73 Parameters specified here supersede the values given in
74 `default-frame-alist'.
75
76 If the value calls for a frame without a minibuffer, and you have
77 not created a minibuffer frame on your own, a minibuffer frame is
78 created according to `minibuffer-frame-alist'.
79
80 You can specify geometry-related options for just the initial
81 frame by setting this variable in your init file; however, they
82 won't take effect until Emacs reads your init file, which happens
83 after creating the initial frame. If you want the initial frame
84 to have the proper geometry as soon as it appears, you need to
85 use this three-step process:
86 * Specify X resources to give the geometry you want.
87 * Set `default-frame-alist' to override these options so that they
88 don't affect subsequent frames.
89 * Set `initial-frame-alist' in a way that matches the X resources,
90 to override what you put in `default-frame-alist'."
91 :type '(repeat (cons :format "%v"
92 (symbol :tag "Parameter")
93 (sexp :tag "Value")))
94 :group 'frames)
95
96 (defcustom minibuffer-frame-alist '((width . 80) (height . 2))
97 "Alist of parameters for the initial minibuffer frame.
98 This is the minibuffer frame created if `initial-frame-alist'
99 calls for a frame without a minibuffer. The parameters specified
100 here supersede those given in `default-frame-alist', for the
101 initial minibuffer frame.
102
103 You can set this in your init file; for example,
104
105 (setq minibuffer-frame-alist
106 \\='((top . 1) (left . 1) (width . 80) (height . 2)))
107
108 It is not necessary to include (minibuffer . only); that is
109 appended when the minibuffer frame is created."
110 :type '(repeat (cons :format "%v"
111 (symbol :tag "Parameter")
112 (sexp :tag "Value")))
113 :group 'frames)
114
115 (defun handle-delete-frame (event)
116 "Handle delete-frame events from the X server."
117 (interactive "e")
118 (let ((frame (posn-window (event-start event)))
119 (i 0)
120 (tail (frame-list)))
121 (while tail
122 (and (frame-visible-p (car tail))
123 (not (eq (car tail) frame))
124 (setq i (1+ i)))
125 (setq tail (cdr tail)))
126 (if (> i 0)
127 (delete-frame frame t)
128 ;; Gildea@x.org says it is ok to ask questions before terminating.
129 (save-buffers-kill-emacs))))
130
131 (defun handle-focus-in (_event)
132 "Handle a focus-in event.
133 Focus-in events are usually bound to this function.
134 Focus-in events occur when a frame has focus, but a switch-frame event
135 is not generated.
136 This function runs the hook `focus-in-hook'."
137 (interactive "e")
138 (run-hooks 'focus-in-hook))
139
140 (defun handle-focus-out (_event)
141 "Handle a focus-out event.
142 Focus-out events are usually bound to this function.
143 Focus-out events occur when no frame has focus.
144 This function runs the hook `focus-out-hook'."
145 (interactive "e")
146 (run-hooks 'focus-out-hook))
147 \f
148 ;;;; Arrangement of frames at startup
149
150 ;; 1) Load the window system startup file from the lisp library and read the
151 ;; high-priority arguments (-q and the like). The window system startup
152 ;; file should create any frames specified in the window system defaults.
153 ;;
154 ;; 2) If no frames have been opened, we open an initial text frame.
155 ;;
156 ;; 3) Once the init file is done, we apply any newly set parameters
157 ;; in initial-frame-alist to the frame.
158
159 ;; If we create the initial frame, this is it.
160 (defvar frame-initial-frame nil)
161
162 ;; Record the parameters used in frame-initialize to make the initial frame.
163 (defvar frame-initial-frame-alist)
164
165 (defvar frame-initial-geometry-arguments nil)
166
167 ;; startup.el calls this function before loading the user's init
168 ;; file - if there is no frame with a minibuffer open now, create
169 ;; one to display messages while loading the init file.
170 (defun frame-initialize ()
171 "Create an initial frame if necessary."
172 ;; Are we actually running under a window system at all?
173 (if (and initial-window-system
174 (not noninteractive)
175 (not (eq initial-window-system 'pc)))
176 (progn
177 ;; If there is no frame with a minibuffer besides the terminal
178 ;; frame, then we need to create the opening frame. Make sure
179 ;; it has a minibuffer, but let initial-frame-alist omit the
180 ;; minibuffer spec.
181 (or (delq terminal-frame (minibuffer-frame-list))
182 (progn
183 (setq frame-initial-frame-alist
184 (append initial-frame-alist default-frame-alist nil))
185 (setq frame-initial-frame-alist
186 (cons (cons 'window-system initial-window-system)
187 frame-initial-frame-alist))
188 (setq default-minibuffer-frame
189 (setq frame-initial-frame
190 (make-frame frame-initial-frame-alist)))
191 ;; Delete any specifications for window geometry parameters
192 ;; so that we won't reapply them in frame-notice-user-settings.
193 ;; It would be wrong to reapply them then,
194 ;; because that would override explicit user resizing.
195 (setq initial-frame-alist
196 (frame-remove-geometry-params initial-frame-alist))))
197 ;; Copy the environment of the Emacs process into the new frame.
198 (set-frame-parameter frame-initial-frame 'environment
199 (frame-parameter terminal-frame 'environment))
200 ;; At this point, we know that we have a frame open, so we
201 ;; can delete the terminal frame.
202 (delete-frame terminal-frame)
203 (setq terminal-frame nil))))
204
205 (defvar frame-notice-user-settings t
206 "Non-nil means function `frame-notice-user-settings' wasn't run yet.")
207
208 (declare-function tool-bar-mode "tool-bar" (&optional arg))
209 (declare-function tool-bar-height "xdisp.c" (&optional frame pixelwise))
210
211 (defalias 'tool-bar-lines-needed 'tool-bar-height)
212
213 ;; startup.el calls this function after loading the user's init
214 ;; file. Now default-frame-alist and initial-frame-alist contain
215 ;; information to which we must react; do what needs to be done.
216 (defun frame-notice-user-settings ()
217 "Act on user's init file settings of frame parameters.
218 React to settings of `initial-frame-alist',
219 `window-system-default-frame-alist' and `default-frame-alist'
220 there (in decreasing order of priority)."
221 ;; Creating and deleting frames may shift the selected frame around,
222 ;; and thus the current buffer. Protect against that. We don't
223 ;; want to use save-excursion here, because that may also try to set
224 ;; the buffer of the selected window, which fails when the selected
225 ;; window is the minibuffer.
226 (let ((old-buffer (current-buffer))
227 (window-system-frame-alist
228 (cdr (assq initial-window-system
229 window-system-default-frame-alist))))
230
231 (when (and frame-notice-user-settings
232 (null frame-initial-frame))
233 ;; This case happens when we don't have a window system, and
234 ;; also for MS-DOS frames.
235 (let ((parms (frame-parameters)))
236 ;; Don't change the frame names.
237 (setq parms (delq (assq 'name parms) parms))
238 ;; Can't modify the minibuffer parameter, so don't try.
239 (setq parms (delq (assq 'minibuffer parms) parms))
240 (modify-frame-parameters
241 nil
242 (if initial-window-system
243 parms
244 ;; initial-frame-alist and default-frame-alist were already
245 ;; applied in pc-win.el.
246 (append initial-frame-alist window-system-frame-alist
247 default-frame-alist parms nil)))
248 (if (null initial-window-system) ;; MS-DOS does this differently in pc-win.el
249 (let ((newparms (frame-parameters))
250 (frame (selected-frame)))
251 (tty-handle-reverse-video frame newparms)
252 ;; tty-handle-reverse-video might change the frame's
253 ;; color parameters, and we need to use the updated
254 ;; value below.
255 (setq newparms (frame-parameters))
256 ;; If we changed the background color, we need to update
257 ;; the background-mode parameter, and maybe some faces,
258 ;; too.
259 (when (assq 'background-color newparms)
260 (unless (or (assq 'background-mode initial-frame-alist)
261 (assq 'background-mode default-frame-alist))
262 (frame-set-background-mode frame))
263 (face-set-after-frame-default frame newparms))))))
264
265 ;; If the initial frame is still around, apply initial-frame-alist
266 ;; and default-frame-alist to it.
267 (when (frame-live-p frame-initial-frame)
268 ;; When tool-bar has been switched off, correct the frame size
269 ;; by the lines added in x-create-frame for the tool-bar and
270 ;; switch `tool-bar-mode' off.
271 (when (display-graphic-p)
272 (let* ((init-lines
273 (assq 'tool-bar-lines initial-frame-alist))
274 (other-lines
275 (or (assq 'tool-bar-lines window-system-frame-alist)
276 (assq 'tool-bar-lines default-frame-alist)))
277 (lines (or init-lines other-lines))
278 (height (tool-bar-height frame-initial-frame t)))
279 ;; Adjust frame top if either zero (nil) tool bar lines have
280 ;; been requested in the most relevant of the frame's alists
281 ;; or tool bar mode has been explicitly turned off in the
282 ;; user's init file.
283 (when (and (> height 0)
284 (or (and lines
285 (or (null (cdr lines))
286 (eq 0 (cdr lines))))
287 (not tool-bar-mode)))
288 (let* ((initial-top
289 (cdr (assq 'top frame-initial-geometry-arguments)))
290 (top (frame-parameter frame-initial-frame 'top)))
291 (when (and (consp initial-top) (eq '- (car initial-top)))
292 (let ((adjusted-top
293 (cond
294 ((and (consp top) (eq '+ (car top)))
295 (list '+ (+ (cadr top) height)))
296 ((and (consp top) (eq '- (car top)))
297 (list '- (- (cadr top) height)))
298 (t (+ top height)))))
299 (modify-frame-parameters
300 frame-initial-frame `((top . ,adjusted-top))))))
301 ;; Reset `tool-bar-mode' when zero tool bar lines have been
302 ;; requested for the window-system or default frame alists.
303 (when (and tool-bar-mode
304 (and other-lines
305 (or (null (cdr other-lines))
306 (eq 0 (cdr other-lines)))))
307 (tool-bar-mode -1)))))
308
309 ;; The initial frame we create above always has a minibuffer.
310 ;; If the user wants to remove it, or make it a minibuffer-only
311 ;; frame, then we'll have to delete the current frame and make a
312 ;; new one; you can't remove or add a root window to/from an
313 ;; existing frame.
314 ;;
315 ;; NOTE: default-frame-alist was nil when we created the
316 ;; existing frame. We need to explicitly include
317 ;; default-frame-alist in the parameters of the screen we
318 ;; create here, so that its new value, gleaned from the user's
319 ;; init file, will be applied to the existing screen.
320 (if (not (eq (cdr (or (assq 'minibuffer initial-frame-alist)
321 (assq 'minibuffer window-system-frame-alist)
322 (assq 'minibuffer default-frame-alist)
323 '(minibuffer . t)))
324 t))
325 ;; Create the new frame.
326 (let (parms new)
327 ;; MS-Windows needs this to avoid inflooping below.
328 (if (eq system-type 'windows-nt)
329 (sit-for 0 t))
330 ;; If the frame isn't visible yet, wait till it is.
331 ;; If the user has to position the window,
332 ;; Emacs doesn't know its real position until
333 ;; the frame is seen to be visible.
334 (while (not (cdr (assq 'visibility
335 (frame-parameters frame-initial-frame))))
336 (sleep-for 1))
337 (setq parms (frame-parameters frame-initial-frame))
338
339 ;; Get rid of `name' unless it was specified explicitly before.
340 (or (assq 'name frame-initial-frame-alist)
341 (setq parms (delq (assq 'name parms) parms)))
342 ;; An explicit parent-id is a request to XEmbed the frame.
343 (or (assq 'parent-id frame-initial-frame-alist)
344 (setq parms (delq (assq 'parent-id parms) parms)))
345
346 (setq parms (append initial-frame-alist
347 window-system-frame-alist
348 default-frame-alist
349 parms
350 nil))
351
352 ;; Get rid of `reverse', because that was handled
353 ;; when we first made the frame.
354 (setq parms (cons '(reverse) (delq (assq 'reverse parms) parms)))
355
356 (if (assq 'height frame-initial-geometry-arguments)
357 (setq parms (assq-delete-all 'height parms)))
358 (if (assq 'width frame-initial-geometry-arguments)
359 (setq parms (assq-delete-all 'width parms)))
360 (if (assq 'left frame-initial-geometry-arguments)
361 (setq parms (assq-delete-all 'left parms)))
362 (if (assq 'top frame-initial-geometry-arguments)
363 (setq parms (assq-delete-all 'top parms)))
364 (setq new
365 (make-frame
366 ;; Use the geometry args that created the existing
367 ;; frame, rather than the parms we get for it.
368 (append frame-initial-geometry-arguments
369 '((user-size . t) (user-position . t))
370 parms)))
371 ;; The initial frame, which we are about to delete, may be
372 ;; the only frame with a minibuffer. If it is, create a
373 ;; new one.
374 (or (delq frame-initial-frame (minibuffer-frame-list))
375 (make-initial-minibuffer-frame nil))
376
377 ;; If the initial frame is serving as a surrogate
378 ;; minibuffer frame for any frames, we need to wean them
379 ;; onto a new frame. The default-minibuffer-frame
380 ;; variable must be handled similarly.
381 (let ((users-of-initial
382 (filtered-frame-list
383 (lambda (frame)
384 (and (not (eq frame frame-initial-frame))
385 (eq (window-frame
386 (minibuffer-window frame))
387 frame-initial-frame))))))
388 (if (or users-of-initial
389 (eq default-minibuffer-frame frame-initial-frame))
390
391 ;; Choose an appropriate frame. Prefer frames which
392 ;; are only minibuffers.
393 (let* ((new-surrogate
394 (car
395 (or (filtered-frame-list
396 (lambda (frame)
397 (eq (cdr (assq 'minibuffer
398 (frame-parameters frame)))
399 'only)))
400 (minibuffer-frame-list))))
401 (new-minibuffer (minibuffer-window new-surrogate)))
402
403 (if (eq default-minibuffer-frame frame-initial-frame)
404 (setq default-minibuffer-frame new-surrogate))
405
406 ;; Wean the frames using frame-initial-frame as
407 ;; their minibuffer frame.
408 (dolist (frame users-of-initial)
409 (modify-frame-parameters
410 frame (list (cons 'minibuffer new-minibuffer)))))))
411
412 ;; Redirect events enqueued at this frame to the new frame.
413 ;; Is this a good idea?
414 (redirect-frame-focus frame-initial-frame new)
415
416 ;; Finally, get rid of the old frame.
417 (delete-frame frame-initial-frame t))
418
419 ;; Otherwise, we don't need all that rigmarole; just apply
420 ;; the new parameters.
421 (let (newparms allparms tail)
422 (setq allparms (append initial-frame-alist
423 window-system-frame-alist
424 default-frame-alist nil))
425 (if (assq 'height frame-initial-geometry-arguments)
426 (setq allparms (assq-delete-all 'height allparms)))
427 (if (assq 'width frame-initial-geometry-arguments)
428 (setq allparms (assq-delete-all 'width allparms)))
429 (if (assq 'left frame-initial-geometry-arguments)
430 (setq allparms (assq-delete-all 'left allparms)))
431 (if (assq 'top frame-initial-geometry-arguments)
432 (setq allparms (assq-delete-all 'top allparms)))
433 (setq tail allparms)
434 ;; Find just the parms that have changed since we first
435 ;; made this frame. Those are the ones actually set by
436 ;; the init file. For those parms whose values we already knew
437 ;; (such as those spec'd by command line options)
438 ;; it is undesirable to specify the parm again
439 ;; once the user has seen the frame and been able to alter it
440 ;; manually.
441 (let (newval oldval)
442 (dolist (entry tail)
443 (setq oldval (assq (car entry) frame-initial-frame-alist))
444 (setq newval (cdr (assq (car entry) allparms)))
445 (or (and oldval (eq (cdr oldval) newval))
446 (setq newparms
447 (cons (cons (car entry) newval) newparms)))))
448 (setq newparms (nreverse newparms))
449
450 (let ((new-bg (assq 'background-color newparms)))
451 ;; If the `background-color' parameter is changed, apply
452 ;; it first, then make sure that the `background-mode'
453 ;; parameter and other faces are updated, before applying
454 ;; the other parameters.
455 (when new-bg
456 (modify-frame-parameters frame-initial-frame
457 (list new-bg))
458 (unless (assq 'background-mode newparms)
459 (frame-set-background-mode frame-initial-frame))
460 (face-set-after-frame-default frame-initial-frame)
461 (setq newparms (delq new-bg newparms)))
462
463 (when (numberp (car frame-size-history))
464 (setq frame-size-history
465 (cons (1- (car frame-size-history))
466 (cons
467 (list frame-initial-frame
468 "FRAME-NOTICE-USER"
469 nil newparms)
470 (cdr frame-size-history)))))
471
472 (modify-frame-parameters frame-initial-frame newparms)))))
473
474 ;; Restore the original buffer.
475 (set-buffer old-buffer)
476
477 ;; Make sure the initial frame can be GC'd if it is ever deleted.
478 ;; Make sure frame-notice-user-settings does nothing if called twice.
479 (setq frame-notice-user-settings nil)
480 (setq frame-initial-frame nil)))
481
482 (defun make-initial-minibuffer-frame (display)
483 (let ((parms (append minibuffer-frame-alist '((minibuffer . only)))))
484 (if display
485 (make-frame-on-display display parms)
486 (make-frame parms))))
487
488 ;;;; Creation of additional frames, and other frame miscellanea
489
490 (defun modify-all-frames-parameters (alist)
491 "Modify all current and future frames' parameters according to ALIST.
492 This changes `default-frame-alist' and possibly `initial-frame-alist'.
493 Furthermore, this function removes all parameters in ALIST from
494 `window-system-default-frame-alist'.
495 See help of `modify-frame-parameters' for more information."
496 (dolist (frame (frame-list))
497 (modify-frame-parameters frame alist))
498
499 (dolist (pair alist) ;; conses to add/replace
500 ;; initial-frame-alist needs setting only when
501 ;; frame-notice-user-settings is true.
502 (and frame-notice-user-settings
503 (setq initial-frame-alist
504 (assq-delete-all (car pair) initial-frame-alist)))
505 (setq default-frame-alist
506 (assq-delete-all (car pair) default-frame-alist))
507 ;; Remove any similar settings from the window-system specific
508 ;; parameters---they would override default-frame-alist.
509 (dolist (w window-system-default-frame-alist)
510 (setcdr w (assq-delete-all (car pair) (cdr w)))))
511
512 (and frame-notice-user-settings
513 (setq initial-frame-alist (append initial-frame-alist alist)))
514 (setq default-frame-alist (append default-frame-alist alist)))
515
516 (defun get-other-frame ()
517 "Return some frame other than the current frame.
518 Create one if necessary. Note that the minibuffer frame, if separate,
519 is not considered (see `next-frame')."
520 (if (equal (next-frame) (selected-frame)) (make-frame) (next-frame)))
521
522 (defun next-multiframe-window ()
523 "Select the next window, regardless of which frame it is on."
524 (interactive)
525 (select-window (next-window (selected-window)
526 (> (minibuffer-depth) 0)
527 0))
528 (select-frame-set-input-focus (selected-frame)))
529
530 (defun previous-multiframe-window ()
531 "Select the previous window, regardless of which frame it is on."
532 (interactive)
533 (select-window (previous-window (selected-window)
534 (> (minibuffer-depth) 0)
535 0))
536 (select-frame-set-input-focus (selected-frame)))
537
538 (defun window-system-for-display (display)
539 "Return the window system for DISPLAY.
540 Return nil if we don't know how to interpret DISPLAY."
541 ;; MS-Windows doesn't know how to create a GUI frame in a -nw session.
542 (if (and (eq system-type 'windows-nt)
543 (null (window-system))
544 (not (daemonp)))
545 nil
546 (cl-loop for descriptor in display-format-alist
547 for pattern = (car descriptor)
548 for system = (cdr descriptor)
549 when (string-match-p pattern display) return system)))
550
551 (defun make-frame-on-display (display &optional parameters)
552 "Make a frame on display DISPLAY.
553 The optional argument PARAMETERS specifies additional frame parameters."
554 (interactive "sMake frame on display: ")
555 (make-frame (cons (cons 'display display) parameters)))
556
557 (declare-function x-close-connection "xfns.c" (terminal))
558
559 (defun close-display-connection (display)
560 "Close the connection to a display, deleting all its associated frames.
561 For DISPLAY, specify either a frame or a display name (a string).
562 If DISPLAY is nil, that stands for the selected frame's display."
563 (interactive
564 (list
565 (let* ((default (frame-parameter nil 'display))
566 (display (completing-read
567 (format "Close display (default %s): " default)
568 (delete-dups
569 (mapcar (lambda (frame)
570 (frame-parameter frame 'display))
571 (frame-list)))
572 nil t nil nil
573 default)))
574 (if (zerop (length display)) default display))))
575 (let ((frames (delq nil
576 (mapcar (lambda (frame)
577 (if (equal display
578 (frame-parameter frame 'display))
579 frame))
580 (frame-list)))))
581 (if (and (consp frames)
582 (not (y-or-n-p (if (cdr frames)
583 (format "Delete %s frames? " (length frames))
584 (format "Delete %s ? " (car frames))))))
585 (error "Abort!")
586 (mapc 'delete-frame frames)
587 (x-close-connection display))))
588
589 (defun make-frame-command ()
590 "Make a new frame, on the same terminal as the selected frame.
591 If the terminal is a text-only terminal, this also selects the
592 new frame."
593 (interactive)
594 (if (display-graphic-p)
595 (make-frame)
596 (select-frame (make-frame))))
597
598 (defvar before-make-frame-hook nil
599 "Functions to run before a frame is created.")
600
601 (defvar after-make-frame-functions nil
602 "Functions to run after a frame is created.
603 The functions are run with one arg, the newly created frame.")
604
605 (defvar after-setting-font-hook nil
606 "Functions to run after a frame's font has been changed.")
607
608 ;; Alias, kept temporarily.
609 (define-obsolete-function-alias 'new-frame 'make-frame "22.1")
610
611 (defvar frame-inherited-parameters '()
612 "Parameters `make-frame' copies from the `selected-frame' to the new frame.")
613
614 (defvar x-display-name)
615
616 (defun make-frame (&optional parameters)
617 "Return a newly created frame displaying the current buffer.
618 Optional argument PARAMETERS is an alist of frame parameters for
619 the new frame. Each element of PARAMETERS should have the
620 form (NAME . VALUE), for example:
621
622 (name . STRING) The frame should be named STRING.
623
624 (width . NUMBER) The frame should be NUMBER characters in width.
625 (height . NUMBER) The frame should be NUMBER text lines high.
626
627 You cannot specify either `width' or `height', you must specify
628 neither or both.
629
630 (minibuffer . t) The frame should have a minibuffer.
631 (minibuffer . nil) The frame should have no minibuffer.
632 (minibuffer . only) The frame should contain only a minibuffer.
633 (minibuffer . WINDOW) The frame should use WINDOW as its minibuffer window.
634
635 (window-system . nil) The frame should be displayed on a terminal device.
636 (window-system . x) The frame should be displayed in an X window.
637
638 (display . \":0\") The frame should appear on display :0.
639
640 (terminal . TERMINAL) The frame should use the terminal object TERMINAL.
641
642 In addition, any parameter specified in `default-frame-alist',
643 but not present in PARAMETERS, is applied.
644
645 Before creating the frame (via `frame-creation-function-alist'),
646 this function runs the hook `before-make-frame-hook'. After
647 creating the frame, it runs the hook `after-make-frame-functions'
648 with one arg, the newly created frame.
649
650 If a display parameter is supplied and a window-system is not,
651 guess the window-system from the display.
652
653 On graphical displays, this function does not itself make the new
654 frame the selected frame. However, the window system may select
655 the new frame according to its own rules."
656 (interactive)
657 (let* ((display (cdr (assq 'display parameters)))
658 (w (cond
659 ((assq 'terminal parameters)
660 (let ((type (terminal-live-p
661 (cdr (assq 'terminal parameters)))))
662 (cond
663 ((eq t type) nil)
664 ((null type) (error "Terminal %s does not exist"
665 (cdr (assq 'terminal parameters))))
666 (t type))))
667 ((assq 'window-system parameters)
668 (cdr (assq 'window-system parameters)))
669 (display
670 (or (window-system-for-display display)
671 (error "Don't know how to interpret display %S"
672 display)))
673 (t window-system)))
674 (oldframe (selected-frame))
675 (params parameters)
676 frame)
677
678 (unless (get w 'window-system-initialized)
679 (let ((window-system w)) ;Hack attack!
680 (window-system-initialization display))
681 (setq x-display-name display)
682 (put w 'window-system-initialized t))
683
684 ;; Add parameters from `window-system-default-frame-alist'.
685 (dolist (p (cdr (assq w window-system-default-frame-alist)))
686 (unless (assq (car p) params)
687 (push p params)))
688 ;; Add parameters from `default-frame-alist'.
689 (dolist (p default-frame-alist)
690 (unless (assq (car p) params)
691 (push p params)))
692 ;; Now make the frame.
693 (run-hooks 'before-make-frame-hook)
694
695 ;; (setq frame-size-history '(1000))
696
697 (setq frame (let ((window-system w)) ;Hack attack!
698 (frame-creation-function params)))
699 (normal-erase-is-backspace-setup-frame frame)
700 ;; Inherit the original frame's parameters.
701 (dolist (param frame-inherited-parameters)
702 (unless (assq param parameters) ;Overridden by explicit parameters.
703 (let ((val (frame-parameter oldframe param)))
704 (when val (set-frame-parameter frame param val)))))
705
706 (when (numberp (car frame-size-history))
707 (setq frame-size-history
708 (cons (1- (car frame-size-history))
709 (cons (list frame "MAKE-FRAME")
710 (cdr frame-size-history)))))
711
712 ;; We can run `window-configuration-change-hook' for this frame now.
713 (frame-after-make-frame frame t)
714 (run-hook-with-args 'after-make-frame-functions frame)
715 frame))
716
717 (defun filtered-frame-list (predicate)
718 "Return a list of all live frames which satisfy PREDICATE."
719 (let* ((frames (frame-list))
720 (list frames))
721 (while (consp frames)
722 (unless (funcall predicate (car frames))
723 (setcar frames nil))
724 (setq frames (cdr frames)))
725 (delq nil list)))
726
727 (defun minibuffer-frame-list ()
728 "Return a list of all frames with their own minibuffers."
729 (filtered-frame-list
730 (lambda (frame)
731 (eq frame (window-frame (minibuffer-window frame))))))
732
733 ;; Used to be called `terminal-id' in termdev.el.
734 (defun get-device-terminal (device)
735 "Return the terminal corresponding to DEVICE.
736 DEVICE can be a terminal, a frame, nil (meaning the selected frame's terminal),
737 the name of an X display device (HOST.SERVER.SCREEN) or a tty device file."
738 (cond
739 ((or (null device) (framep device))
740 (frame-terminal device))
741 ((stringp device)
742 (let ((f (car (filtered-frame-list
743 (lambda (frame)
744 (or (equal (frame-parameter frame 'display) device)
745 (equal (frame-parameter frame 'tty) device)))))))
746 (or f (error "Display %s does not exist" device))
747 (frame-terminal f)))
748 ((terminal-live-p device) device)
749 (t
750 (error "Invalid argument %s in `get-device-terminal'" device))))
751
752 (defun frames-on-display-list (&optional device)
753 "Return a list of all frames on DEVICE.
754
755 DEVICE should be a terminal, a frame,
756 or a name of an X display or tty (a string of the form
757 HOST:SERVER.SCREEN).
758
759 If DEVICE is omitted or nil, it defaults to the selected
760 frame's terminal device."
761 (let* ((terminal (get-device-terminal device))
762 (func #'(lambda (frame)
763 (eq (frame-terminal frame) terminal))))
764 (filtered-frame-list func)))
765
766 (defun framep-on-display (&optional terminal)
767 "Return the type of frames on TERMINAL.
768 TERMINAL may be a terminal id, a display name or a frame. If it
769 is a frame, its type is returned. If TERMINAL is omitted or nil,
770 it defaults to the selected frame's terminal device. All frames
771 on a given display are of the same type."
772 (or (terminal-live-p terminal)
773 (framep terminal)
774 (framep (car (frames-on-display-list terminal)))))
775
776 (defun frame-remove-geometry-params (param-list)
777 "Return the parameter list PARAM-LIST, but with geometry specs removed.
778 This deletes all bindings in PARAM-LIST for `top', `left', `width',
779 `height', `user-size' and `user-position' parameters.
780 Emacs uses this to avoid overriding explicit moves and resizings from
781 the user during startup."
782 (setq param-list (cons nil param-list))
783 (let ((tail param-list))
784 (while (consp (cdr tail))
785 (if (and (consp (car (cdr tail)))
786 (memq (car (car (cdr tail)))
787 '(height width top left user-position user-size)))
788 (progn
789 (setq frame-initial-geometry-arguments
790 (cons (car (cdr tail)) frame-initial-geometry-arguments))
791 (setcdr tail (cdr (cdr tail))))
792 (setq tail (cdr tail)))))
793 (setq frame-initial-geometry-arguments
794 (nreverse frame-initial-geometry-arguments))
795 (cdr param-list))
796
797 (declare-function x-focus-frame "frame.c" (frame))
798
799 (defun select-frame-set-input-focus (frame &optional norecord)
800 "Select FRAME, raise it, and set input focus, if possible.
801 If `mouse-autoselect-window' is non-nil, also move mouse pointer
802 to FRAME's selected window. Otherwise, if `focus-follows-mouse'
803 is non-nil, move mouse cursor to FRAME.
804
805 Optional argument NORECORD means to neither change the order of
806 recently selected windows nor the buffer list."
807 (select-frame frame norecord)
808 (raise-frame frame)
809 ;; Ensure, if possible, that FRAME gets input focus.
810 (when (memq (window-system frame) '(x w32 ns))
811 (x-focus-frame frame))
812 ;; Move mouse cursor if necessary.
813 (cond
814 (mouse-autoselect-window
815 (let ((edges (window-inside-edges (frame-selected-window frame))))
816 ;; Move mouse cursor into FRAME's selected window to avoid that
817 ;; Emacs mouse-autoselects another window.
818 (set-mouse-position frame (nth 2 edges) (nth 1 edges))))
819 (focus-follows-mouse
820 ;; Move mouse cursor into FRAME to avoid that another frame gets
821 ;; selected by the window manager.
822 (set-mouse-position frame (1- (frame-width frame)) 0))))
823
824 (defun other-frame (arg)
825 "Select the ARGth different visible frame on current display, and raise it.
826 All frames are arranged in a cyclic order.
827 This command selects the frame ARG steps away in that order.
828 A negative ARG moves in the opposite order.
829
830 To make this command work properly, you must tell Emacs
831 how the system (or the window manager) generally handles
832 focus-switching between windows. If moving the mouse onto a window
833 selects it (gives it focus), set `focus-follows-mouse' to t.
834 Otherwise, that variable should be nil."
835 (interactive "p")
836 (let ((frame (selected-frame)))
837 (while (> arg 0)
838 (setq frame (next-frame frame))
839 (while (not (eq (frame-visible-p frame) t))
840 (setq frame (next-frame frame)))
841 (setq arg (1- arg)))
842 (while (< arg 0)
843 (setq frame (previous-frame frame))
844 (while (not (eq (frame-visible-p frame) t))
845 (setq frame (previous-frame frame)))
846 (setq arg (1+ arg)))
847 (select-frame-set-input-focus frame)))
848
849 (defun iconify-or-deiconify-frame ()
850 "Iconify the selected frame, or deiconify if it's currently an icon."
851 (interactive)
852 (if (eq (cdr (assq 'visibility (frame-parameters))) t)
853 (iconify-frame)
854 (make-frame-visible)))
855
856 (defun suspend-frame ()
857 "Do whatever is right to suspend the current frame.
858 Calls `suspend-emacs' if invoked from the controlling tty device,
859 `suspend-tty' from a secondary tty device, and
860 `iconify-or-deiconify-frame' from an X frame."
861 (interactive)
862 (let ((type (framep (selected-frame))))
863 (cond
864 ((memq type '(x ns w32)) (iconify-or-deiconify-frame))
865 ((eq type t)
866 (if (controlling-tty-p)
867 (suspend-emacs)
868 (suspend-tty)))
869 (t (suspend-emacs)))))
870
871 (defun make-frame-names-alist ()
872 ;; Only consider the frames on the same display.
873 (let* ((current-frame (selected-frame))
874 (falist
875 (cons
876 (cons (frame-parameter current-frame 'name) current-frame) nil))
877 (frame (next-frame nil 0)))
878 (while (not (eq frame current-frame))
879 (progn
880 (push (cons (frame-parameter frame 'name) frame) falist)
881 (setq frame (next-frame frame 0))))
882 falist))
883
884 (defvar frame-name-history nil)
885 (defun select-frame-by-name (name)
886 "Select the frame on the current terminal whose name is NAME and raise it.
887 If there is no frame by that name, signal an error."
888 (interactive
889 (let* ((frame-names-alist (make-frame-names-alist))
890 (default (car (car frame-names-alist)))
891 (input (completing-read
892 (format "Select Frame (default %s): " default)
893 frame-names-alist nil t nil 'frame-name-history)))
894 (if (= (length input) 0)
895 (list default)
896 (list input))))
897 (let* ((frame-names-alist (make-frame-names-alist))
898 (frame (cdr (assoc name frame-names-alist))))
899 (if frame
900 (select-frame-set-input-focus frame)
901 (error "There is no frame named `%s'" name))))
902
903 \f
904 ;;;; Background mode.
905
906 (defcustom frame-background-mode nil
907 "The brightness of the background.
908 Set this to the symbol `dark' if your background color is dark,
909 `light' if your background is light, or nil (automatic by default)
910 if you want Emacs to examine the brightness for you.
911
912 If you change this without using customize, you should use
913 `frame-set-background-mode' to update existing frames;
914 e.g. (mapc \\='frame-set-background-mode (frame-list))."
915 :group 'faces
916 :set #'(lambda (var value)
917 (set-default var value)
918 (mapc 'frame-set-background-mode (frame-list)))
919 :initialize 'custom-initialize-changed
920 :type '(choice (const dark)
921 (const light)
922 (const :tag "automatic" nil)))
923
924 (declare-function x-get-resource "frame.c"
925 (attribute class &optional component subclass))
926
927 ;; Only used if window-system is not null.
928 (declare-function x-display-grayscale-p "xfns.c" (&optional terminal))
929
930 (defvar inhibit-frame-set-background-mode nil)
931
932 (defun frame-set-background-mode (frame &optional keep-face-specs)
933 "Set up display-dependent faces on FRAME.
934 Display-dependent faces are those which have different definitions
935 according to the `background-mode' and `display-type' frame parameters.
936
937 If optional arg KEEP-FACE-SPECS is non-nil, don't recalculate
938 face specs for the new background mode."
939 (unless inhibit-frame-set-background-mode
940 (let* ((frame-default-bg-mode (frame-terminal-default-bg-mode frame))
941 (bg-color (frame-parameter frame 'background-color))
942 (tty-type (tty-type frame))
943 (default-bg-mode
944 (if (or (window-system frame)
945 (and tty-type
946 (string-match "^\\(xterm\\|\\rxvt\\|dtterm\\|eterm\\)"
947 tty-type)))
948 'light
949 'dark))
950 (non-default-bg-mode (if (eq default-bg-mode 'light) 'dark 'light))
951 (bg-mode
952 (cond (frame-default-bg-mode)
953 ((equal bg-color "unspecified-fg") ; inverted colors
954 non-default-bg-mode)
955 ((not (color-values bg-color frame))
956 default-bg-mode)
957 ((>= (apply '+ (color-values bg-color frame))
958 ;; Just looking at the screen, colors whose
959 ;; values add up to .6 of the white total
960 ;; still look dark to me.
961 (* (apply '+ (color-values "white" frame)) .6))
962 'light)
963 (t 'dark)))
964 (display-type
965 (cond ((null (window-system frame))
966 (if (tty-display-color-p frame) 'color 'mono))
967 ((display-color-p frame)
968 'color)
969 ((x-display-grayscale-p frame)
970 'grayscale)
971 (t 'mono)))
972 (old-bg-mode
973 (frame-parameter frame 'background-mode))
974 (old-display-type
975 (frame-parameter frame 'display-type)))
976
977 (unless (and (eq bg-mode old-bg-mode) (eq display-type old-display-type))
978 (let ((locally-modified-faces nil)
979 ;; Prevent face-spec-recalc from calling this function
980 ;; again, resulting in a loop (bug#911).
981 (inhibit-frame-set-background-mode t)
982 (params (list (cons 'background-mode bg-mode)
983 (cons 'display-type display-type))))
984 (if keep-face-specs
985 (modify-frame-parameters frame params)
986 ;; If we are recomputing face specs, first collect a list
987 ;; of faces that don't match their face-specs. These are
988 ;; the faces modified on FRAME, and we avoid changing them
989 ;; below. Use a negative list to avoid consing (we assume
990 ;; most faces are unmodified).
991 (dolist (face (face-list))
992 (and (not (get face 'face-override-spec))
993 (not (face-spec-match-p face
994 (face-user-default-spec face)
995 (selected-frame)))
996 (push face locally-modified-faces)))
997 ;; Now change to the new frame parameters
998 (modify-frame-parameters frame params)
999 ;; For all unmodified named faces, choose face specs
1000 ;; matching the new frame parameters.
1001 (dolist (face (face-list))
1002 (unless (memq face locally-modified-faces)
1003 (face-spec-recalc face frame)))))))))
1004
1005 (defun frame-terminal-default-bg-mode (frame)
1006 "Return the default background mode of FRAME.
1007 This checks the `frame-background-mode' variable, the X resource
1008 named \"backgroundMode\" (if FRAME is an X frame), and finally
1009 the `background-mode' terminal parameter."
1010 (or frame-background-mode
1011 (let ((bg-resource
1012 (and (window-system frame)
1013 (x-get-resource "backgroundMode" "BackgroundMode"))))
1014 (if bg-resource
1015 (intern (downcase bg-resource))))
1016 (terminal-parameter frame 'background-mode)))
1017
1018 \f
1019 ;;;; Frame configurations
1020
1021 (defun current-frame-configuration ()
1022 "Return a list describing the positions and states of all frames.
1023 Its car is `frame-configuration'.
1024 Each element of the cdr is a list of the form (FRAME ALIST WINDOW-CONFIG),
1025 where
1026 FRAME is a frame object,
1027 ALIST is an association list specifying some of FRAME's parameters, and
1028 WINDOW-CONFIG is a window configuration object for FRAME."
1029 (cons 'frame-configuration
1030 (mapcar (lambda (frame)
1031 (list frame
1032 (frame-parameters frame)
1033 (current-window-configuration frame)))
1034 (frame-list))))
1035
1036 (defun set-frame-configuration (configuration &optional nodelete)
1037 "Restore the frames to the state described by CONFIGURATION.
1038 Each frame listed in CONFIGURATION has its position, size, window
1039 configuration, and other parameters set as specified in CONFIGURATION.
1040 However, this function does not restore deleted frames.
1041
1042 Ordinarily, this function deletes all existing frames not
1043 listed in CONFIGURATION. But if optional second argument NODELETE
1044 is given and non-nil, the unwanted frames are iconified instead."
1045 (or (frame-configuration-p configuration)
1046 (signal 'wrong-type-argument
1047 (list 'frame-configuration-p configuration)))
1048 (let ((config-alist (cdr configuration))
1049 frames-to-delete)
1050 (dolist (frame (frame-list))
1051 (let ((parameters (assq frame config-alist)))
1052 (if parameters
1053 (progn
1054 (modify-frame-parameters
1055 frame
1056 ;; Since we can't set a frame's minibuffer status,
1057 ;; we might as well omit the parameter altogether.
1058 (let* ((parms (nth 1 parameters))
1059 (mini (assq 'minibuffer parms))
1060 (name (assq 'name parms))
1061 (explicit-name (cdr (assq 'explicit-name parms))))
1062 (when mini (setq parms (delq mini parms)))
1063 ;; Leave name in iff it was set explicitly.
1064 ;; This should fix the behavior reported in
1065 ;; http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg01632.html
1066 (when (and name (not explicit-name))
1067 (setq parms (delq name parms)))
1068 parms))
1069 (set-window-configuration (nth 2 parameters)))
1070 (setq frames-to-delete (cons frame frames-to-delete)))))
1071 (mapc (if nodelete
1072 ;; Note: making frames invisible here was tried
1073 ;; but led to some strange behavior--each time the frame
1074 ;; was made visible again, the window manager asked afresh
1075 ;; for where to put it.
1076 'iconify-frame
1077 'delete-frame)
1078 frames-to-delete)))
1079 \f
1080 ;;;; Convenience functions for accessing and interactively changing
1081 ;;;; frame parameters.
1082
1083 (defun frame-height (&optional frame)
1084 "Return number of lines available for display on FRAME.
1085 If FRAME is omitted, describe the currently selected frame.
1086 Exactly what is included in the return value depends on the
1087 window-system and toolkit in use - see `frame-pixel-height' for
1088 more details. The lines are in units of the default font height.
1089
1090 The result is roughly related to the frame pixel height via
1091 height in pixels = height in lines * `frame-char-height'.
1092 However, this is only approximate, and is complicated e.g. by the
1093 fact that individual window lines and menu bar lines can have
1094 differing font heights."
1095 (cdr (assq 'height (frame-parameters frame))))
1096
1097 (defun frame-width (&optional frame)
1098 "Return number of columns available for display on FRAME.
1099 If FRAME is omitted, describe the currently selected frame."
1100 (cdr (assq 'width (frame-parameters frame))))
1101
1102 (declare-function x-list-fonts "xfaces.c"
1103 (pattern &optional face frame maximum width))
1104
1105 (define-obsolete-function-alias 'set-default-font 'set-frame-font "23.1")
1106
1107 (defun set-frame-font (font &optional keep-size frames)
1108 "Set the default font to FONT.
1109 When called interactively, prompt for the name of a font, and use
1110 that font on the selected frame. When called from Lisp, FONT
1111 should be a font name (a string), a font object, font entity, or
1112 font spec.
1113
1114 If KEEP-SIZE is nil, keep the number of frame lines and columns
1115 fixed. If KEEP-SIZE is non-nil (or with a prefix argument), try
1116 to keep the current frame size fixed (in pixels) by adjusting the
1117 number of lines and columns.
1118
1119 If FRAMES is nil, apply the font to the selected frame only.
1120 If FRAMES is non-nil, it should be a list of frames to act upon,
1121 or t meaning all existing graphical frames.
1122 Also, if FRAMES is non-nil, alter the user's Customization settings
1123 as though the font-related attributes of the `default' face had been
1124 \"set in this session\", so that the font is applied to future frames."
1125 (interactive
1126 (let* ((completion-ignore-case t)
1127 (font (completing-read "Font name: "
1128 ;; x-list-fonts will fail with an error
1129 ;; if this frame doesn't support fonts.
1130 (x-list-fonts "*" nil (selected-frame))
1131 nil nil nil nil
1132 (frame-parameter nil 'font))))
1133 (list font current-prefix-arg nil)))
1134 (when (or (stringp font) (fontp font))
1135 (let* ((this-frame (selected-frame))
1136 ;; FRAMES nil means affect the selected frame.
1137 (frame-list (cond ((null frames)
1138 (list this-frame))
1139 ((eq frames t)
1140 (frame-list))
1141 (t frames)))
1142 height width)
1143 (dolist (f frame-list)
1144 (when (display-multi-font-p f)
1145 (if keep-size
1146 (setq height (* (frame-parameter f 'height)
1147 (frame-char-height f))
1148 width (* (frame-parameter f 'width)
1149 (frame-char-width f))))
1150 ;; When set-face-attribute is called for :font, Emacs
1151 ;; guesses the best font according to other face attributes
1152 ;; (:width, :weight, etc.) so reset them too (Bug#2476).
1153 (set-face-attribute 'default f
1154 :width 'normal :weight 'normal
1155 :slant 'normal :font font)
1156 (if keep-size
1157 (modify-frame-parameters
1158 f
1159 (list (cons 'height (round height (frame-char-height f)))
1160 (cons 'width (round width (frame-char-width f))))))))
1161 (when frames
1162 ;; Alter the user's Custom setting of the `default' face, but
1163 ;; only for font-related attributes.
1164 (let ((specs (cadr (assq 'user (get 'default 'theme-face))))
1165 (attrs '(:family :foundry :slant :weight :height :width))
1166 (new-specs nil))
1167 (if (null specs) (setq specs '((t nil))))
1168 (dolist (spec specs)
1169 ;; Each SPEC has the form (DISPLAY ATTRIBUTE-PLIST)
1170 (let ((display (nth 0 spec))
1171 (plist (copy-tree (nth 1 spec))))
1172 ;; Alter only DISPLAY conditions matching this frame.
1173 (when (or (memq display '(t default))
1174 (face-spec-set-match-display display this-frame))
1175 (dolist (attr attrs)
1176 (setq plist (plist-put plist attr
1177 (face-attribute 'default attr)))))
1178 (push (list display plist) new-specs)))
1179 (setq new-specs (nreverse new-specs))
1180 (put 'default 'customized-face new-specs)
1181 (custom-push-theme 'theme-face 'default 'user 'set new-specs)
1182 (put 'default 'face-modified nil))))
1183 (run-hooks 'after-setting-font-hook 'after-setting-font-hooks)))
1184
1185 (defun set-frame-parameter (frame parameter value)
1186 "Set frame parameter PARAMETER to VALUE on FRAME.
1187 If FRAME is nil, it defaults to the selected frame.
1188 See `modify-frame-parameters'."
1189 (modify-frame-parameters frame (list (cons parameter value))))
1190
1191 (defun set-background-color (color-name)
1192 "Set the background color of the selected frame to COLOR-NAME.
1193 When called interactively, prompt for the name of the color to use.
1194 To get the frame's current background color, use `frame-parameters'."
1195 (interactive (list (read-color "Background color: ")))
1196 (modify-frame-parameters (selected-frame)
1197 (list (cons 'background-color color-name)))
1198 (or window-system
1199 (face-set-after-frame-default (selected-frame)
1200 (list
1201 (cons 'background-color color-name)
1202 ;; Pass the foreground-color as
1203 ;; well, if defined, to avoid
1204 ;; losing it when faces are reset
1205 ;; to their defaults.
1206 (assq 'foreground-color
1207 (frame-parameters))))))
1208
1209 (defun set-foreground-color (color-name)
1210 "Set the foreground color of the selected frame to COLOR-NAME.
1211 When called interactively, prompt for the name of the color to use.
1212 To get the frame's current foreground color, use `frame-parameters'."
1213 (interactive (list (read-color "Foreground color: ")))
1214 (modify-frame-parameters (selected-frame)
1215 (list (cons 'foreground-color color-name)))
1216 (or window-system
1217 (face-set-after-frame-default (selected-frame)
1218 (list
1219 (cons 'foreground-color color-name)
1220 ;; Pass the background-color as
1221 ;; well, if defined, to avoid
1222 ;; losing it when faces are reset
1223 ;; to their defaults.
1224 (assq 'background-color
1225 (frame-parameters))))))
1226
1227 (defun set-cursor-color (color-name)
1228 "Set the text cursor color of the selected frame to COLOR-NAME.
1229 When called interactively, prompt for the name of the color to use.
1230 This works by setting the `cursor-color' frame parameter on the
1231 selected frame.
1232
1233 You can also set the text cursor color, for all frames, by
1234 customizing the `cursor' face."
1235 (interactive (list (read-color "Cursor color: ")))
1236 (modify-frame-parameters (selected-frame)
1237 (list (cons 'cursor-color color-name))))
1238
1239 (defun set-mouse-color (color-name)
1240 "Set the color of the mouse pointer of the selected frame to COLOR-NAME.
1241 When called interactively, prompt for the name of the color to use.
1242 To get the frame's current mouse color, use `frame-parameters'."
1243 (interactive (list (read-color "Mouse color: ")))
1244 (modify-frame-parameters (selected-frame)
1245 (list (cons 'mouse-color
1246 (or color-name
1247 (cdr (assq 'mouse-color
1248 (frame-parameters))))))))
1249
1250 (defun set-border-color (color-name)
1251 "Set the color of the border of the selected frame to COLOR-NAME.
1252 When called interactively, prompt for the name of the color to use.
1253 To get the frame's current border color, use `frame-parameters'."
1254 (interactive (list (read-color "Border color: ")))
1255 (modify-frame-parameters (selected-frame)
1256 (list (cons 'border-color color-name))))
1257
1258 (define-minor-mode auto-raise-mode
1259 "Toggle whether or not selected frames should auto-raise.
1260 With a prefix argument ARG, enable Auto Raise mode if ARG is
1261 positive, and disable it otherwise. If called from Lisp, enable
1262 the mode if ARG is omitted or nil.
1263
1264 Auto Raise mode does nothing under most window managers, which
1265 switch focus on mouse clicks. It only has an effect if your
1266 window manager switches focus on mouse movement (in which case
1267 you should also change `focus-follows-mouse' to t). Then,
1268 enabling Auto Raise mode causes any graphical Emacs frame which
1269 acquires focus to be automatically raised.
1270
1271 Note that this minor mode controls Emacs's own auto-raise
1272 feature. Window managers that switch focus on mouse movement
1273 often have their own auto-raise feature."
1274 :variable (frame-parameter nil 'auto-raise)
1275 (if (frame-parameter nil 'auto-raise)
1276 (raise-frame)))
1277
1278 (define-minor-mode auto-lower-mode
1279 "Toggle whether or not the selected frame should auto-lower.
1280 With a prefix argument ARG, enable Auto Lower mode if ARG is
1281 positive, and disable it otherwise. If called from Lisp, enable
1282 the mode if ARG is omitted or nil.
1283
1284 Auto Lower mode does nothing under most window managers, which
1285 switch focus on mouse clicks. It only has an effect if your
1286 window manager switches focus on mouse movement (in which case
1287 you should also change `focus-follows-mouse' to t). Then,
1288 enabling Auto Lower Mode causes any graphical Emacs frame which
1289 loses focus to be automatically lowered.
1290
1291 Note that this minor mode controls Emacs's own auto-lower
1292 feature. Window managers that switch focus on mouse movement
1293 often have their own features for raising or lowering frames."
1294 :variable (frame-parameter nil 'auto-lower))
1295
1296 (defun set-frame-name (name)
1297 "Set the name of the selected frame to NAME.
1298 When called interactively, prompt for the name of the frame.
1299 On text terminals, the frame name is displayed on the mode line.
1300 On graphical displays, it is displayed on the frame's title bar."
1301 (interactive "sFrame name: ")
1302 (modify-frame-parameters (selected-frame)
1303 (list (cons 'name name))))
1304
1305 (defun frame-current-scroll-bars (&optional frame)
1306 "Return the current scroll-bar types for frame FRAME.
1307 Value is a cons (VERTICAL . HORIZ0NTAL) where VERTICAL specifies
1308 the current location of the vertical scroll-bars (`left', `right'
1309 or nil), and HORIZONTAL specifies the current location of the
1310 horizontal scroll bars (`bottom' or nil). FRAME must specify a
1311 live frame and defaults to the selected one."
1312 (let* ((frame (window-normalize-frame frame))
1313 (vertical (frame-parameter frame 'vertical-scroll-bars))
1314 (horizontal (frame-parameter frame 'horizontal-scroll-bars)))
1315 (unless (memq vertical '(left right nil))
1316 (setq vertical default-frame-scroll-bars))
1317 (cons vertical (and horizontal 'bottom))))
1318
1319 (declare-function x-frame-geometry "xfns.c" (&optional frame))
1320 (declare-function w32-frame-geometry "w32fns.c" (&optional frame))
1321 (declare-function ns-frame-geometry "nsfns.m" (&optional frame))
1322
1323 (defun frame-geometry (&optional frame)
1324 "Return geometric attributes of FRAME.
1325 FRAME must be a live frame and defaults to the selected one. The return
1326 value is an association list of the attributes listed below. All height
1327 and width values are in pixels.
1328
1329 `outer-position' is a cons of the outer left and top edges of FRAME
1330 relative to the origin - the position (0, 0) - of FRAME's display.
1331
1332 `outer-size' is a cons of the outer width and height of FRAME. The
1333 outer size includes the title bar and the external borders as well as
1334 any menu and/or tool bar of frame.
1335
1336 `external-border-size' is a cons of the horizontal and vertical width of
1337 FRAME's external borders as supplied by the window manager.
1338
1339 `title-bar-size' is a cons of the width and height of the title bar of
1340 FRAME as supplied by the window manager. If both of them are zero,
1341 FRAME has no title bar. If only the width is zero, Emacs was not
1342 able to retrieve the width information.
1343
1344 `menu-bar-external', if non-nil, means the menu bar is external (never
1345 included in the inner edges of FRAME).
1346
1347 `menu-bar-size' is a cons of the width and height of the menu bar of
1348 FRAME.
1349
1350 `tool-bar-external', if non-nil, means the tool bar is external (never
1351 included in the inner edges of FRAME).
1352
1353 `tool-bar-position' tells on which side the tool bar on FRAME is and can
1354 be one of `left', `top', `right' or `bottom'. If this is nil, FRAME
1355 has no tool bar.
1356
1357 `tool-bar-size' is a cons of the width and height of the tool bar of
1358 FRAME.
1359
1360 `internal-border-width' is the width of the internal border of
1361 FRAME."
1362 (let* ((frame (window-normalize-frame frame))
1363 (frame-type (framep-on-display frame)))
1364 (cond
1365 ((eq frame-type 'x)
1366 (x-frame-geometry frame))
1367 ((eq frame-type 'w32)
1368 (w32-frame-geometry frame))
1369 ((eq frame-type 'ns)
1370 (ns-frame-geometry frame))
1371 (t
1372 (list
1373 '(outer-position 0 . 0)
1374 (cons 'outer-size (cons (frame-width frame) (frame-height frame)))
1375 '(external-border-size 0 . 0)
1376 '(title-bar-size 0 . 0)
1377 '(menu-bar-external . nil)
1378 (let ((menu-bar-lines (frame-parameter frame 'menu-bar-lines)))
1379 (cons 'menu-bar-size
1380 (if menu-bar-lines
1381 (cons (frame-width frame) 1)
1382 1 0)))
1383 '(tool-bar-external . nil)
1384 '(tool-bar-position . nil)
1385 '(tool-bar-size 0 . 0)
1386 (cons 'internal-border-width
1387 (frame-parameter frame 'internal-border-width)))))))
1388
1389 (defun frame--size-history (&optional frame)
1390 "Print history of resize operations for FRAME.
1391 Print prettified version of `frame-size-history' into a buffer
1392 called *frame-size-history*. Optional argument FRAME denotes the
1393 frame whose history will be printed. FRAME defaults to the
1394 selected frame."
1395 (let ((history (reverse frame-size-history))
1396 entry)
1397 (setq frame (window-normalize-frame frame))
1398 (with-current-buffer (get-buffer-create "*frame-size-history*")
1399 (erase-buffer)
1400 (insert (format "Frame size history of %s\n" frame))
1401 (while (listp (setq entry (pop history)))
1402 (when (eq (car entry) frame)
1403 (pop entry)
1404 (insert (format "%s" (pop entry)))
1405 (move-to-column 24 t)
1406 (while entry
1407 (insert (format " %s" (pop entry))))
1408 (insert "\n"))))))
1409
1410 (declare-function x-frame-edges "xfns.c" (&optional frame type))
1411 (declare-function w32-frame-edges "w32fns.c" (&optional frame type))
1412 (declare-function ns-frame-edges "nsfns.m" (&optional frame type))
1413
1414 (defun frame-edges (&optional frame type)
1415 "Return coordinates of FRAME's edges.
1416 FRAME must be a live frame and defaults to the selected one. The
1417 list returned has the form (LEFT TOP RIGHT BOTTOM) where all
1418 values are in pixels relative to the origin - the position (0, 0)
1419 - of FRAME's display. For terminal frames all values are
1420 relative to LEFT and TOP which are both zero.
1421
1422 Optional argument TYPE specifies the type of the edges. TYPE
1423 `outer-edges' means to return the outer edges of FRAME. TYPE
1424 `native-edges' (or nil) means to return the native edges of
1425 FRAME. TYPE `inner-edges' means to return the inner edges of
1426 FRAME."
1427 (let* ((frame (window-normalize-frame frame))
1428 (frame-type (framep-on-display frame)))
1429 (cond
1430 ((eq frame-type 'x)
1431 (x-frame-edges frame type))
1432 ((eq frame-type 'w32)
1433 (w32-frame-edges frame type))
1434 ((eq frame-type 'ns)
1435 (ns-frame-edges frame type))
1436 (t
1437 (list 0 0 (frame-width frame) (frame-height frame))))))
1438
1439 (declare-function w32-mouse-absolute-pixel-position "w32fns.c")
1440 (declare-function x-mouse-absolute-pixel-position "xfns.c")
1441
1442 (defun mouse-absolute-pixel-position ()
1443 "Return absolute position of mouse cursor in pixels.
1444 The position is returned as a cons cell (X . Y) of the
1445 coordinates of the mouse cursor position in pixels relative to a
1446 position (0, 0) of the selected frame's terminal."
1447 (let ((frame-type (framep-on-display)))
1448 (cond
1449 ((eq frame-type 'x)
1450 (x-mouse-absolute-pixel-position))
1451 ((eq frame-type 'w32)
1452 (w32-mouse-absolute-pixel-position))
1453 (t
1454 (cons 0 0)))))
1455
1456 (declare-function w32-set-mouse-absolute-pixel-position "w32fns.c" (x y))
1457 (declare-function x-set-mouse-absolute-pixel-position "xfns.c" (x y))
1458
1459 (defun set-mouse-absolute-pixel-position (x y)
1460 "Move mouse pointer to absolute pixel position (X, Y).
1461 The coordinates X and Y are interpreted in pixels relative to a
1462 position (0, 0) of the selected frame's terminal."
1463 (let ((frame-type (framep-on-display)))
1464 (cond
1465 ((eq frame-type 'x)
1466 (x-set-mouse-absolute-pixel-position x y))
1467 ((eq frame-type 'w32)
1468 (w32-set-mouse-absolute-pixel-position x y)))))
1469
1470 (defun frame-monitor-attributes (&optional frame)
1471 "Return the attributes of the physical monitor dominating FRAME.
1472 If FRAME is omitted or nil, describe the currently selected frame.
1473
1474 A frame is dominated by a physical monitor when either the
1475 largest area of the frame resides in the monitor, or the monitor
1476 is the closest to the frame if the frame does not intersect any
1477 physical monitors.
1478
1479 See `display-monitor-attributes-list' for the list of attribute
1480 keys and their meanings."
1481 (or frame (setq frame (selected-frame)))
1482 (cl-loop for attributes in (display-monitor-attributes-list frame)
1483 for frames = (cdr (assq 'frames attributes))
1484 if (memq frame frames) return attributes))
1485
1486 \f
1487 ;;;; Frame/display capabilities.
1488
1489 (declare-function msdos-mouse-p "dosfns.c")
1490
1491 (defun display-mouse-p (&optional display)
1492 "Return non-nil if DISPLAY has a mouse available.
1493 DISPLAY can be a display name, a frame, or nil (meaning the selected
1494 frame's display)."
1495 (let ((frame-type (framep-on-display display)))
1496 (cond
1497 ((eq frame-type 'pc)
1498 (msdos-mouse-p))
1499 ((eq frame-type 'w32)
1500 (with-no-warnings
1501 (> w32-num-mouse-buttons 0)))
1502 ((memq frame-type '(x ns))
1503 t) ;; We assume X and NeXTstep *always* have a pointing device
1504 (t
1505 (or (and (featurep 'xt-mouse)
1506 xterm-mouse-mode)
1507 ;; t-mouse is distributed with the GPM package. It doesn't have
1508 ;; a toggle.
1509 (featurep 't-mouse)
1510 ;; No way to check whether a w32 console has a mouse, assume
1511 ;; it always does.
1512 (boundp 'w32-use-full-screen-buffer))))))
1513
1514 (defun display-popup-menus-p (&optional display)
1515 "Return non-nil if popup menus are supported on DISPLAY.
1516 DISPLAY can be a display name, a frame, or nil (meaning the selected
1517 frame's display).
1518 Support for popup menus requires that the mouse be available."
1519 (display-mouse-p display))
1520
1521 (defun display-graphic-p (&optional display)
1522 "Return non-nil if DISPLAY is a graphic display.
1523 Graphical displays are those which are capable of displaying several
1524 frames and several different fonts at once. This is true for displays
1525 that use a window system such as X, and false for text-only terminals.
1526 DISPLAY can be a display name, a frame, or nil (meaning the selected
1527 frame's display)."
1528 (not (null (memq (framep-on-display display) '(x w32 ns)))))
1529
1530 (defun display-images-p (&optional display)
1531 "Return non-nil if DISPLAY can display images.
1532
1533 DISPLAY can be a display name, a frame, or nil (meaning the selected
1534 frame's display)."
1535 (and (display-graphic-p display)
1536 (fboundp 'image-mask-p)
1537 (fboundp 'image-size)))
1538
1539 (defalias 'display-multi-frame-p 'display-graphic-p)
1540 (defalias 'display-multi-font-p 'display-graphic-p)
1541
1542 (defun display-selections-p (&optional display)
1543 "Return non-nil if DISPLAY supports selections.
1544 A selection is a way to transfer text or other data between programs
1545 via special system buffers called `selection' or `clipboard'.
1546 DISPLAY can be a display name, a frame, or nil (meaning the selected
1547 frame's display)."
1548 (let ((frame-type (framep-on-display display)))
1549 (cond
1550 ((eq frame-type 'pc)
1551 ;; MS-DOS frames support selections when Emacs runs inside
1552 ;; a Windows DOS Box.
1553 (with-no-warnings
1554 (not (null dos-windows-version))))
1555 ((memq frame-type '(x w32 ns))
1556 t)
1557 (t
1558 nil))))
1559
1560 (declare-function x-display-screens "xfns.c" (&optional terminal))
1561
1562 (defun display-screens (&optional display)
1563 "Return the number of screens associated with DISPLAY.
1564 DISPLAY should be either a frame or a display name (a string).
1565 If DISPLAY is omitted or nil, it defaults to the selected frame's display."
1566 (let ((frame-type (framep-on-display display)))
1567 (cond
1568 ((memq frame-type '(x w32 ns))
1569 (x-display-screens display))
1570 (t
1571 1))))
1572
1573 (declare-function x-display-pixel-height "xfns.c" (&optional terminal))
1574
1575 (defun display-pixel-height (&optional display)
1576 "Return the height of DISPLAY's screen in pixels.
1577 DISPLAY can be a display name or a frame.
1578 If DISPLAY is omitted or nil, it defaults to the selected frame's display.
1579
1580 For character terminals, each character counts as a single pixel.
1581
1582 For graphical terminals, note that on \"multi-monitor\" setups this
1583 refers to the pixel height for all physical monitors associated
1584 with DISPLAY. To get information for each physical monitor, use
1585 `display-monitor-attributes-list'."
1586 (let ((frame-type (framep-on-display display)))
1587 (cond
1588 ((memq frame-type '(x w32 ns))
1589 (x-display-pixel-height display))
1590 (t
1591 (frame-height (if (framep display) display (selected-frame)))))))
1592
1593 (declare-function x-display-pixel-width "xfns.c" (&optional terminal))
1594
1595 (defun display-pixel-width (&optional display)
1596 "Return the width of DISPLAY's screen in pixels.
1597 DISPLAY can be a display name or a frame.
1598 If DISPLAY is omitted or nil, it defaults to the selected frame's display.
1599
1600 For character terminals, each character counts as a single pixel.
1601
1602 For graphical terminals, note that on \"multi-monitor\" setups this
1603 refers to the pixel width for all physical monitors associated
1604 with DISPLAY. To get information for each physical monitor, use
1605 `display-monitor-attributes-list'."
1606 (let ((frame-type (framep-on-display display)))
1607 (cond
1608 ((memq frame-type '(x w32 ns))
1609 (x-display-pixel-width display))
1610 (t
1611 (frame-width (if (framep display) display (selected-frame)))))))
1612
1613 (defcustom display-mm-dimensions-alist nil
1614 "Alist for specifying screen dimensions in millimeters.
1615 The functions `display-mm-height' and `display-mm-width' consult
1616 this list before asking the system.
1617
1618 Each element has the form (DISPLAY . (WIDTH . HEIGHT)), e.g.
1619 \(\":0.0\" . (287 . 215)).
1620
1621 If `display' is t, it specifies dimensions for all graphical displays
1622 not explicitly specified."
1623 :version "22.1"
1624 :type '(alist :key-type (choice (string :tag "Display name")
1625 (const :tag "Default" t))
1626 :value-type (cons :tag "Dimensions"
1627 (integer :tag "Width")
1628 (integer :tag "Height")))
1629 :group 'frames)
1630
1631 (declare-function x-display-mm-height "xfns.c" (&optional terminal))
1632
1633 (defun display-mm-height (&optional display)
1634 "Return the height of DISPLAY's screen in millimeters.
1635 If the information is unavailable, this function returns nil.
1636 DISPLAY can be a display name or a frame.
1637 If DISPLAY is omitted or nil, it defaults to the selected frame's display.
1638
1639 You can override what the system thinks the result should be by
1640 adding an entry to `display-mm-dimensions-alist'.
1641
1642 For graphical terminals, note that on \"multi-monitor\" setups this
1643 refers to the height in millimeters for all physical monitors
1644 associated with DISPLAY. To get information for each physical
1645 monitor, use `display-monitor-attributes-list'."
1646 (and (memq (framep-on-display display) '(x w32 ns))
1647 (or (cddr (assoc (or display (frame-parameter nil 'display))
1648 display-mm-dimensions-alist))
1649 (cddr (assoc t display-mm-dimensions-alist))
1650 (x-display-mm-height display))))
1651
1652 (declare-function x-display-mm-width "xfns.c" (&optional terminal))
1653
1654 (defun display-mm-width (&optional display)
1655 "Return the width of DISPLAY's screen in millimeters.
1656 If the information is unavailable, this function returns nil.
1657 DISPLAY can be a display name or a frame.
1658 If DISPLAY is omitted or nil, it defaults to the selected frame's display.
1659
1660 You can override what the system thinks the result should be by
1661 adding an entry to `display-mm-dimensions-alist'.
1662
1663 For graphical terminals, note that on \"multi-monitor\" setups this
1664 refers to the width in millimeters for all physical monitors
1665 associated with DISPLAY. To get information for each physical
1666 monitor, use `display-monitor-attributes-list'."
1667 (and (memq (framep-on-display display) '(x w32 ns))
1668 (or (cadr (assoc (or display (frame-parameter nil 'display))
1669 display-mm-dimensions-alist))
1670 (cadr (assoc t display-mm-dimensions-alist))
1671 (x-display-mm-width display))))
1672
1673 (declare-function x-display-backing-store "xfns.c" (&optional terminal))
1674
1675 ;; In NS port, the return value may be `buffered', `retained', or
1676 ;; `non-retained'. See src/nsfns.m.
1677 (defun display-backing-store (&optional display)
1678 "Return the backing store capability of DISPLAY's screen.
1679 The value may be `always', `when-mapped', `not-useful', or nil if
1680 the question is inapplicable to a certain kind of display.
1681 DISPLAY can be a display name or a frame.
1682 If DISPLAY is omitted or nil, it defaults to the selected frame's display."
1683 (let ((frame-type (framep-on-display display)))
1684 (cond
1685 ((memq frame-type '(x w32 ns))
1686 (x-display-backing-store display))
1687 (t
1688 'not-useful))))
1689
1690 (declare-function x-display-save-under "xfns.c" (&optional terminal))
1691
1692 (defun display-save-under (&optional display)
1693 "Return non-nil if DISPLAY's screen supports the SaveUnder feature.
1694 DISPLAY can be a display name or a frame.
1695 If DISPLAY is omitted or nil, it defaults to the selected frame's display."
1696 (let ((frame-type (framep-on-display display)))
1697 (cond
1698 ((memq frame-type '(x w32 ns))
1699 (x-display-save-under display))
1700 (t
1701 'not-useful))))
1702
1703 (declare-function x-display-planes "xfns.c" (&optional terminal))
1704
1705 (defun display-planes (&optional display)
1706 "Return the number of planes supported by DISPLAY.
1707 DISPLAY can be a display name or a frame.
1708 If DISPLAY is omitted or nil, it defaults to the selected frame's display."
1709 (let ((frame-type (framep-on-display display)))
1710 (cond
1711 ((memq frame-type '(x w32 ns))
1712 (x-display-planes display))
1713 ((eq frame-type 'pc)
1714 4)
1715 (t
1716 (truncate (log (length (tty-color-alist)) 2))))))
1717
1718 (declare-function x-display-color-cells "xfns.c" (&optional terminal))
1719
1720 (defun display-color-cells (&optional display)
1721 "Return the number of color cells supported by DISPLAY.
1722 DISPLAY can be a display name or a frame.
1723 If DISPLAY is omitted or nil, it defaults to the selected frame's display."
1724 (let ((frame-type (framep-on-display display)))
1725 (cond
1726 ((memq frame-type '(x w32 ns))
1727 (x-display-color-cells display))
1728 ((eq frame-type 'pc)
1729 16)
1730 (t
1731 (tty-display-color-cells display)))))
1732
1733 (declare-function x-display-visual-class "xfns.c" (&optional terminal))
1734
1735 (defun display-visual-class (&optional display)
1736 "Return the visual class of DISPLAY.
1737 The value is one of the symbols `static-gray', `gray-scale',
1738 `static-color', `pseudo-color', `true-color', or `direct-color'.
1739 DISPLAY can be a display name or a frame.
1740 If DISPLAY is omitted or nil, it defaults to the selected frame's display."
1741 (let ((frame-type (framep-on-display display)))
1742 (cond
1743 ((memq frame-type '(x w32 ns))
1744 (x-display-visual-class display))
1745 ((and (memq frame-type '(pc t))
1746 (tty-display-color-p display))
1747 'static-color)
1748 (t
1749 'static-gray))))
1750
1751 (declare-function x-display-monitor-attributes-list "xfns.c"
1752 (&optional terminal))
1753 (declare-function w32-display-monitor-attributes-list "w32fns.c"
1754 (&optional display))
1755 (declare-function ns-display-monitor-attributes-list "nsfns.m"
1756 (&optional terminal))
1757
1758 (defun display-monitor-attributes-list (&optional display)
1759 "Return a list of physical monitor attributes on DISPLAY.
1760 DISPLAY can be a display name, a terminal name, or a frame.
1761 If DISPLAY is omitted or nil, it defaults to the selected frame's display.
1762 Each element of the list represents the attributes of a physical
1763 monitor. The first element corresponds to the primary monitor.
1764
1765 The attributes for a physical monitor are represented as an alist
1766 of attribute keys and values as follows:
1767
1768 geometry -- Position and size in pixels in the form of (X Y WIDTH HEIGHT)
1769 workarea -- Position and size of the work area in pixels in the
1770 form of (X Y WIDTH HEIGHT)
1771 mm-size -- Width and height in millimeters in the form of
1772 (WIDTH HEIGHT)
1773 frames -- List of frames dominated by the physical monitor
1774 name (*) -- Name of the physical monitor as a string
1775 source (*) -- Source of multi-monitor information as a string
1776
1777 where X, Y, WIDTH, and HEIGHT are integers. X and Y are coordinates
1778 of the top-left corner, and might be negative for monitors other than
1779 the primary one. Keys labeled with (*) are optional.
1780
1781 The \"work area\" is a measure of the \"usable\" display space.
1782 It may be less than the total screen size, owing to space taken up
1783 by window manager features (docks, taskbars, etc.). The precise
1784 details depend on the platform and environment.
1785
1786 The `source' attribute describes the source from which the information
1787 was obtained. On X, this may be one of: \"Gdk\", \"XRandr\", \"Xinerama\",
1788 or \"fallback\".
1789
1790 A frame is dominated by a physical monitor when either the
1791 largest area of the frame resides in the monitor, or the monitor
1792 is the closest to the frame if the frame does not intersect any
1793 physical monitors. Every (non-tooltip) frame (including invisible ones)
1794 in a graphical display is dominated by exactly one physical
1795 monitor at a time, though it can span multiple (or no) physical
1796 monitors."
1797 (let ((frame-type (framep-on-display display)))
1798 (cond
1799 ((eq frame-type 'x)
1800 (x-display-monitor-attributes-list display))
1801 ((eq frame-type 'w32)
1802 (w32-display-monitor-attributes-list display))
1803 ((eq frame-type 'ns)
1804 (ns-display-monitor-attributes-list display))
1805 (t
1806 (let ((geometry (list 0 0 (display-pixel-width display)
1807 (display-pixel-height display))))
1808 `(((geometry . ,geometry)
1809 (workarea . ,geometry)
1810 (mm-size . (,(display-mm-width display)
1811 ,(display-mm-height display)))
1812 (frames . ,(frames-on-display-list display)))))))))
1813
1814 \f
1815 ;;;; Frame geometry values
1816
1817 (defun frame-geom-value-cons (type value &optional frame)
1818 "Return equivalent geometry value for FRAME as a cons with car `+'.
1819 A geometry value equivalent to VALUE for FRAME is returned,
1820 where the value is a cons with car `+', not numeric.
1821 TYPE is the car of the original geometry spec (TYPE . VALUE).
1822 It is `top' or `left', depending on which edge VALUE is related to.
1823 VALUE is the cdr of a frame geometry spec: (left/top . VALUE).
1824 If VALUE is a number, then it is converted to a cons value, perhaps
1825 relative to the opposite frame edge from that in the original spec.
1826 FRAME defaults to the selected frame.
1827
1828 Examples (measures in pixels) -
1829 Assuming display height/width=1024, frame height/width=600:
1830 300 inside display edge: 300 => (+ 300)
1831 (+ 300) => (+ 300)
1832 300 inside opposite display edge: (- 300) => (+ 124)
1833 -300 => (+ 124)
1834 300 beyond display edge
1835 (= 724 inside opposite display edge): (+ -300) => (+ -300)
1836 300 beyond display edge
1837 (= 724 inside opposite display edge): (- -300) => (+ 724)
1838
1839 In the 3rd, 4th, and 6th examples, the returned value is relative to
1840 the opposite frame edge from the edge indicated in the input spec."
1841 (cond ((and (consp value) (eq '+ (car value))) ; e.g. (+ 300), (+ -300)
1842 value)
1843 ((natnump value) (list '+ value)) ; e.g. 300 => (+ 300)
1844 (t ; e.g. -300, (- 300), (- -300)
1845 (list '+ (- (if (eq 'left type) ; => (+ 124), (+ 124), (+ 724)
1846 (x-display-pixel-width)
1847 (x-display-pixel-height))
1848 (if (integerp value) (- value) (cadr value))
1849 (if (eq 'left type)
1850 (frame-pixel-width frame)
1851 (frame-pixel-height frame)))))))
1852
1853 (defun frame-geom-spec-cons (spec &optional frame)
1854 "Return equivalent geometry spec for FRAME as a cons with car `+'.
1855 A geometry specification equivalent to SPEC for FRAME is returned,
1856 where the value is a cons with car `+', not numeric.
1857 SPEC is a frame geometry spec: (left . VALUE) or (top . VALUE).
1858 If VALUE is a number, then it is converted to a cons value, perhaps
1859 relative to the opposite frame edge from that in the original spec.
1860 FRAME defaults to the selected frame.
1861
1862 Examples (measures in pixels) -
1863 Assuming display height=1024, frame height=600:
1864 top 300 below display top: (top . 300) => (top + 300)
1865 (top + 300) => (top + 300)
1866 bottom 300 above display bottom: (top - 300) => (top + 124)
1867 (top . -300) => (top + 124)
1868 top 300 above display top
1869 (= bottom 724 above display bottom): (top + -300) => (top + -300)
1870 bottom 300 below display bottom
1871 (= top 724 below display top): (top - -300) => (top + 724)
1872
1873 In the 3rd, 4th, and 6th examples, the returned value is relative to
1874 the opposite frame edge from the edge indicated in the input spec."
1875 (cons (car spec) (frame-geom-value-cons (car spec) (cdr spec) frame)))
1876 \f
1877
1878 (defun delete-other-frames (&optional frame)
1879 "Delete all frames on the current terminal, except FRAME.
1880 If FRAME uses another frame's minibuffer, the minibuffer frame is
1881 left untouched. FRAME nil or omitted means use the selected frame."
1882 (interactive)
1883 (unless frame
1884 (setq frame (selected-frame)))
1885 (let* ((mini-frame (window-frame (minibuffer-window frame)))
1886 (frames (delq mini-frame (delq frame (frame-list)))))
1887 ;; Only consider frames on the same terminal.
1888 (dolist (frame (prog1 frames (setq frames nil)))
1889 (if (eq (frame-terminal) (frame-terminal frame))
1890 (push frame frames)))
1891 ;; Delete mon-minibuffer-only frames first, because `delete-frame'
1892 ;; signals an error when trying to delete a mini-frame that's
1893 ;; still in use by another frame.
1894 (dolist (frame frames)
1895 (unless (eq (frame-parameter frame 'minibuffer) 'only)
1896 (delete-frame frame)))
1897 ;; Delete minibuffer-only frames.
1898 (dolist (frame frames)
1899 (when (eq (frame-parameter frame 'minibuffer) 'only)
1900 (delete-frame frame)))))
1901
1902 ;; miscellaneous obsolescence declarations
1903 (define-obsolete-variable-alias 'delete-frame-hook
1904 'delete-frame-functions "22.1")
1905
1906 \f
1907 ;;; Window dividers.
1908 (defgroup window-divider nil
1909 "Window dividers."
1910 :version "25.1"
1911 :group 'frames
1912 :group 'windows)
1913
1914 (defcustom window-divider-default-places 'right-only
1915 "Default positions of window dividers.
1916 Possible values are `bottom-only' (dividers on the bottom of each
1917 window only), `right-only' (dividers on the right of each window
1918 only), and t (dividers on the bottom and on the right of each
1919 window). The default is `right-only'.
1920
1921 The value takes effect if and only if dividers are enabled by
1922 `window-divider-mode'.
1923
1924 To position dividers on frames individually, use the frame
1925 parameters `bottom-divider-width' and `right-divider-width'."
1926 :type '(choice (const :tag "Bottom only" bottom-only)
1927 (const :tag "Right only" right-only)
1928 (const :tag "Bottom and right" t))
1929 :initialize 'custom-initialize-default
1930 :set (lambda (symbol value)
1931 (set-default symbol value)
1932 (when window-divider-mode
1933 (window-divider-mode-apply t)))
1934 :version "25.1")
1935
1936 (defun window-divider-width-valid-p (value)
1937 "Return non-nil if VALUE is a positive number."
1938 (and (numberp value) (> value 0)))
1939
1940 (defcustom window-divider-default-bottom-width 6
1941 "Default width of dividers on bottom of windows.
1942 The value must be a positive integer and takes effect when bottom
1943 dividers are displayed by `window-divider-mode'.
1944
1945 To adjust bottom dividers for frames individually, use the frame
1946 parameter `bottom-divider-width'."
1947 :type '(restricted-sexp
1948 :tag "Default width of bottom dividers"
1949 :match-alternatives (frame-window-divider-width-valid-p))
1950 :initialize 'custom-initialize-default
1951 :set (lambda (symbol value)
1952 (set-default symbol value)
1953 (when window-divider-mode
1954 (window-divider-mode-apply t)))
1955 :version "25.1")
1956
1957 (defcustom window-divider-default-right-width 6
1958 "Default width of dividers on the right of windows.
1959 The value must be a positive integer and takes effect when right
1960 dividers are displayed by `window-divider-mode'.
1961
1962 To adjust right dividers for frames individually, use the frame
1963 parameter `right-divider-width'."
1964 :type '(restricted-sexp
1965 :tag "Default width of right dividers"
1966 :match-alternatives (frame-window-divider-width-valid-p))
1967 :initialize 'custom-initialize-default
1968 :set (lambda (symbol value)
1969 (set-default symbol value)
1970 (when window-divider-mode
1971 (window-divider-mode-apply t)))
1972 :version "25.1")
1973
1974 (defun window-divider-mode-apply (enable)
1975 "Apply window divider places and widths to all frames.
1976 If ENABLE is nil, apply default places and widths. Else reset
1977 all divider widths to zero."
1978 (let ((bottom (if (and enable
1979 (memq window-divider-default-places
1980 '(bottom-only t)))
1981 window-divider-default-bottom-width
1982 0))
1983 (right (if (and enable
1984 (memq window-divider-default-places
1985 '(right-only t)))
1986 window-divider-default-right-width
1987 0)))
1988 (modify-all-frames-parameters
1989 (list (cons 'bottom-divider-width bottom)
1990 (cons 'right-divider-width right)))
1991 (setq default-frame-alist
1992 (assq-delete-all
1993 'bottom-divider-width default-frame-alist))
1994 (setq default-frame-alist
1995 (assq-delete-all
1996 'right-divider-width default-frame-alist))
1997 (when (> bottom 0)
1998 (setq default-frame-alist
1999 (cons
2000 (cons 'bottom-divider-width bottom)
2001 default-frame-alist)))
2002 (when (> right 0)
2003 (setq default-frame-alist
2004 (cons
2005 (cons 'right-divider-width right)
2006 default-frame-alist)))))
2007
2008 (define-minor-mode window-divider-mode
2009 "Display dividers between windows (Window Divider mode).
2010 With a prefix argument ARG, enable Window Divider mode if ARG is
2011 positive, and disable it otherwise. If called from Lisp, enable
2012 the mode if ARG is omitted or nil.
2013
2014 The option `window-divider-default-places' specifies on which
2015 side of a window dividers are displayed. The options
2016 `window-divider-default-bottom-width' and
2017 `window-divider-default-right-width' specify their respective
2018 widths."
2019 :group 'window-divider
2020 :global t
2021 (window-divider-mode-apply window-divider-mode))
2022 \f
2023 ;; Blinking cursor
2024
2025 (defgroup cursor nil
2026 "Displaying text cursors."
2027 :version "21.1"
2028 :group 'frames)
2029
2030 (defcustom blink-cursor-delay 0.5
2031 "Seconds of idle time after which cursor starts to blink."
2032 :type 'number
2033 :group 'cursor)
2034
2035 (defcustom blink-cursor-interval 0.5
2036 "Length of cursor blink interval in seconds."
2037 :type 'number
2038 :group 'cursor)
2039
2040 (defcustom blink-cursor-blinks 10
2041 "How many times to blink before using a solid cursor on NS, X, and MS-Windows.
2042 Use 0 or negative value to blink forever."
2043 :version "24.4"
2044 :type 'integer
2045 :group 'cursor)
2046
2047 (defvar blink-cursor-blinks-done 1
2048 "Number of blinks done since we started blinking on NS, X, and MS-Windows.")
2049
2050 (defvar blink-cursor-idle-timer nil
2051 "Timer started after `blink-cursor-delay' seconds of Emacs idle time.
2052 The function `blink-cursor-start' is called when the timer fires.")
2053
2054 (defvar blink-cursor-timer nil
2055 "Timer started from `blink-cursor-start'.
2056 This timer calls `blink-cursor-timer-function' every
2057 `blink-cursor-interval' seconds.")
2058
2059 (defun blink-cursor-start ()
2060 "Timer function called from the timer `blink-cursor-idle-timer'.
2061 This starts the timer `blink-cursor-timer', which makes the cursor blink
2062 if appropriate. It also arranges to cancel that timer when the next
2063 command starts, by installing a pre-command hook."
2064 (when (null blink-cursor-timer)
2065 ;; Set up the timer first, so that if this signals an error,
2066 ;; blink-cursor-end is not added to pre-command-hook.
2067 (setq blink-cursor-blinks-done 1)
2068 (setq blink-cursor-timer
2069 (run-with-timer blink-cursor-interval blink-cursor-interval
2070 'blink-cursor-timer-function))
2071 (add-hook 'pre-command-hook 'blink-cursor-end)
2072 (internal-show-cursor nil nil)))
2073
2074 (defun blink-cursor-timer-function ()
2075 "Timer function of timer `blink-cursor-timer'."
2076 (internal-show-cursor nil (not (internal-show-cursor-p)))
2077 ;; Suspend counting blinks when the w32 menu-bar menu is displayed,
2078 ;; since otherwise menu tooltips will behave erratically.
2079 (or (and (fboundp 'w32--menu-bar-in-use)
2080 (w32--menu-bar-in-use))
2081 (setq blink-cursor-blinks-done (1+ blink-cursor-blinks-done)))
2082 ;; Each blink is two calls to this function.
2083 (when (and (> blink-cursor-blinks 0)
2084 (<= (* 2 blink-cursor-blinks) blink-cursor-blinks-done))
2085 (blink-cursor-suspend)
2086 (add-hook 'post-command-hook 'blink-cursor-check)))
2087
2088
2089 (defun blink-cursor-end ()
2090 "Stop cursor blinking.
2091 This is installed as a pre-command hook by `blink-cursor-start'.
2092 When run, it cancels the timer `blink-cursor-timer' and removes
2093 itself as a pre-command hook."
2094 (remove-hook 'pre-command-hook 'blink-cursor-end)
2095 (internal-show-cursor nil t)
2096 (when blink-cursor-timer
2097 (cancel-timer blink-cursor-timer)
2098 (setq blink-cursor-timer nil)))
2099
2100 (defun blink-cursor-suspend ()
2101 "Suspend cursor blinking.
2102 This is called when no frame has focus and timers can be suspended.
2103 Timers are restarted by `blink-cursor-check', which is called when a
2104 frame receives focus."
2105 (blink-cursor-end)
2106 (when blink-cursor-idle-timer
2107 (cancel-timer blink-cursor-idle-timer)
2108 (setq blink-cursor-idle-timer nil)))
2109
2110 (defun blink-cursor-check ()
2111 "Check if cursor blinking shall be restarted.
2112 This is done when a frame gets focus. Blink timers may be stopped by
2113 `blink-cursor-suspend'."
2114 (when (and blink-cursor-mode
2115 (not blink-cursor-idle-timer))
2116 (remove-hook 'post-command-hook 'blink-cursor-check)
2117 (setq blink-cursor-idle-timer
2118 (run-with-idle-timer blink-cursor-delay
2119 blink-cursor-delay
2120 'blink-cursor-start))))
2121
2122 (define-obsolete-variable-alias 'blink-cursor 'blink-cursor-mode "22.1")
2123
2124 (define-minor-mode blink-cursor-mode
2125 "Toggle cursor blinking (Blink Cursor mode).
2126 With a prefix argument ARG, enable Blink Cursor mode if ARG is
2127 positive, and disable it otherwise. If called from Lisp, enable
2128 the mode if ARG is omitted or nil.
2129
2130 If the value of `blink-cursor-blinks' is positive (10 by default),
2131 the cursor stops blinking after that number of blinks, if Emacs
2132 gets no input during that time.
2133
2134 See also `blink-cursor-interval' and `blink-cursor-delay'.
2135
2136 This command is effective only on graphical frames. On text-only
2137 terminals, cursor blinking is controlled by the terminal."
2138 :init-value (not (or noninteractive
2139 no-blinking-cursor
2140 (eq system-type 'ms-dos)
2141 (not (memq window-system '(x w32 ns)))))
2142 :initialize 'custom-initialize-delay
2143 :group 'cursor
2144 :global t
2145 (blink-cursor-suspend)
2146 (remove-hook 'focus-in-hook #'blink-cursor-check)
2147 (remove-hook 'focus-out-hook #'blink-cursor-suspend)
2148 (when blink-cursor-mode
2149 (add-hook 'focus-in-hook #'blink-cursor-check)
2150 (add-hook 'focus-out-hook #'blink-cursor-suspend)
2151 (setq blink-cursor-idle-timer
2152 (run-with-idle-timer blink-cursor-delay
2153 blink-cursor-delay
2154 #'blink-cursor-start))))
2155
2156 \f
2157 ;; Frame maximization/fullscreen
2158
2159 (defun toggle-frame-maximized ()
2160 "Toggle maximization state of selected frame.
2161 Maximize selected frame or un-maximize if it is already maximized.
2162
2163 If the frame is in fullscreen state, don't change its state, but
2164 set the frame's `fullscreen-restore' parameter to `maximized', so
2165 the frame will be maximized after disabling fullscreen state.
2166
2167 Note that with some window managers you may have to set
2168 `frame-resize-pixelwise' to non-nil in order to make a frame
2169 appear truly maximized. In addition, you may have to set
2170 `x-frame-normalize-before-maximize' in order to enable
2171 transitions from one fullscreen state to another.
2172
2173 See also `toggle-frame-fullscreen'."
2174 (interactive)
2175 (let ((fullscreen (frame-parameter nil 'fullscreen)))
2176 (cond
2177 ((memq fullscreen '(fullscreen fullboth))
2178 (set-frame-parameter nil 'fullscreen-restore 'maximized))
2179 ((eq fullscreen 'maximized)
2180 (set-frame-parameter nil 'fullscreen nil))
2181 (t
2182 (set-frame-parameter nil 'fullscreen 'maximized)))))
2183
2184 (defun toggle-frame-fullscreen ()
2185 "Toggle fullscreen state of selected frame.
2186 Make selected frame fullscreen or restore its previous size if it
2187 is already fullscreen.
2188
2189 Before making the frame fullscreen remember the current value of
2190 the frame's `fullscreen' parameter in the `fullscreen-restore'
2191 parameter of the frame. That value is used to restore the
2192 frame's fullscreen state when toggling fullscreen the next time.
2193
2194 Note that with some window managers you may have to set
2195 `frame-resize-pixelwise' to non-nil in order to make a frame
2196 appear truly fullscreen. In addition, you may have to set
2197 `x-frame-normalize-before-maximize' in order to enable
2198 transitions from one fullscreen state to another.
2199
2200 See also `toggle-frame-maximized'."
2201 (interactive)
2202 (let ((fullscreen (frame-parameter nil 'fullscreen)))
2203 (if (memq fullscreen '(fullscreen fullboth))
2204 (let ((fullscreen-restore (frame-parameter nil 'fullscreen-restore)))
2205 (if (memq fullscreen-restore '(maximized fullheight fullwidth))
2206 (set-frame-parameter nil 'fullscreen fullscreen-restore)
2207 (set-frame-parameter nil 'fullscreen nil)))
2208 (modify-frame-parameters
2209 nil `((fullscreen . fullboth) (fullscreen-restore . ,fullscreen))))))
2210 \f
2211 ;;;; Key bindings
2212
2213 (define-key ctl-x-5-map "2" 'make-frame-command)
2214 (define-key ctl-x-5-map "1" 'delete-other-frames)
2215 (define-key ctl-x-5-map "0" 'delete-frame)
2216 (define-key ctl-x-5-map "o" 'other-frame)
2217 (define-key global-map [f11] 'toggle-frame-fullscreen)
2218 (define-key global-map [(meta f10)] 'toggle-frame-maximized)
2219 (define-key esc-map [f10] 'toggle-frame-maximized)
2220
2221 \f
2222 ;; Misc.
2223
2224 ;; Only marked as obsolete in 24.3.
2225 (define-obsolete-variable-alias 'automatic-hscrolling
2226 'auto-hscroll-mode "22.1")
2227
2228 (make-variable-buffer-local 'show-trailing-whitespace)
2229
2230 ;; Defined in dispnew.c.
2231 (make-obsolete-variable
2232 'window-system-version "it does not give useful information." "24.3")
2233
2234 ;; Variables which should trigger redisplay of the current buffer.
2235 (setq redisplay--variables (make-hash-table :test 'eq :size 10))
2236 (mapc (lambda (var)
2237 (puthash var 1 redisplay--variables))
2238 '(line-spacing
2239 overline-margin
2240 line-prefix
2241 wrap-prefix
2242 truncate-lines
2243 bidi-paragraph-direction
2244 bidi-display-reordering))
2245
2246 (provide 'frame)
2247
2248 ;;; frame.el ends here