Output alists with dotted pair notation in .dir-locals.el
[emacs.git] / lisp / frame.el
blob56b8c5487c8842c51a08a55c6b1d77d40788014e
1 ;;; frame.el --- multi-frame management independent of window systems -*- lexical-binding:t -*-
3 ;; Copyright (C) 1993-1994, 1996-1997, 2000-2018 Free Software
4 ;; Foundation, Inc.
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: internal
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
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.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;;; Code:
28 (eval-when-compile (require 'cl-lib))
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.")
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))))
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))
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'.")
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.")
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,
70 (setq initial-frame-alist
71 \\='((top . 1) (left . 1) (width . 80) (height . 55)))
73 Parameters specified here supersede the values given in
74 `default-frame-alist'.
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'.
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)
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.
103 You can set this in your init file; for example,
105 (setq minibuffer-frame-alist
106 \\='((top . 1) (left . 1) (width . 80) (height . 2)))
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)
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 (if (catch 'other-frame
120 (dolist (frame-1 (frame-list))
121 ;; A valid "other" frame is visible, has its `delete-before'
122 ;; parameter unset and is not a child frame.
123 (when (and (not (eq frame-1 frame))
124 (frame-visible-p frame-1)
125 (not (frame-parent frame-1))
126 (not (frame-parameter frame-1 'delete-before)))
127 (throw 'other-frame t))))
128 (delete-frame frame t)
129 ;; Gildea@x.org says it is ok to ask questions before terminating.
130 (save-buffers-kill-emacs))))
132 (defun frame-focus-state (&optional frame)
133 "Return FRAME's last known focus state.
134 If nil or omitted, FRAME defaults to the selected frame.
136 Return nil if the frame is definitely known not be focused, t if
137 the frame is known to be focused, and `unknown' if we don't know."
138 (let* ((frame (or frame (selected-frame)))
139 (tty-top-frame (tty-top-frame frame)))
140 (if (not tty-top-frame)
141 (frame-parameter frame 'last-focus-update)
142 ;; All tty frames are frame-visible-p if the terminal is
143 ;; visible, so check whether the frame is the top tty frame
144 ;; before checking visibility.
145 (cond ((not (eq tty-top-frame frame)) nil)
146 ((not (frame-visible-p frame)) nil)
147 (t (let ((tty-focus-state
148 (terminal-parameter frame 'tty-focus-state)))
149 (cond ((eq tty-focus-state 'focused) t)
150 ((eq tty-focus-state 'defocused) nil)
151 (t 'unknown))))))))
153 (defvar after-focus-change-function #'ignore
154 "Function called after frame focus may have changed.
156 This function is called with no arguments when Emacs notices that
157 the set of focused frames may have changed. Code wanting to do
158 something when frame focus changes should use `add-function' to
159 add a function to this one, and in this added function, re-scan
160 the set of focused frames, calling `frame-focus-state' to
161 retrieve the last known focus state of each frame. Focus events
162 are delivered asynchronously, and frame input focus according to
163 an external system may not correspond to the notion of the Emacs
164 selected frame. Multiple frames may appear to have input focus
165 simultaneously due to focus event delivery differences, the
166 presence of multiple Emacs terminals, and other factors, and code
167 should be robust in the face of this situation.
169 Depending on window system, focus events may also be delivered
170 repeatedly and with different focus states before settling to the
171 expected values. Code relying on focus notifications should
172 \"debounce\" any user-visible updates arising from focus changes,
173 perhaps by deferring work until redisplay.
175 This function may be called in arbitrary contexts, including from
176 inside `read-event', so take the same care as you might when
177 writing a process filter.")
179 (defvar focus-in-hook nil
180 "Normal hook run when a frame gains focus.
181 The frame gaining focus is selected at the time this hook is run.
183 This hook is obsolete. Despite its name, this hook may be run in
184 situations other than when a frame obtains input focus: for
185 example, we also run this hook when switching the selected frame
186 internally to handle certain input events (like mouse wheel
187 scrolling) even when the user's notion of input focus
188 hasn't changed.
190 Prefer using `after-focus-change-function'.")
191 (make-obsolete-variable
192 'focus-in-hook "after-focus-change-function" "27.1" 'set)
194 (defvar focus-out-hook nil
195 "Normal hook run when all frames lost input focus.
197 This hook is obsolete; see `focus-in-hook'. Depending on timing,
198 this hook may be delivered when a frame does in fact have focus.
199 Prefer `after-focus-change-function'.")
200 (make-obsolete-variable
201 'focus-out-hook "after-focus-change-function" "27.1" 'set)
203 (defun handle-focus-in (event)
204 "Handle a focus-in event.
205 Focus-in events are bound to this function; do not change this
206 binding. Focus-in events occur when a frame receives focus from
207 the window system."
208 ;; N.B. tty focus goes down a different path; see xterm.el.
209 (interactive "e")
210 (unless (eq (car-safe event) 'focus-in)
211 (error "handle-focus-in should handle focus-in events"))
212 (let ((frame (nth 1 event)))
213 (when (frame-live-p frame)
214 (internal-handle-focus-in event)
215 (setf (frame-parameter frame 'last-focus-update) t)
216 (run-hooks 'focus-in-hook)))
217 (funcall after-focus-change-function))
219 (defun handle-focus-out (event)
220 "Handle a focus-out event.
221 Focus-out events are bound to this function; do not change this
222 binding. Focus-out events occur when a frame loses focus, but
223 that's not the whole story: see `after-focus-change-function'."
224 ;; N.B. tty focus goes down a different path; see xterm.el.
225 (interactive "e")
226 (unless (eq (car event) 'focus-out)
227 (error "handle-focus-out should handle focus-out events"))
228 (let ((frame (nth 1 event)))
229 (when (frame-live-p frame)
230 (setf (frame-parameter frame 'last-focus-update) nil)
231 (run-hooks 'focus-out-hook)))
232 (funcall after-focus-change-function))
234 (defun handle-move-frame (event)
235 "Handle a move-frame event.
236 This function runs the abnormal hook `move-frame-functions'."
237 (interactive "e")
238 (let ((frame (posn-window (event-start event))))
239 (run-hook-with-args 'move-frame-functions frame)))
241 ;;;; Arrangement of frames at startup
243 ;; 1) Load the window system startup file from the lisp library and read the
244 ;; high-priority arguments (-q and the like). The window system startup
245 ;; file should create any frames specified in the window system defaults.
247 ;; 2) If no frames have been opened, we open an initial text frame.
249 ;; 3) Once the init file is done, we apply any newly set parameters
250 ;; in initial-frame-alist to the frame.
252 ;; If we create the initial frame, this is it.
253 (defvar frame-initial-frame nil)
255 ;; Record the parameters used in frame-initialize to make the initial frame.
256 (defvar frame-initial-frame-alist)
258 (defvar frame-initial-geometry-arguments nil)
260 ;; startup.el calls this function before loading the user's init
261 ;; file - if there is no frame with a minibuffer open now, create
262 ;; one to display messages while loading the init file.
263 (defun frame-initialize ()
264 "Create an initial frame if necessary."
265 ;; Are we actually running under a window system at all?
266 (if (and initial-window-system
267 (not noninteractive)
268 (not (eq initial-window-system 'pc)))
269 (progn
270 ;; If there is no frame with a minibuffer besides the terminal
271 ;; frame, then we need to create the opening frame. Make sure
272 ;; it has a minibuffer, but let initial-frame-alist omit the
273 ;; minibuffer spec.
274 (or (delq terminal-frame (minibuffer-frame-list))
275 (progn
276 (setq frame-initial-frame-alist
277 (append initial-frame-alist default-frame-alist nil))
278 (setq frame-initial-frame-alist
279 (cons (cons 'window-system initial-window-system)
280 frame-initial-frame-alist))
281 (setq default-minibuffer-frame
282 (setq frame-initial-frame
283 (make-frame frame-initial-frame-alist)))
284 ;; Delete any specifications for window geometry parameters
285 ;; so that we won't reapply them in frame-notice-user-settings.
286 ;; It would be wrong to reapply them then,
287 ;; because that would override explicit user resizing.
288 (setq initial-frame-alist
289 (frame-remove-geometry-params initial-frame-alist))))
290 ;; Copy the environment of the Emacs process into the new frame.
291 (set-frame-parameter frame-initial-frame 'environment
292 (frame-parameter terminal-frame 'environment))
293 ;; At this point, we know that we have a frame open, so we
294 ;; can delete the terminal frame.
295 (delete-frame terminal-frame)
296 (setq terminal-frame nil))))
298 (defvar frame-notice-user-settings t
299 "Non-nil means function `frame-notice-user-settings' wasn't run yet.")
301 (declare-function tool-bar-mode "tool-bar" (&optional arg))
302 (declare-function tool-bar-height "xdisp.c" (&optional frame pixelwise))
304 (defalias 'tool-bar-lines-needed 'tool-bar-height)
306 ;; startup.el calls this function after loading the user's init
307 ;; file. Now default-frame-alist and initial-frame-alist contain
308 ;; information to which we must react; do what needs to be done.
309 (defun frame-notice-user-settings ()
310 "Act on user's init file settings of frame parameters.
311 React to settings of `initial-frame-alist',
312 `window-system-default-frame-alist' and `default-frame-alist'
313 there (in decreasing order of priority)."
314 ;; Creating and deleting frames may shift the selected frame around,
315 ;; and thus the current buffer. Protect against that. We don't
316 ;; want to use save-excursion here, because that may also try to set
317 ;; the buffer of the selected window, which fails when the selected
318 ;; window is the minibuffer.
319 (let ((old-buffer (current-buffer))
320 (window-system-frame-alist
321 (cdr (assq initial-window-system
322 window-system-default-frame-alist))))
324 (when (and frame-notice-user-settings
325 (null frame-initial-frame))
326 ;; This case happens when we don't have a window system, and
327 ;; also for MS-DOS frames.
328 (let ((parms (frame-parameters)))
329 ;; Don't change the frame names.
330 (setq parms (delq (assq 'name parms) parms))
331 ;; Can't modify the minibuffer parameter, so don't try.
332 (setq parms (delq (assq 'minibuffer parms) parms))
333 (modify-frame-parameters
335 (if initial-window-system
336 parms
337 ;; initial-frame-alist and default-frame-alist were already
338 ;; applied in pc-win.el.
339 (append initial-frame-alist window-system-frame-alist
340 default-frame-alist parms nil)))
341 (if (null initial-window-system) ;; MS-DOS does this differently in pc-win.el
342 (let ((newparms (frame-parameters))
343 (frame (selected-frame)))
344 (tty-handle-reverse-video frame newparms)
345 ;; tty-handle-reverse-video might change the frame's
346 ;; color parameters, and we need to use the updated
347 ;; value below.
348 (setq newparms (frame-parameters))
349 ;; If we changed the background color, we need to update
350 ;; the background-mode parameter, and maybe some faces,
351 ;; too.
352 (when (assq 'background-color newparms)
353 (unless (or (assq 'background-mode initial-frame-alist)
354 (assq 'background-mode default-frame-alist))
355 (frame-set-background-mode frame))
356 (face-set-after-frame-default frame newparms))))))
358 ;; If the initial frame is still around, apply initial-frame-alist
359 ;; and default-frame-alist to it.
360 (when (frame-live-p frame-initial-frame)
361 ;; When tool-bar has been switched off, correct the frame size
362 ;; by the lines added in x-create-frame for the tool-bar and
363 ;; switch `tool-bar-mode' off.
364 (when (display-graphic-p)
365 (let* ((init-lines
366 (assq 'tool-bar-lines initial-frame-alist))
367 (other-lines
368 (or (assq 'tool-bar-lines window-system-frame-alist)
369 (assq 'tool-bar-lines default-frame-alist)))
370 (lines (or init-lines other-lines))
371 (height (tool-bar-height frame-initial-frame t)))
372 ;; Adjust frame top if either zero (nil) tool bar lines have
373 ;; been requested in the most relevant of the frame's alists
374 ;; or tool bar mode has been explicitly turned off in the
375 ;; user's init file.
376 (when (and (> height 0)
377 (or (and lines
378 (or (null (cdr lines))
379 (eq 0 (cdr lines))))
380 (not tool-bar-mode)))
381 (let* ((initial-top
382 (cdr (assq 'top frame-initial-geometry-arguments)))
383 (top (frame-parameter frame-initial-frame 'top)))
384 (when (and (consp initial-top) (eq '- (car initial-top)))
385 (let ((adjusted-top
386 (cond
387 ((and (consp top) (eq '+ (car top)))
388 (list '+ (+ (cadr top) height)))
389 ((and (consp top) (eq '- (car top)))
390 (list '- (- (cadr top) height)))
391 (t (+ top height)))))
392 (modify-frame-parameters
393 frame-initial-frame `((top . ,adjusted-top))))))
394 ;; Reset `tool-bar-mode' when zero tool bar lines have been
395 ;; requested for the window-system or default frame alists.
396 (when (and tool-bar-mode
397 (and other-lines
398 (or (null (cdr other-lines))
399 (eq 0 (cdr other-lines)))))
400 (tool-bar-mode -1)))))
402 ;; The initial frame we create above always has a minibuffer.
403 ;; If the user wants to remove it, or make it a minibuffer-only
404 ;; frame, then we'll have to delete the current frame and make a
405 ;; new one; you can't remove or add a root window to/from an
406 ;; existing frame.
408 ;; NOTE: default-frame-alist was nil when we created the
409 ;; existing frame. We need to explicitly include
410 ;; default-frame-alist in the parameters of the screen we
411 ;; create here, so that its new value, gleaned from the user's
412 ;; init file, will be applied to the existing screen.
413 (if (not (eq (cdr (or (assq 'minibuffer initial-frame-alist)
414 (assq 'minibuffer window-system-frame-alist)
415 (assq 'minibuffer default-frame-alist)
416 '(minibuffer . t)))
418 ;; Create the new frame.
419 (let (parms new)
420 ;; MS-Windows needs this to avoid inflooping below.
421 (if (eq system-type 'windows-nt)
422 (sit-for 0 t))
423 ;; If the frame isn't visible yet, wait till it is.
424 ;; If the user has to position the window,
425 ;; Emacs doesn't know its real position until
426 ;; the frame is seen to be visible.
427 (while (not (cdr (assq 'visibility
428 (frame-parameters frame-initial-frame))))
429 (sleep-for 1))
430 (setq parms (frame-parameters frame-initial-frame))
432 ;; Get rid of `name' unless it was specified explicitly before.
433 (or (assq 'name frame-initial-frame-alist)
434 (setq parms (delq (assq 'name parms) parms)))
435 ;; An explicit parent-id is a request to XEmbed the frame.
436 (or (assq 'parent-id frame-initial-frame-alist)
437 (setq parms (delq (assq 'parent-id parms) parms)))
439 (setq parms (append initial-frame-alist
440 window-system-frame-alist
441 default-frame-alist
442 parms
443 nil))
445 ;; Get rid of `reverse', because that was handled
446 ;; when we first made the frame.
447 (setq parms (cons '(reverse) (delq (assq 'reverse parms) parms)))
449 (if (assq 'height frame-initial-geometry-arguments)
450 (setq parms (assq-delete-all 'height parms)))
451 (if (assq 'width frame-initial-geometry-arguments)
452 (setq parms (assq-delete-all 'width parms)))
453 (if (assq 'left frame-initial-geometry-arguments)
454 (setq parms (assq-delete-all 'left parms)))
455 (if (assq 'top frame-initial-geometry-arguments)
456 (setq parms (assq-delete-all 'top parms)))
457 (setq new
458 (make-frame
459 ;; Use the geometry args that created the existing
460 ;; frame, rather than the parms we get for it.
461 (append frame-initial-geometry-arguments
462 '((user-size . t) (user-position . t))
463 parms)))
464 ;; The initial frame, which we are about to delete, may be
465 ;; the only frame with a minibuffer. If it is, create a
466 ;; new one.
467 (or (delq frame-initial-frame (minibuffer-frame-list))
468 (make-initial-minibuffer-frame nil))
470 ;; If the initial frame is serving as a surrogate
471 ;; minibuffer frame for any frames, we need to wean them
472 ;; onto a new frame. The default-minibuffer-frame
473 ;; variable must be handled similarly.
474 (let ((users-of-initial
475 (filtered-frame-list
476 (lambda (frame)
477 (and (not (eq frame frame-initial-frame))
478 (eq (window-frame
479 (minibuffer-window frame))
480 frame-initial-frame))))))
481 (if (or users-of-initial
482 (eq default-minibuffer-frame frame-initial-frame))
484 ;; Choose an appropriate frame. Prefer frames which
485 ;; are only minibuffers.
486 (let* ((new-surrogate
487 (car
488 (or (filtered-frame-list
489 (lambda (frame)
490 (eq (cdr (assq 'minibuffer
491 (frame-parameters frame)))
492 'only)))
493 (minibuffer-frame-list))))
494 (new-minibuffer (minibuffer-window new-surrogate)))
496 (if (eq default-minibuffer-frame frame-initial-frame)
497 (setq default-minibuffer-frame new-surrogate))
499 ;; Wean the frames using frame-initial-frame as
500 ;; their minibuffer frame.
501 (dolist (frame users-of-initial)
502 (modify-frame-parameters
503 frame (list (cons 'minibuffer new-minibuffer)))))))
505 ;; Redirect events enqueued at this frame to the new frame.
506 ;; Is this a good idea?
507 (redirect-frame-focus frame-initial-frame new)
509 ;; Finally, get rid of the old frame.
510 (delete-frame frame-initial-frame t))
512 ;; Otherwise, we don't need all that rigmarole; just apply
513 ;; the new parameters.
514 (let (newparms allparms tail)
515 (setq allparms (append initial-frame-alist
516 window-system-frame-alist
517 default-frame-alist nil))
518 (if (assq 'height frame-initial-geometry-arguments)
519 (setq allparms (assq-delete-all 'height allparms)))
520 (if (assq 'width frame-initial-geometry-arguments)
521 (setq allparms (assq-delete-all 'width allparms)))
522 (if (assq 'left frame-initial-geometry-arguments)
523 (setq allparms (assq-delete-all 'left allparms)))
524 (if (assq 'top frame-initial-geometry-arguments)
525 (setq allparms (assq-delete-all 'top allparms)))
526 (setq tail allparms)
527 ;; Find just the parms that have changed since we first
528 ;; made this frame. Those are the ones actually set by
529 ;; the init file. For those parms whose values we already knew
530 ;; (such as those spec'd by command line options)
531 ;; it is undesirable to specify the parm again
532 ;; once the user has seen the frame and been able to alter it
533 ;; manually.
534 (let (newval oldval)
535 (dolist (entry tail)
536 (setq oldval (assq (car entry) frame-initial-frame-alist))
537 (setq newval (cdr (assq (car entry) allparms)))
538 (or (and oldval (eq (cdr oldval) newval))
539 (setq newparms
540 (cons (cons (car entry) newval) newparms)))))
541 (setq newparms (nreverse newparms))
543 (let ((new-bg (assq 'background-color newparms)))
544 ;; If the `background-color' parameter is changed, apply
545 ;; it first, then make sure that the `background-mode'
546 ;; parameter and other faces are updated, before applying
547 ;; the other parameters.
548 (when new-bg
549 (modify-frame-parameters frame-initial-frame
550 (list new-bg))
551 (unless (assq 'background-mode newparms)
552 (frame-set-background-mode frame-initial-frame))
553 (face-set-after-frame-default frame-initial-frame)
554 (setq newparms (delq new-bg newparms)))
556 (when (numberp (car frame-size-history))
557 (setq frame-size-history
558 (cons (1- (car frame-size-history))
559 (cons
560 (list frame-initial-frame
561 "FRAME-NOTICE-USER"
562 nil newparms)
563 (cdr frame-size-history)))))
565 (modify-frame-parameters frame-initial-frame newparms)))))
567 ;; Restore the original buffer.
568 (set-buffer old-buffer)
570 ;; Make sure the initial frame can be GC'd if it is ever deleted.
571 ;; Make sure frame-notice-user-settings does nothing if called twice.
572 (setq frame-notice-user-settings nil)
573 (setq frame-initial-frame nil)))
575 (defun make-initial-minibuffer-frame (display)
576 (let ((parms (append minibuffer-frame-alist '((minibuffer . only)))))
577 (if display
578 (make-frame-on-display display parms)
579 (make-frame parms))))
581 ;;;; Creation of additional frames, and other frame miscellanea
583 (defun modify-all-frames-parameters (alist)
584 "Modify all current and future frames' parameters according to ALIST.
585 This changes `default-frame-alist' and possibly `initial-frame-alist'.
586 Furthermore, this function removes all parameters in ALIST from
587 `window-system-default-frame-alist'.
588 See help of `modify-frame-parameters' for more information."
589 (dolist (frame (frame-list))
590 (modify-frame-parameters frame alist))
592 (dolist (pair alist) ;; conses to add/replace
593 ;; initial-frame-alist needs setting only when
594 ;; frame-notice-user-settings is true.
595 (and frame-notice-user-settings
596 (setq initial-frame-alist
597 (assq-delete-all (car pair) initial-frame-alist)))
598 (setq default-frame-alist
599 (assq-delete-all (car pair) default-frame-alist))
600 ;; Remove any similar settings from the window-system specific
601 ;; parameters---they would override default-frame-alist.
602 (dolist (w window-system-default-frame-alist)
603 (setcdr w (assq-delete-all (car pair) (cdr w)))))
605 (and frame-notice-user-settings
606 (setq initial-frame-alist (append initial-frame-alist alist)))
607 (setq default-frame-alist (append default-frame-alist alist)))
609 (defun get-other-frame ()
610 "Return some frame other than the current frame.
611 Create one if necessary. Note that the minibuffer frame, if separate,
612 is not considered (see `next-frame')."
613 (if (equal (next-frame) (selected-frame)) (make-frame) (next-frame)))
615 (defun next-multiframe-window ()
616 "Select the next window, regardless of which frame it is on."
617 (interactive)
618 (select-window (next-window (selected-window)
619 (> (minibuffer-depth) 0)
621 (select-frame-set-input-focus (selected-frame)))
623 (defun previous-multiframe-window ()
624 "Select the previous window, regardless of which frame it is on."
625 (interactive)
626 (select-window (previous-window (selected-window)
627 (> (minibuffer-depth) 0)
629 (select-frame-set-input-focus (selected-frame)))
631 (defun window-system-for-display (display)
632 "Return the window system for DISPLAY.
633 Return nil if we don't know how to interpret DISPLAY."
634 ;; MS-Windows doesn't know how to create a GUI frame in a -nw session.
635 (if (and (eq system-type 'windows-nt)
636 (null (window-system))
637 (not (daemonp)))
639 (cl-loop for descriptor in display-format-alist
640 for pattern = (car descriptor)
641 for system = (cdr descriptor)
642 when (string-match-p pattern display) return system)))
644 (defun make-frame-on-display (display &optional parameters)
645 "Make a frame on display DISPLAY.
646 The optional argument PARAMETERS specifies additional frame parameters."
647 (interactive "sMake frame on display: ")
648 (make-frame (cons (cons 'display display) parameters)))
650 (declare-function x-close-connection "xfns.c" (terminal))
652 (defun close-display-connection (display)
653 "Close the connection to a display, deleting all its associated frames.
654 For DISPLAY, specify either a frame or a display name (a string).
655 If DISPLAY is nil, that stands for the selected frame's display."
656 (interactive
657 (list
658 (let* ((default (frame-parameter nil 'display))
659 (display (completing-read
660 (format "Close display (default %s): " default)
661 (delete-dups
662 (mapcar (lambda (frame)
663 (frame-parameter frame 'display))
664 (frame-list)))
665 nil t nil nil
666 default)))
667 (if (zerop (length display)) default display))))
668 (let ((frames (delq nil
669 (mapcar (lambda (frame)
670 (if (equal display
671 (frame-parameter frame 'display))
672 frame))
673 (frame-list)))))
674 (if (and (consp frames)
675 (not (y-or-n-p (if (cdr frames)
676 (format "Delete %s frames? " (length frames))
677 (format "Delete %s ? " (car frames))))))
678 (error "Abort!")
679 (mapc 'delete-frame frames)
680 (x-close-connection display))))
682 (defun make-frame-command ()
683 "Make a new frame, on the same terminal as the selected frame.
684 If the terminal is a text-only terminal, this also selects the
685 new frame."
686 (interactive)
687 (if (display-graphic-p)
688 (make-frame)
689 (select-frame (make-frame))))
691 (defvar before-make-frame-hook nil
692 "Functions to run before `make-frame' creates a new frame.")
694 (defvar after-make-frame-functions nil
695 "Functions to run after `make-frame' created a new frame.
696 The functions are run with one argument, the newly created
697 frame.")
699 (defvar after-setting-font-hook nil
700 "Functions to run after a frame's font has been changed.")
702 (defvar frame-inherited-parameters '()
703 "Parameters `make-frame' copies from the selected to the new frame.")
705 (defvar x-display-name)
707 (defun make-frame (&optional parameters)
708 "Return a newly created frame displaying the current buffer.
709 Optional argument PARAMETERS is an alist of frame parameters for
710 the new frame. Each element of PARAMETERS should have the
711 form (NAME . VALUE), for example:
713 (name . STRING) The frame should be named STRING.
715 (width . NUMBER) The frame should be NUMBER characters in width.
716 (height . NUMBER) The frame should be NUMBER text lines high.
718 (minibuffer . t) The frame should have a minibuffer.
719 (minibuffer . nil) The frame should have no minibuffer.
720 (minibuffer . only) The frame should contain only a minibuffer.
721 (minibuffer . WINDOW) The frame should use WINDOW as its minibuffer window.
723 (window-system . nil) The frame should be displayed on a terminal device.
724 (window-system . x) The frame should be displayed in an X window.
726 (display . \":0\") The frame should appear on display :0.
728 (terminal . TERMINAL) The frame should use the terminal object TERMINAL.
730 In addition, any parameter specified in `default-frame-alist',
731 but not present in PARAMETERS, is applied.
733 Before creating the frame (via `frame-creation-function'), this
734 function runs the hook `before-make-frame-hook'. After creating
735 the frame, it runs the hook `after-make-frame-functions' with one
736 argument, the newly created frame.
738 If a display parameter is supplied and a window-system is not,
739 guess the window-system from the display.
741 On graphical displays, this function does not itself make the new
742 frame the selected frame. However, the window system may select
743 the new frame according to its own rules."
744 (interactive)
745 (let* ((display (cdr (assq 'display parameters)))
746 (w (cond
747 ((assq 'terminal parameters)
748 (let ((type (terminal-live-p
749 (cdr (assq 'terminal parameters)))))
750 (cond
751 ((eq t type) nil)
752 ((null type) (error "Terminal %s does not exist"
753 (cdr (assq 'terminal parameters))))
754 (t type))))
755 ((assq 'window-system parameters)
756 (cdr (assq 'window-system parameters)))
757 (display
758 (or (window-system-for-display display)
759 (error "Don't know how to interpret display %S"
760 display)))
761 (t window-system)))
762 (oldframe (selected-frame))
763 (params parameters)
764 frame)
766 (unless (get w 'window-system-initialized)
767 (let ((window-system w)) ;Hack attack!
768 (window-system-initialization display))
769 (setq x-display-name display)
770 (put w 'window-system-initialized t))
772 ;; Add parameters from `window-system-default-frame-alist'.
773 (dolist (p (cdr (assq w window-system-default-frame-alist)))
774 (unless (assq (car p) params)
775 (push p params)))
776 ;; Add parameters from `default-frame-alist'.
777 (dolist (p default-frame-alist)
778 (unless (assq (car p) params)
779 (push p params)))
780 ;; Now make the frame.
781 (run-hooks 'before-make-frame-hook)
783 ;; (setq frame-size-history '(1000))
785 (setq frame (let ((window-system w)) ;Hack attack!
786 (frame-creation-function params)))
787 (normal-erase-is-backspace-setup-frame frame)
788 ;; Inherit the original frame's parameters.
789 (dolist (param frame-inherited-parameters)
790 (unless (assq param parameters) ;Overridden by explicit parameters.
791 (let ((val (frame-parameter oldframe param)))
792 (when val (set-frame-parameter frame param val)))))
794 (when (numberp (car frame-size-history))
795 (setq frame-size-history
796 (cons (1- (car frame-size-history))
797 (cons (list frame "MAKE-FRAME")
798 (cdr frame-size-history)))))
800 ;; We can run `window-configuration-change-hook' for this frame now.
801 (frame-after-make-frame frame t)
802 (run-hook-with-args 'after-make-frame-functions frame)
803 frame))
805 (defun filtered-frame-list (predicate)
806 "Return a list of all live frames which satisfy PREDICATE."
807 (let* ((frames (frame-list))
808 (list frames))
809 (while (consp frames)
810 (unless (funcall predicate (car frames))
811 (setcar frames nil))
812 (setq frames (cdr frames)))
813 (delq nil list)))
815 (defun minibuffer-frame-list ()
816 "Return a list of all frames with their own minibuffers."
817 (filtered-frame-list
818 (lambda (frame)
819 (eq frame (window-frame (minibuffer-window frame))))))
821 ;; Used to be called `terminal-id' in termdev.el.
822 (defun get-device-terminal (device)
823 "Return the terminal corresponding to DEVICE.
824 DEVICE can be a terminal, a frame, nil (meaning the selected frame's terminal),
825 the name of an X display device (HOST.SERVER.SCREEN) or a tty device file."
826 (cond
827 ((or (null device) (framep device))
828 (frame-terminal device))
829 ((stringp device)
830 (let ((f (car (filtered-frame-list
831 (lambda (frame)
832 (or (equal (frame-parameter frame 'display) device)
833 (equal (frame-parameter frame 'tty) device)))))))
834 (or f (error "Display %s does not exist" device))
835 (frame-terminal f)))
836 ((terminal-live-p device) device)
838 (error "Invalid argument %s in `get-device-terminal'" device))))
840 (defun frames-on-display-list (&optional device)
841 "Return a list of all frames on DEVICE.
843 DEVICE should be a terminal, a frame,
844 or a name of an X display or tty (a string of the form
845 HOST:SERVER.SCREEN).
847 If DEVICE is omitted or nil, it defaults to the selected
848 frame's terminal device."
849 (let* ((terminal (get-device-terminal device))
850 (func #'(lambda (frame)
851 (eq (frame-terminal frame) terminal))))
852 (filtered-frame-list func)))
854 (defun framep-on-display (&optional terminal)
855 "Return the type of frames on TERMINAL.
856 TERMINAL may be a terminal id, a display name or a frame. If it
857 is a frame, its type is returned. If TERMINAL is omitted or nil,
858 it defaults to the selected frame's terminal device. All frames
859 on a given display are of the same type."
860 (or (terminal-live-p terminal)
861 (framep terminal)
862 (framep (car (frames-on-display-list terminal)))))
864 (defun frame-remove-geometry-params (param-list)
865 "Return the parameter list PARAM-LIST, but with geometry specs removed.
866 This deletes all bindings in PARAM-LIST for `top', `left', `width',
867 `height', `user-size' and `user-position' parameters.
868 Emacs uses this to avoid overriding explicit moves and resizings from
869 the user during startup."
870 (setq param-list (cons nil param-list))
871 (let ((tail param-list))
872 (while (consp (cdr tail))
873 (if (and (consp (car (cdr tail)))
874 (memq (car (car (cdr tail)))
875 '(height width top left user-position user-size)))
876 (progn
877 (setq frame-initial-geometry-arguments
878 (cons (car (cdr tail)) frame-initial-geometry-arguments))
879 (setcdr tail (cdr (cdr tail))))
880 (setq tail (cdr tail)))))
881 (setq frame-initial-geometry-arguments
882 (nreverse frame-initial-geometry-arguments))
883 (cdr param-list))
885 (declare-function x-focus-frame "frame.c" (frame &optional noactivate))
887 (defun select-frame-set-input-focus (frame &optional norecord)
888 "Select FRAME, raise it, and set input focus, if possible.
889 If `mouse-autoselect-window' is non-nil, also move mouse pointer
890 to FRAME's selected window. Otherwise, if `focus-follows-mouse'
891 is non-nil, move mouse cursor to FRAME.
893 Optional argument NORECORD means to neither change the order of
894 recently selected windows nor the buffer list."
895 (select-frame frame norecord)
896 (raise-frame frame)
897 ;; Ensure, if possible, that FRAME gets input focus.
898 (when (memq (window-system frame) '(x w32 ns))
899 (x-focus-frame frame))
900 ;; Move mouse cursor if necessary.
901 (cond
902 (mouse-autoselect-window
903 (let ((edges (window-inside-edges (frame-selected-window frame))))
904 ;; Move mouse cursor into FRAME's selected window to avoid that
905 ;; Emacs mouse-autoselects another window.
906 (set-mouse-position frame (nth 2 edges) (nth 1 edges))))
907 (focus-follows-mouse
908 ;; Move mouse cursor into FRAME to avoid that another frame gets
909 ;; selected by the window manager.
910 (set-mouse-position frame (1- (frame-width frame)) 0))))
912 (defun other-frame (arg)
913 "Select the ARGth different visible frame on current display, and raise it.
914 All frames are arranged in a cyclic order.
915 This command selects the frame ARG steps away in that order.
916 A negative ARG moves in the opposite order.
918 To make this command work properly, you must tell Emacs how the
919 system (or the window manager) generally handles focus-switching
920 between windows. If moving the mouse onto a window selects
921 it (gives it focus), set `focus-follows-mouse' to t. Otherwise,
922 that variable should be nil."
923 (interactive "p")
924 (let ((sframe (selected-frame))
925 (frame (selected-frame)))
926 (while (> arg 0)
927 (setq frame (next-frame frame))
928 (while (and (not (eq frame sframe))
929 (not (eq (frame-visible-p frame) t)))
930 (setq frame (next-frame frame)))
931 (setq arg (1- arg)))
932 (while (< arg 0)
933 (setq frame (previous-frame frame))
934 (while (and (not (eq frame sframe))
935 (not (eq (frame-visible-p frame) t)))
936 (setq frame (previous-frame frame)))
937 (setq arg (1+ arg)))
938 (select-frame-set-input-focus frame)))
940 (defun iconify-or-deiconify-frame ()
941 "Iconify the selected frame, or deiconify if it's currently an icon."
942 (interactive)
943 (if (eq (cdr (assq 'visibility (frame-parameters))) t)
944 (iconify-frame)
945 (make-frame-visible)))
947 (defun suspend-frame ()
948 "Do whatever is right to suspend the current frame.
949 Calls `suspend-emacs' if invoked from the controlling tty device,
950 `suspend-tty' from a secondary tty device, and
951 `iconify-or-deiconify-frame' from an X frame."
952 (interactive)
953 (let ((type (framep (selected-frame))))
954 (cond
955 ((memq type '(x ns w32)) (iconify-or-deiconify-frame))
956 ((eq type t)
957 (if (controlling-tty-p)
958 (suspend-emacs)
959 (suspend-tty)))
960 (t (suspend-emacs)))))
962 (defun make-frame-names-alist ()
963 ;; Only consider the frames on the same display.
964 (let* ((current-frame (selected-frame))
965 (falist
966 (cons
967 (cons (frame-parameter current-frame 'name) current-frame) nil))
968 (frame (next-frame nil 0)))
969 (while (not (eq frame current-frame))
970 (progn
971 (push (cons (frame-parameter frame 'name) frame) falist)
972 (setq frame (next-frame frame 0))))
973 falist))
975 (defvar frame-name-history nil)
976 (defun select-frame-by-name (name)
977 "Select the frame whose name is NAME and raise it.
978 Frames on the current terminal are checked first.
979 If there is no frame by that name, signal an error."
980 (interactive
981 (let* ((frame-names-alist (make-frame-names-alist))
982 (default (car (car frame-names-alist)))
983 (input (completing-read
984 (format "Select Frame (default %s): " default)
985 frame-names-alist nil t nil 'frame-name-history)))
986 (if (= (length input) 0)
987 (list default)
988 (list input))))
989 (select-frame-set-input-focus
990 ;; Prefer frames on the current display.
991 (or (cdr (assoc name (make-frame-names-alist)))
992 (catch 'done
993 (dolist (frame (frame-list))
994 (when (equal (frame-parameter frame 'name) name)
995 (throw 'done frame))))
996 (error "There is no frame named `%s'" name))))
999 ;;;; Background mode.
1001 (defcustom frame-background-mode nil
1002 "The brightness of the background.
1003 Set this to the symbol `dark' if your background color is dark,
1004 `light' if your background is light, or nil (automatic by default)
1005 if you want Emacs to examine the brightness for you.
1007 If you change this without using customize, you should use
1008 `frame-set-background-mode' to update existing frames;
1009 e.g. (mapc \\='frame-set-background-mode (frame-list))."
1010 :group 'faces
1011 :set #'(lambda (var value)
1012 (set-default var value)
1013 (mapc 'frame-set-background-mode (frame-list)))
1014 :initialize 'custom-initialize-changed
1015 :type '(choice (const dark)
1016 (const light)
1017 (const :tag "automatic" nil)))
1019 (declare-function x-get-resource "frame.c"
1020 (attribute class &optional component subclass))
1022 ;; Only used if window-system is not null.
1023 (declare-function x-display-grayscale-p "xfns.c" (&optional terminal))
1025 (defvar inhibit-frame-set-background-mode nil)
1027 (defun frame-set-background-mode (frame &optional keep-face-specs)
1028 "Set up display-dependent faces on FRAME.
1029 Display-dependent faces are those which have different definitions
1030 according to the `background-mode' and `display-type' frame parameters.
1032 If optional arg KEEP-FACE-SPECS is non-nil, don't recalculate
1033 face specs for the new background mode."
1034 (unless inhibit-frame-set-background-mode
1035 (let* ((frame-default-bg-mode (frame-terminal-default-bg-mode frame))
1036 (bg-color (frame-parameter frame 'background-color))
1037 (tty-type (tty-type frame))
1038 (default-bg-mode
1039 (if (or (window-system frame)
1040 (and tty-type
1041 (string-match "^\\(xterm\\|\\rxvt\\|dtterm\\|eterm\\)"
1042 tty-type)))
1043 'light
1044 'dark))
1045 (non-default-bg-mode (if (eq default-bg-mode 'light) 'dark 'light))
1046 (bg-mode
1047 (cond (frame-default-bg-mode)
1048 ((equal bg-color "unspecified-fg") ; inverted colors
1049 non-default-bg-mode)
1050 ((not (color-values bg-color frame))
1051 default-bg-mode)
1052 ((>= (apply '+ (color-values bg-color frame))
1053 ;; Just looking at the screen, colors whose
1054 ;; values add up to .6 of the white total
1055 ;; still look dark to me.
1056 (* (apply '+ (color-values "white" frame)) .6))
1057 'light)
1058 (t 'dark)))
1059 (display-type
1060 (cond ((null (window-system frame))
1061 (if (tty-display-color-p frame) 'color 'mono))
1062 ((display-color-p frame)
1063 'color)
1064 ((x-display-grayscale-p frame)
1065 'grayscale)
1066 (t 'mono)))
1067 (old-bg-mode
1068 (frame-parameter frame 'background-mode))
1069 (old-display-type
1070 (frame-parameter frame 'display-type)))
1072 (unless (and (eq bg-mode old-bg-mode) (eq display-type old-display-type))
1073 (let ((locally-modified-faces nil)
1074 ;; Prevent face-spec-recalc from calling this function
1075 ;; again, resulting in a loop (bug#911).
1076 (inhibit-frame-set-background-mode t)
1077 (params (list (cons 'background-mode bg-mode)
1078 (cons 'display-type display-type))))
1079 (if keep-face-specs
1080 (modify-frame-parameters frame params)
1081 ;; If we are recomputing face specs, first collect a list
1082 ;; of faces that don't match their face-specs. These are
1083 ;; the faces modified on FRAME, and we avoid changing them
1084 ;; below. Use a negative list to avoid consing (we assume
1085 ;; most faces are unmodified).
1086 (dolist (face (face-list))
1087 (and (not (get face 'face-override-spec))
1088 (not (face-spec-match-p face
1089 (face-user-default-spec face)
1090 (selected-frame)))
1091 (push face locally-modified-faces)))
1092 ;; Now change to the new frame parameters
1093 (modify-frame-parameters frame params)
1094 ;; For all unmodified named faces, choose face specs
1095 ;; matching the new frame parameters.
1096 (dolist (face (face-list))
1097 (unless (memq face locally-modified-faces)
1098 (face-spec-recalc face frame)))))))))
1100 (defun frame-terminal-default-bg-mode (frame)
1101 "Return the default background mode of FRAME.
1102 This checks the `frame-background-mode' variable, the X resource
1103 named \"backgroundMode\" (if FRAME is an X frame), and finally
1104 the `background-mode' terminal parameter."
1105 (or frame-background-mode
1106 (let ((bg-resource
1107 (and (window-system frame)
1108 (x-get-resource "backgroundMode" "BackgroundMode"))))
1109 (if bg-resource
1110 (intern (downcase bg-resource))))
1111 (terminal-parameter frame 'background-mode)))
1114 ;;;; Frame configurations
1116 (defun current-frame-configuration ()
1117 "Return a list describing the positions and states of all frames.
1118 Its car is `frame-configuration'.
1119 Each element of the cdr is a list of the form (FRAME ALIST WINDOW-CONFIG),
1120 where
1121 FRAME is a frame object,
1122 ALIST is an association list specifying some of FRAME's parameters, and
1123 WINDOW-CONFIG is a window configuration object for FRAME."
1124 (cons 'frame-configuration
1125 (mapcar (lambda (frame)
1126 (list frame
1127 (frame-parameters frame)
1128 (current-window-configuration frame)))
1129 (frame-list))))
1131 (defun set-frame-configuration (configuration &optional nodelete)
1132 "Restore the frames to the state described by CONFIGURATION.
1133 Each frame listed in CONFIGURATION has its position, size, window
1134 configuration, and other parameters set as specified in CONFIGURATION.
1135 However, this function does not restore deleted frames.
1137 Ordinarily, this function deletes all existing frames not
1138 listed in CONFIGURATION. But if optional second argument NODELETE
1139 is given and non-nil, the unwanted frames are iconified instead."
1140 (or (frame-configuration-p configuration)
1141 (signal 'wrong-type-argument
1142 (list 'frame-configuration-p configuration)))
1143 (let ((config-alist (cdr configuration))
1144 frames-to-delete)
1145 (dolist (frame (frame-list))
1146 (let ((parameters (assq frame config-alist)))
1147 (if parameters
1148 (progn
1149 (modify-frame-parameters
1150 frame
1151 ;; Since we can't set a frame's minibuffer status,
1152 ;; we might as well omit the parameter altogether.
1153 (let* ((parms (nth 1 parameters))
1154 (mini (assq 'minibuffer parms))
1155 (name (assq 'name parms))
1156 (explicit-name (cdr (assq 'explicit-name parms))))
1157 (when mini (setq parms (delq mini parms)))
1158 ;; Leave name in iff it was set explicitly.
1159 ;; This should fix the behavior reported in
1160 ;; https://lists.gnu.org/r/emacs-devel/2007-08/msg01632.html
1161 (when (and name (not explicit-name))
1162 (setq parms (delq name parms)))
1163 parms))
1164 (set-window-configuration (nth 2 parameters)))
1165 (setq frames-to-delete (cons frame frames-to-delete)))))
1166 (mapc (if nodelete
1167 ;; Note: making frames invisible here was tried
1168 ;; but led to some strange behavior--each time the frame
1169 ;; was made visible again, the window manager asked afresh
1170 ;; for where to put it.
1171 'iconify-frame
1172 'delete-frame)
1173 frames-to-delete)))
1175 ;;;; Convenience functions for accessing and interactively changing
1176 ;;;; frame parameters.
1178 (defun frame-height (&optional frame)
1179 "Return number of lines available for display on FRAME.
1180 If FRAME is omitted, describe the currently selected frame.
1181 Exactly what is included in the return value depends on the
1182 window-system and toolkit in use - see `frame-pixel-height' for
1183 more details. The lines are in units of the default font height.
1185 The result is roughly related to the frame pixel height via
1186 height in pixels = height in lines * `frame-char-height'.
1187 However, this is only approximate, and is complicated e.g. by the
1188 fact that individual window lines and menu bar lines can have
1189 differing font heights."
1190 (cdr (assq 'height (frame-parameters frame))))
1192 (defun frame-width (&optional frame)
1193 "Return number of columns available for display on FRAME.
1194 If FRAME is omitted, describe the currently selected frame."
1195 (cdr (assq 'width (frame-parameters frame))))
1197 (defalias 'frame-border-width 'frame-internal-border-width)
1198 (defalias 'frame-pixel-width 'frame-native-width)
1199 (defalias 'frame-pixel-height 'frame-native-height)
1201 (defun frame-inner-width (&optional frame)
1202 "Return inner width of FRAME in pixels.
1203 FRAME defaults to the selected frame."
1204 (setq frame (window-normalize-frame frame))
1205 (- (frame-native-width frame)
1206 (* 2 (frame-internal-border-width frame))))
1208 (defun frame-inner-height (&optional frame)
1209 "Return inner height of FRAME in pixels.
1210 FRAME defaults to the selected frame."
1211 (setq frame (window-normalize-frame frame))
1212 (- (frame-native-height frame)
1213 (* 2 (frame-internal-border-width frame))))
1215 (defun frame-outer-width (&optional frame)
1216 "Return outer width of FRAME in pixels.
1217 FRAME defaults to the selected frame."
1218 (setq frame (window-normalize-frame frame))
1219 (let ((edges (frame-edges frame 'outer-edges)))
1220 (- (nth 2 edges) (nth 0 edges))))
1222 (defun frame-outer-height (&optional frame)
1223 "Return outer height of FRAME in pixels.
1224 FRAME defaults to the selected frame."
1225 (setq frame (window-normalize-frame frame))
1226 (let ((edges (frame-edges frame 'outer-edges)))
1227 (- (nth 3 edges) (nth 1 edges))))
1229 (declare-function x-list-fonts "xfaces.c"
1230 (pattern &optional face frame maximum width))
1232 (defun set-frame-font (font &optional keep-size frames)
1233 "Set the default font to FONT.
1234 When called interactively, prompt for the name of a font, and use
1235 that font on the selected frame. When called from Lisp, FONT
1236 should be a font name (a string), a font object, font entity, or
1237 font spec.
1239 If KEEP-SIZE is nil, keep the number of frame lines and columns
1240 fixed. If KEEP-SIZE is non-nil (or with a prefix argument), try
1241 to keep the current frame size fixed (in pixels) by adjusting the
1242 number of lines and columns.
1244 If FRAMES is nil, apply the font to the selected frame only.
1245 If FRAMES is non-nil, it should be a list of frames to act upon,
1246 or t meaning all existing graphical frames.
1247 Also, if FRAMES is non-nil, alter the user's Customization settings
1248 as though the font-related attributes of the `default' face had been
1249 \"set in this session\", so that the font is applied to future frames."
1250 (interactive
1251 (let* ((completion-ignore-case t)
1252 (font (completing-read "Font name: "
1253 ;; x-list-fonts will fail with an error
1254 ;; if this frame doesn't support fonts.
1255 (x-list-fonts "*" nil (selected-frame))
1256 nil nil nil nil
1257 (frame-parameter nil 'font))))
1258 (list font current-prefix-arg nil)))
1259 (when (or (stringp font) (fontp font))
1260 (let* ((this-frame (selected-frame))
1261 ;; FRAMES nil means affect the selected frame.
1262 (frame-list (cond ((null frames)
1263 (list this-frame))
1264 ((eq frames t)
1265 (frame-list))
1266 (t frames)))
1267 height width)
1268 (dolist (f frame-list)
1269 (when (display-multi-font-p f)
1270 (if keep-size
1271 (setq height (* (frame-parameter f 'height)
1272 (frame-char-height f))
1273 width (* (frame-parameter f 'width)
1274 (frame-char-width f))))
1275 ;; When set-face-attribute is called for :font, Emacs
1276 ;; guesses the best font according to other face attributes
1277 ;; (:width, :weight, etc.) so reset them too (Bug#2476).
1278 (set-face-attribute 'default f
1279 :width 'normal :weight 'normal
1280 :slant 'normal :font font)
1281 (if keep-size
1282 (modify-frame-parameters
1284 (list (cons 'height (round height (frame-char-height f)))
1285 (cons 'width (round width (frame-char-width f))))))))
1286 (when frames
1287 ;; Alter the user's Custom setting of the `default' face, but
1288 ;; only for font-related attributes.
1289 (let ((specs (cadr (assq 'user (get 'default 'theme-face))))
1290 (attrs '(:family :foundry :slant :weight :height :width))
1291 (new-specs nil))
1292 (if (null specs) (setq specs '((t nil))))
1293 (dolist (spec specs)
1294 ;; Each SPEC has the form (DISPLAY ATTRIBUTE-PLIST)
1295 (let ((display (nth 0 spec))
1296 (plist (copy-tree (nth 1 spec))))
1297 ;; Alter only DISPLAY conditions matching this frame.
1298 (when (or (memq display '(t default))
1299 (face-spec-set-match-display display this-frame))
1300 (dolist (attr attrs)
1301 (setq plist (plist-put plist attr
1302 (face-attribute 'default attr)))))
1303 (push (list display plist) new-specs)))
1304 (setq new-specs (nreverse new-specs))
1305 (put 'default 'customized-face new-specs)
1306 (custom-push-theme 'theme-face 'default 'user 'set new-specs)
1307 (put 'default 'face-modified nil))))
1308 (run-hooks 'after-setting-font-hook 'after-setting-font-hooks)))
1310 (defun set-frame-parameter (frame parameter value)
1311 "Set frame parameter PARAMETER to VALUE on FRAME.
1312 If FRAME is nil, it defaults to the selected frame.
1313 See `modify-frame-parameters'."
1314 (modify-frame-parameters frame (list (cons parameter value))))
1316 (defun set-background-color (color-name)
1317 "Set the background color of the selected frame to COLOR-NAME.
1318 When called interactively, prompt for the name of the color to use.
1319 To get the frame's current background color, use `frame-parameters'."
1320 (interactive (list (read-color "Background color: ")))
1321 (modify-frame-parameters (selected-frame)
1322 (list (cons 'background-color color-name)))
1323 (or window-system
1324 (face-set-after-frame-default (selected-frame)
1325 (list
1326 (cons 'background-color color-name)
1327 ;; Pass the foreground-color as
1328 ;; well, if defined, to avoid
1329 ;; losing it when faces are reset
1330 ;; to their defaults.
1331 (assq 'foreground-color
1332 (frame-parameters))))))
1334 (defun set-foreground-color (color-name)
1335 "Set the foreground color of the selected frame to COLOR-NAME.
1336 When called interactively, prompt for the name of the color to use.
1337 To get the frame's current foreground color, use `frame-parameters'."
1338 (interactive (list (read-color "Foreground color: ")))
1339 (modify-frame-parameters (selected-frame)
1340 (list (cons 'foreground-color color-name)))
1341 (or window-system
1342 (face-set-after-frame-default (selected-frame)
1343 (list
1344 (cons 'foreground-color color-name)
1345 ;; Pass the background-color as
1346 ;; well, if defined, to avoid
1347 ;; losing it when faces are reset
1348 ;; to their defaults.
1349 (assq 'background-color
1350 (frame-parameters))))))
1352 (defun set-cursor-color (color-name)
1353 "Set the text cursor color of the selected frame to COLOR-NAME.
1354 When called interactively, prompt for the name of the color to use.
1355 This works by setting the `cursor-color' frame parameter on the
1356 selected frame.
1358 You can also set the text cursor color, for all frames, by
1359 customizing the `cursor' face."
1360 (interactive (list (read-color "Cursor color: ")))
1361 (modify-frame-parameters (selected-frame)
1362 (list (cons 'cursor-color color-name))))
1364 (defun set-mouse-color (color-name)
1365 "Set the color of the mouse pointer of the selected frame to COLOR-NAME.
1366 When called interactively, prompt for the name of the color to use.
1367 To get the frame's current mouse color, use `frame-parameters'."
1368 (interactive (list (read-color "Mouse color: ")))
1369 (modify-frame-parameters (selected-frame)
1370 (list (cons 'mouse-color
1371 (or color-name
1372 (cdr (assq 'mouse-color
1373 (frame-parameters))))))))
1375 (defun set-border-color (color-name)
1376 "Set the color of the border of the selected frame to COLOR-NAME.
1377 When called interactively, prompt for the name of the color to use.
1378 To get the frame's current border color, use `frame-parameters'."
1379 (interactive (list (read-color "Border color: ")))
1380 (modify-frame-parameters (selected-frame)
1381 (list (cons 'border-color color-name))))
1383 (define-minor-mode auto-raise-mode
1384 "Toggle whether or not selected frames should auto-raise.
1386 Auto Raise mode does nothing under most window managers, which
1387 switch focus on mouse clicks. It only has an effect if your
1388 window manager switches focus on mouse movement (in which case
1389 you should also change `focus-follows-mouse' to t). Then,
1390 enabling Auto Raise mode causes any graphical Emacs frame which
1391 acquires focus to be automatically raised.
1393 Note that this minor mode controls Emacs's own auto-raise
1394 feature. Window managers that switch focus on mouse movement
1395 often have their own auto-raise feature."
1396 :variable (frame-parameter nil 'auto-raise)
1397 (if (frame-parameter nil 'auto-raise)
1398 (raise-frame)))
1400 (define-minor-mode auto-lower-mode
1401 "Toggle whether or not the selected frame should auto-lower.
1403 Auto Lower mode does nothing under most window managers, which
1404 switch focus on mouse clicks. It only has an effect if your
1405 window manager switches focus on mouse movement (in which case
1406 you should also change `focus-follows-mouse' to t). Then,
1407 enabling Auto Lower Mode causes any graphical Emacs frame which
1408 loses focus to be automatically lowered.
1410 Note that this minor mode controls Emacs's own auto-lower
1411 feature. Window managers that switch focus on mouse movement
1412 often have their own features for raising or lowering frames."
1413 :variable (frame-parameter nil 'auto-lower))
1415 (defun set-frame-name (name)
1416 "Set the name of the selected frame to NAME.
1417 When called interactively, prompt for the name of the frame.
1418 On text terminals, the frame name is displayed on the mode line.
1419 On graphical displays, it is displayed on the frame's title bar."
1420 (interactive "sFrame name: ")
1421 (modify-frame-parameters (selected-frame)
1422 (list (cons 'name name))))
1424 (defun frame-current-scroll-bars (&optional frame)
1425 "Return the current scroll-bar types for frame FRAME.
1426 Value is a cons (VERTICAL . HORIZ0NTAL) where VERTICAL specifies
1427 the current location of the vertical scroll-bars (`left', `right'
1428 or nil), and HORIZONTAL specifies the current location of the
1429 horizontal scroll bars (`bottom' or nil). FRAME must specify a
1430 live frame and defaults to the selected one."
1431 (let* ((frame (window-normalize-frame frame))
1432 (vertical (frame-parameter frame 'vertical-scroll-bars))
1433 (horizontal (frame-parameter frame 'horizontal-scroll-bars)))
1434 (unless (memq vertical '(left right nil))
1435 (setq vertical default-frame-scroll-bars))
1436 (cons vertical (and horizontal 'bottom))))
1438 (declare-function x-frame-geometry "xfns.c" (&optional frame))
1439 (declare-function w32-frame-geometry "w32fns.c" (&optional frame))
1440 (declare-function ns-frame-geometry "nsfns.m" (&optional frame))
1442 (defun frame-geometry (&optional frame)
1443 "Return geometric attributes of FRAME.
1444 FRAME must be a live frame and defaults to the selected one. The return
1445 value is an association list of the attributes listed below. All height
1446 and width values are in pixels.
1448 `outer-position' is a cons of the outer left and top edges of FRAME
1449 relative to the origin - the position (0, 0) - of FRAME's display.
1451 `outer-size' is a cons of the outer width and height of FRAME. The
1452 outer size includes the title bar and the external borders as well as
1453 any menu and/or tool bar of frame.
1455 `external-border-size' is a cons of the horizontal and vertical width of
1456 FRAME's external borders as supplied by the window manager.
1458 `title-bar-size' is a cons of the width and height of the title bar of
1459 FRAME as supplied by the window manager. If both of them are zero,
1460 FRAME has no title bar. If only the width is zero, Emacs was not
1461 able to retrieve the width information.
1463 `menu-bar-external', if non-nil, means the menu bar is external (never
1464 included in the inner edges of FRAME).
1466 `menu-bar-size' is a cons of the width and height of the menu bar of
1467 FRAME.
1469 `tool-bar-external', if non-nil, means the tool bar is external (never
1470 included in the inner edges of FRAME).
1472 `tool-bar-position' tells on which side the tool bar on FRAME is and can
1473 be one of `left', `top', `right' or `bottom'. If this is nil, FRAME
1474 has no tool bar.
1476 `tool-bar-size' is a cons of the width and height of the tool bar of
1477 FRAME.
1479 `internal-border-width' is the width of the internal border of
1480 FRAME."
1481 (let* ((frame (window-normalize-frame frame))
1482 (frame-type (framep-on-display frame)))
1483 (cond
1484 ((eq frame-type 'x)
1485 (x-frame-geometry frame))
1486 ((eq frame-type 'w32)
1487 (w32-frame-geometry frame))
1488 ((eq frame-type 'ns)
1489 (ns-frame-geometry frame))
1491 (list
1492 '(outer-position 0 . 0)
1493 (cons 'outer-size (cons (frame-width frame) (frame-height frame)))
1494 '(external-border-size 0 . 0)
1495 '(outer-border-width . 0)
1496 '(title-bar-size 0 . 0)
1497 '(menu-bar-external . nil)
1498 (let ((menu-bar-lines (frame-parameter frame 'menu-bar-lines)))
1499 (cons 'menu-bar-size
1500 (if menu-bar-lines
1501 (cons (frame-width frame) 1)
1502 1 0)))
1503 '(tool-bar-external . nil)
1504 '(tool-bar-position . nil)
1505 '(tool-bar-size 0 . 0)
1506 (cons 'internal-border-width
1507 (frame-parameter frame 'internal-border-width)))))))
1509 (defun frame--size-history (&optional frame)
1510 "Print history of resize operations for FRAME.
1511 Print prettified version of `frame-size-history' into a buffer
1512 called *frame-size-history*. Optional argument FRAME denotes the
1513 frame whose history will be printed. FRAME defaults to the
1514 selected frame."
1515 (let ((history (reverse frame-size-history))
1516 entry)
1517 (setq frame (window-normalize-frame frame))
1518 (with-current-buffer (get-buffer-create "*frame-size-history*")
1519 (erase-buffer)
1520 (insert (format "Frame size history of %s\n" frame))
1521 (while (listp (setq entry (pop history)))
1522 (when (eq (car entry) frame)
1523 (pop entry)
1524 (insert (format "%s" (pop entry)))
1525 (move-to-column 24 t)
1526 (while entry
1527 (insert (format " %s" (pop entry))))
1528 (insert "\n"))))))
1530 (declare-function x-frame-edges "xfns.c" (&optional frame type))
1531 (declare-function w32-frame-edges "w32fns.c" (&optional frame type))
1532 (declare-function ns-frame-edges "nsfns.m" (&optional frame type))
1534 (defun frame-edges (&optional frame type)
1535 "Return coordinates of FRAME's edges.
1536 FRAME must be a live frame and defaults to the selected one. The
1537 list returned has the form (LEFT TOP RIGHT BOTTOM) where all
1538 values are in pixels relative to the origin - the position (0, 0)
1539 - of FRAME's display. For terminal frames all values are
1540 relative to LEFT and TOP which are both zero.
1542 Optional argument TYPE specifies the type of the edges. TYPE
1543 `outer-edges' means to return the outer edges of FRAME. TYPE
1544 `native-edges' (or nil) means to return the native edges of
1545 FRAME. TYPE `inner-edges' means to return the inner edges of
1546 FRAME."
1547 (let* ((frame (window-normalize-frame frame))
1548 (frame-type (framep-on-display frame)))
1549 (cond
1550 ((eq frame-type 'x)
1551 (x-frame-edges frame type))
1552 ((eq frame-type 'w32)
1553 (w32-frame-edges frame type))
1554 ((eq frame-type 'ns)
1555 (ns-frame-edges frame type))
1557 (list 0 0 (frame-width frame) (frame-height frame))))))
1559 (declare-function w32-mouse-absolute-pixel-position "w32fns.c")
1560 (declare-function x-mouse-absolute-pixel-position "xfns.c")
1561 (declare-function ns-mouse-absolute-pixel-position "nsfns.m")
1563 (defun mouse-absolute-pixel-position ()
1564 "Return absolute position of mouse cursor in pixels.
1565 The position is returned as a cons cell (X . Y) of the
1566 coordinates of the mouse cursor position in pixels relative to a
1567 position (0, 0) of the selected frame's terminal."
1568 (let ((frame-type (framep-on-display)))
1569 (cond
1570 ((eq frame-type 'x)
1571 (x-mouse-absolute-pixel-position))
1572 ((eq frame-type 'w32)
1573 (w32-mouse-absolute-pixel-position))
1574 ((eq frame-type 'ns)
1575 (ns-mouse-absolute-pixel-position))
1577 (cons 0 0)))))
1579 (declare-function ns-set-mouse-absolute-pixel-position "nsfns.m" (x y))
1580 (declare-function w32-set-mouse-absolute-pixel-position "w32fns.c" (x y))
1581 (declare-function x-set-mouse-absolute-pixel-position "xfns.c" (x y))
1583 (defun set-mouse-absolute-pixel-position (x y)
1584 "Move mouse pointer to absolute pixel position (X, Y).
1585 The coordinates X and Y are interpreted in pixels relative to a
1586 position (0, 0) of the selected frame's terminal."
1587 (let ((frame-type (framep-on-display)))
1588 (cond
1589 ((eq frame-type 'ns)
1590 (ns-set-mouse-absolute-pixel-position x y))
1591 ((eq frame-type 'x)
1592 (x-set-mouse-absolute-pixel-position x y))
1593 ((eq frame-type 'w32)
1594 (w32-set-mouse-absolute-pixel-position x y)))))
1596 (defun frame-monitor-attributes (&optional frame)
1597 "Return the attributes of the physical monitor dominating FRAME.
1598 If FRAME is omitted or nil, describe the currently selected frame.
1600 A frame is dominated by a physical monitor when either the
1601 largest area of the frame resides in the monitor, or the monitor
1602 is the closest to the frame if the frame does not intersect any
1603 physical monitors.
1605 See `display-monitor-attributes-list' for the list of attribute
1606 keys and their meanings."
1607 (or frame (setq frame (selected-frame)))
1608 (cl-loop for attributes in (display-monitor-attributes-list frame)
1609 for frames = (cdr (assq 'frames attributes))
1610 if (memq frame frames) return attributes))
1612 (defun frame-monitor-attribute (attribute &optional frame x y)
1613 "Return the value of ATTRIBUTE on FRAME's monitor.
1614 If FRAME is omitted or nil, use currently selected frame.
1616 By default, the current monitor is the physical monitor
1617 dominating the selected frame. A frame is dominated by a
1618 physical monitor when either the largest area of the frame
1619 resides in the monitor, or the monitor is the closest to the
1620 frame if the frame does not intersect any physical monitors.
1622 If X and Y are both numbers, then ignore the value of FRAME; the
1623 monitor is determined to be the physical monitor that contains
1624 the pixel coordinate (X, Y).
1626 See `display-monitor-attributes-list' for the list of attribute
1627 keys and their meanings."
1628 (if (and (numberp x)
1629 (numberp y))
1630 (cl-loop for monitor in (display-monitor-attributes-list)
1631 for geometry = (alist-get 'geometry monitor)
1632 for min-x = (pop geometry)
1633 for min-y = (pop geometry)
1634 for max-x = (+ min-x (pop geometry))
1635 for max-y = (+ min-y (car geometry))
1636 when (and (<= min-x x)
1637 (< x max-x)
1638 (<= min-y y)
1639 (< y max-y))
1640 return (alist-get attribute monitor))
1641 (alist-get attribute (frame-monitor-attributes frame))))
1643 (defun frame-monitor-geometry (&optional frame x y)
1644 "Return the geometry of FRAME's monitor.
1645 FRAME can be a frame name, a terminal name, or a frame.
1646 If FRAME is omitted or nil, use the currently selected frame.
1648 By default, the current monitor is said to be the physical
1649 monitor dominating the selected frame. A frame is dominated by
1650 a physical monitor when either the largest area of the frame resides
1651 in the monitor, or the monitor is the closest to the frame if the
1652 frame does not intersect any physical monitors.
1654 If X and Y are both numbers, then ignore the value of FRAME; the
1655 monitor is determined to be the physical monitor that contains
1656 the pixel coordinate (X, Y).
1658 See `display-monitor-attributes-list' for information on the
1659 geometry attribute."
1660 (frame-monitor-attribute 'geometry frame x y))
1662 (defun frame-monitor-workarea (&optional frame x y)
1663 "Return the workarea of FRAME's monitor.
1664 FRAME can be a frame name, a terminal name, or a frame.
1665 If FRAME is omitted or nil, use currently selected frame.
1667 By default, the current monitor is said to be the physical
1668 monitor dominating the selected frame. A frame is dominated by
1669 a physical monitor when either the largest area of the frame resides
1670 in the monitor, or the monitor is the closest to the frame if the
1671 frame does not intersect any physical monitors.
1673 If X and Y are both numbers, then ignore the value of FRAME; the
1674 monitor is determined to be the physical monitor that contains
1675 the pixel coordinate (X, Y).
1677 See `display-monitor-attributes-list' for information on the
1678 workarea attribute."
1679 (frame-monitor-attribute 'workarea frame x y))
1681 (declare-function x-frame-list-z-order "xfns.c" (&optional display))
1682 (declare-function w32-frame-list-z-order "w32fns.c" (&optional display))
1683 (declare-function ns-frame-list-z-order "nsfns.m" (&optional display))
1685 (defun frame-list-z-order (&optional display)
1686 "Return list of Emacs' frames, in Z (stacking) order.
1687 The optional argument DISPLAY specifies which display to poll.
1688 DISPLAY should be either a frame or a display name (a string).
1689 If omitted or nil, that stands for the selected frame's display.
1691 Frames are listed from topmost (first) to bottommost (last). As
1692 a special case, if DISPLAY is non-nil and specifies a live frame,
1693 return the child frames of that frame in Z (stacking) order.
1695 Return nil if DISPLAY contains no Emacs frame."
1696 (let ((frame-type (framep-on-display display)))
1697 (cond
1698 ((eq frame-type 'x)
1699 (x-frame-list-z-order display))
1700 ((eq frame-type 'w32)
1701 (w32-frame-list-z-order display))
1702 ((eq frame-type 'ns)
1703 (ns-frame-list-z-order display)))))
1705 (declare-function x-frame-restack "xfns.c" (frame1 frame2 &optional above))
1706 (declare-function w32-frame-restack "w32fns.c" (frame1 frame2 &optional above))
1707 (declare-function ns-frame-restack "nsfns.m" (frame1 frame2 &optional above))
1709 (defun frame-restack (frame1 frame2 &optional above)
1710 "Restack FRAME1 below FRAME2.
1711 This implies that if both frames are visible and the display
1712 areas of these frames overlap, FRAME2 will (partially) obscure
1713 FRAME1. If the optional third argument ABOVE is non-nil, restack
1714 FRAME1 above FRAME2. This means that if both frames are visible
1715 and the display areas of these frames overlap, FRAME1 will
1716 \(partially) obscure FRAME2.
1718 This may be thought of as an atomic action performed in two
1719 steps: The first step removes FRAME1's window-system window from
1720 the display. The second step reinserts FRAME1's window
1721 below (above if ABOVE is true) that of FRAME2. Hence the
1722 position of FRAME2 in its display's Z (stacking) order relative
1723 to all other frames excluding FRAME1 remains unaltered.
1725 Some window managers may refuse to restack windows. "
1726 (if (and (frame-live-p frame1)
1727 (frame-live-p frame2)
1728 (equal (frame-parameter frame1 'display)
1729 (frame-parameter frame2 'display)))
1730 (let ((frame-type (framep-on-display frame1)))
1731 (cond
1732 ((eq frame-type 'x)
1733 (x-frame-restack frame1 frame2 above))
1734 ((eq frame-type 'w32)
1735 (w32-frame-restack frame1 frame2 above))
1736 ((eq frame-type 'ns)
1737 (ns-frame-restack frame1 frame2 above))))
1738 (error "Cannot restack frames")))
1740 (defun frame-size-changed-p (&optional frame)
1741 "Return non-nil when the size of FRAME has changed.
1742 More precisely, return non-nil when the inner width or height of
1743 FRAME has changed since `window-size-change-functions' was run
1744 for FRAME."
1745 (let* ((frame (window-normalize-frame frame))
1746 (root (frame-root-window frame))
1747 (mini (minibuffer-window frame))
1748 (mini-height-before-size-change 0)
1749 (mini-height 0))
1750 ;; FRAME's minibuffer window counts iff it's on FRAME and FRAME is
1751 ;; not a minibuffer-only frame.
1752 (when (and (eq (window-frame mini) frame) (not (eq mini root)))
1753 (setq mini-height-before-size-change
1754 (window-pixel-height-before-size-change mini))
1755 (setq mini-height (window-pixel-height mini)))
1756 ;; Return non-nil when either the width of the root or the sum of
1757 ;; the heights of root and minibuffer window changed.
1758 (or (/= (window-pixel-width-before-size-change root)
1759 (window-pixel-width root))
1760 (/= (+ (window-pixel-height-before-size-change root)
1761 mini-height-before-size-change)
1762 (+ (window-pixel-height root) mini-height)))))
1764 ;;;; Frame/display capabilities.
1766 (declare-function msdos-mouse-p "dosfns.c")
1768 (defun display-mouse-p (&optional display)
1769 "Return non-nil if DISPLAY has a mouse available.
1770 DISPLAY can be a display name, a frame, or nil (meaning the selected
1771 frame's display)."
1772 (let ((frame-type (framep-on-display display)))
1773 (cond
1774 ((eq frame-type 'pc)
1775 (msdos-mouse-p))
1776 ((eq frame-type 'w32)
1777 (with-no-warnings
1778 (> w32-num-mouse-buttons 0)))
1779 ((memq frame-type '(x ns))
1780 t) ;; We assume X and NeXTstep *always* have a pointing device
1782 (or (and (featurep 'xt-mouse)
1783 xterm-mouse-mode)
1784 ;; t-mouse is distributed with the GPM package. It doesn't have
1785 ;; a toggle.
1786 (featurep 't-mouse)
1787 ;; No way to check whether a w32 console has a mouse, assume
1788 ;; it always does.
1789 (boundp 'w32-use-full-screen-buffer))))))
1791 (defun display-popup-menus-p (&optional display)
1792 "Return non-nil if popup menus are supported on DISPLAY.
1793 DISPLAY can be a display name, a frame, or nil (meaning the selected
1794 frame's display).
1795 Support for popup menus requires that the mouse be available."
1796 (display-mouse-p display))
1798 (defun display-graphic-p (&optional display)
1799 "Return non-nil if DISPLAY is a graphic display.
1800 Graphical displays are those which are capable of displaying several
1801 frames and several different fonts at once. This is true for displays
1802 that use a window system such as X, and false for text-only terminals.
1803 DISPLAY can be a display name, a frame, or nil (meaning the selected
1804 frame's display)."
1805 (not (null (memq (framep-on-display display) '(x w32 ns)))))
1807 (defun display-images-p (&optional display)
1808 "Return non-nil if DISPLAY can display images.
1810 DISPLAY can be a display name, a frame, or nil (meaning the selected
1811 frame's display)."
1812 (and (display-graphic-p display)
1813 (fboundp 'image-mask-p)
1814 (fboundp 'image-size)))
1816 (defalias 'display-multi-frame-p 'display-graphic-p)
1817 (defalias 'display-multi-font-p 'display-graphic-p)
1819 (defun display-selections-p (&optional display)
1820 "Return non-nil if DISPLAY supports selections.
1821 A selection is a way to transfer text or other data between programs
1822 via special system buffers called `selection' or `clipboard'.
1823 DISPLAY can be a display name, a frame, or nil (meaning the selected
1824 frame's display)."
1825 (let ((frame-type (framep-on-display display)))
1826 (cond
1827 ((eq frame-type 'pc)
1828 ;; MS-DOS frames support selections when Emacs runs inside
1829 ;; a Windows DOS Box.
1830 (with-no-warnings
1831 (not (null dos-windows-version))))
1832 ((memq frame-type '(x w32 ns))
1835 nil))))
1837 (declare-function x-display-screens "xfns.c" (&optional terminal))
1839 (defun display-screens (&optional display)
1840 "Return the number of screens associated with DISPLAY.
1841 DISPLAY should be either a frame or a display name (a string).
1842 If DISPLAY is omitted or nil, it defaults to the selected frame's display."
1843 (let ((frame-type (framep-on-display display)))
1844 (cond
1845 ((memq frame-type '(x w32 ns))
1846 (x-display-screens display))
1848 1))))
1850 (declare-function x-display-pixel-height "xfns.c" (&optional terminal))
1852 (defun display-pixel-height (&optional display)
1853 "Return the height of DISPLAY's screen in pixels.
1854 DISPLAY can be a display name or a frame.
1855 If DISPLAY is omitted or nil, it defaults to the selected frame's display.
1857 For character terminals, each character counts as a single pixel.
1859 For graphical terminals, note that on \"multi-monitor\" setups this
1860 refers to the pixel height for all physical monitors associated
1861 with DISPLAY. To get information for each physical monitor, use
1862 `display-monitor-attributes-list'."
1863 (let ((frame-type (framep-on-display display)))
1864 (cond
1865 ((memq frame-type '(x w32 ns))
1866 (x-display-pixel-height display))
1868 (frame-height (if (framep display) display (selected-frame)))))))
1870 (declare-function x-display-pixel-width "xfns.c" (&optional terminal))
1872 (defun display-pixel-width (&optional display)
1873 "Return the width of DISPLAY's screen in pixels.
1874 DISPLAY can be a display name or a frame.
1875 If DISPLAY is omitted or nil, it defaults to the selected frame's display.
1877 For character terminals, each character counts as a single pixel.
1879 For graphical terminals, note that on \"multi-monitor\" setups this
1880 refers to the pixel width for all physical monitors associated
1881 with DISPLAY. To get information for each physical monitor, use
1882 `display-monitor-attributes-list'."
1883 (let ((frame-type (framep-on-display display)))
1884 (cond
1885 ((memq frame-type '(x w32 ns))
1886 (x-display-pixel-width display))
1888 (frame-width (if (framep display) display (selected-frame)))))))
1890 (defcustom display-mm-dimensions-alist nil
1891 "Alist for specifying screen dimensions in millimeters.
1892 The functions `display-mm-height' and `display-mm-width' consult
1893 this list before asking the system.
1895 Each element has the form (DISPLAY . (WIDTH . HEIGHT)), e.g.
1896 \(\":0.0\" . (287 . 215)).
1898 If `display' is t, it specifies dimensions for all graphical displays
1899 not explicitly specified."
1900 :version "22.1"
1901 :type '(alist :key-type (choice (string :tag "Display name")
1902 (const :tag "Default" t))
1903 :value-type (cons :tag "Dimensions"
1904 (integer :tag "Width")
1905 (integer :tag "Height")))
1906 :group 'frames)
1908 (declare-function x-display-mm-height "xfns.c" (&optional terminal))
1910 (defun display-mm-height (&optional display)
1911 "Return the height of DISPLAY's screen in millimeters.
1912 If the information is unavailable, this function returns nil.
1913 DISPLAY can be a display name or a frame.
1914 If DISPLAY is omitted or nil, it defaults to the selected frame's display.
1916 You can override what the system thinks the result should be by
1917 adding an entry to `display-mm-dimensions-alist'.
1919 For graphical terminals, note that on \"multi-monitor\" setups this
1920 refers to the height in millimeters for all physical monitors
1921 associated with DISPLAY. To get information for each physical
1922 monitor, use `display-monitor-attributes-list'."
1923 (and (memq (framep-on-display display) '(x w32 ns))
1924 (or (cddr (assoc (or display (frame-parameter nil 'display))
1925 display-mm-dimensions-alist))
1926 (cddr (assoc t display-mm-dimensions-alist))
1927 (x-display-mm-height display))))
1929 (declare-function x-display-mm-width "xfns.c" (&optional terminal))
1931 (defun display-mm-width (&optional display)
1932 "Return the width of DISPLAY's screen in millimeters.
1933 If the information is unavailable, this function returns nil.
1934 DISPLAY can be a display name or a frame.
1935 If DISPLAY is omitted or nil, it defaults to the selected frame's display.
1937 You can override what the system thinks the result should be by
1938 adding an entry to `display-mm-dimensions-alist'.
1940 For graphical terminals, note that on \"multi-monitor\" setups this
1941 refers to the width in millimeters for all physical monitors
1942 associated with DISPLAY. To get information for each physical
1943 monitor, use `display-monitor-attributes-list'."
1944 (and (memq (framep-on-display display) '(x w32 ns))
1945 (or (cadr (assoc (or display (frame-parameter nil 'display))
1946 display-mm-dimensions-alist))
1947 (cadr (assoc t display-mm-dimensions-alist))
1948 (x-display-mm-width display))))
1950 (declare-function x-display-backing-store "xfns.c" (&optional terminal))
1952 ;; In NS port, the return value may be `buffered', `retained', or
1953 ;; `non-retained'. See src/nsfns.m.
1954 (defun display-backing-store (&optional display)
1955 "Return the backing store capability of DISPLAY's screen.
1956 The value may be `always', `when-mapped', `not-useful', or nil if
1957 the question is inapplicable to a certain kind of display.
1958 DISPLAY can be a display name or a frame.
1959 If DISPLAY is omitted or nil, it defaults to the selected frame's display."
1960 (let ((frame-type (framep-on-display display)))
1961 (cond
1962 ((memq frame-type '(x w32 ns))
1963 (x-display-backing-store display))
1965 'not-useful))))
1967 (declare-function x-display-save-under "xfns.c" (&optional terminal))
1969 (defun display-save-under (&optional display)
1970 "Return non-nil if DISPLAY's screen supports the SaveUnder feature.
1971 DISPLAY can be a display name or a frame.
1972 If DISPLAY is omitted or nil, it defaults to the selected frame's display."
1973 (let ((frame-type (framep-on-display display)))
1974 (cond
1975 ((memq frame-type '(x w32 ns))
1976 (x-display-save-under display))
1978 'not-useful))))
1980 (declare-function x-display-planes "xfns.c" (&optional terminal))
1982 (defun display-planes (&optional display)
1983 "Return the number of planes supported by DISPLAY.
1984 DISPLAY can be a display name or a frame.
1985 If DISPLAY is omitted or nil, it defaults to the selected frame's display."
1986 (let ((frame-type (framep-on-display display)))
1987 (cond
1988 ((memq frame-type '(x w32 ns))
1989 (x-display-planes display))
1990 ((eq frame-type 'pc)
1993 (truncate (log (length (tty-color-alist)) 2))))))
1995 (declare-function x-display-color-cells "xfns.c" (&optional terminal))
1997 (defun display-color-cells (&optional display)
1998 "Return the number of color cells supported by DISPLAY.
1999 DISPLAY can be a display name or a frame.
2000 If DISPLAY is omitted or nil, it defaults to the selected frame's display."
2001 (let ((frame-type (framep-on-display display)))
2002 (cond
2003 ((memq frame-type '(x w32 ns))
2004 (x-display-color-cells display))
2005 ((eq frame-type 'pc)
2008 (tty-display-color-cells display)))))
2010 (declare-function x-display-visual-class "xfns.c" (&optional terminal))
2012 (defun display-visual-class (&optional display)
2013 "Return the visual class of DISPLAY.
2014 The value is one of the symbols `static-gray', `gray-scale',
2015 `static-color', `pseudo-color', `true-color', or `direct-color'.
2016 DISPLAY can be a display name or a frame.
2017 If DISPLAY is omitted or nil, it defaults to the selected frame's display."
2018 (let ((frame-type (framep-on-display display)))
2019 (cond
2020 ((memq frame-type '(x w32 ns))
2021 (x-display-visual-class display))
2022 ((and (memq frame-type '(pc t))
2023 (tty-display-color-p display))
2024 'static-color)
2026 'static-gray))))
2028 (declare-function x-display-monitor-attributes-list "xfns.c"
2029 (&optional terminal))
2030 (declare-function w32-display-monitor-attributes-list "w32fns.c"
2031 (&optional display))
2032 (declare-function ns-display-monitor-attributes-list "nsfns.m"
2033 (&optional terminal))
2035 (defun display-monitor-attributes-list (&optional display)
2036 "Return a list of physical monitor attributes on DISPLAY.
2037 DISPLAY can be a display name, a terminal name, or a frame.
2038 If DISPLAY is omitted or nil, it defaults to the selected frame's display.
2039 Each element of the list represents the attributes of a physical
2040 monitor. The first element corresponds to the primary monitor.
2042 The attributes for a physical monitor are represented as an alist
2043 of attribute keys and values as follows:
2045 geometry -- Position and size in pixels in the form of (X Y WIDTH HEIGHT)
2046 workarea -- Position and size of the work area in pixels in the
2047 form of (X Y WIDTH HEIGHT)
2048 mm-size -- Width and height in millimeters in the form of
2049 (WIDTH HEIGHT)
2050 frames -- List of frames dominated by the physical monitor
2051 name (*) -- Name of the physical monitor as a string
2052 source (*) -- Source of multi-monitor information as a string
2054 where X, Y, WIDTH, and HEIGHT are integers. X and Y are coordinates
2055 of the top-left corner, and might be negative for monitors other than
2056 the primary one. Keys labeled with (*) are optional.
2058 The \"work area\" is a measure of the \"usable\" display space.
2059 It may be less than the total screen size, owing to space taken up
2060 by window manager features (docks, taskbars, etc.). The precise
2061 details depend on the platform and environment.
2063 The `source' attribute describes the source from which the information
2064 was obtained. On X, this may be one of: \"Gdk\", \"XRandr\", \"Xinerama\",
2065 or \"fallback\".
2067 A frame is dominated by a physical monitor when either the
2068 largest area of the frame resides in the monitor, or the monitor
2069 is the closest to the frame if the frame does not intersect any
2070 physical monitors. Every (non-tooltip) frame (including invisible ones)
2071 in a graphical display is dominated by exactly one physical
2072 monitor at a time, though it can span multiple (or no) physical
2073 monitors."
2074 (let ((frame-type (framep-on-display display)))
2075 (cond
2076 ((eq frame-type 'x)
2077 (x-display-monitor-attributes-list display))
2078 ((eq frame-type 'w32)
2079 (w32-display-monitor-attributes-list display))
2080 ((eq frame-type 'ns)
2081 (ns-display-monitor-attributes-list display))
2083 (let ((geometry (list 0 0 (display-pixel-width display)
2084 (display-pixel-height display))))
2085 `(((geometry . ,geometry)
2086 (workarea . ,geometry)
2087 (mm-size . (,(display-mm-width display)
2088 ,(display-mm-height display)))
2089 (frames . ,(frames-on-display-list display)))))))))
2092 ;;;; Frame geometry values
2094 (defun frame-geom-value-cons (type value &optional frame)
2095 "Return equivalent geometry value for FRAME as a cons with car `+'.
2096 A geometry value equivalent to VALUE for FRAME is returned,
2097 where the value is a cons with car `+', not numeric.
2098 TYPE is the car of the original geometry spec (TYPE . VALUE).
2099 It is `top' or `left', depending on which edge VALUE is related to.
2100 VALUE is the cdr of a frame geometry spec: (left/top . VALUE).
2101 If VALUE is a number, then it is converted to a cons value, perhaps
2102 relative to the opposite frame edge from that in the original spec.
2103 FRAME defaults to the selected frame.
2105 Examples (measures in pixels) -
2106 Assuming display height/width=1024, frame height/width=600:
2107 300 inside display edge: 300 => (+ 300)
2108 (+ 300) => (+ 300)
2109 300 inside opposite display edge: (- 300) => (+ 124)
2110 -300 => (+ 124)
2111 300 beyond display edge
2112 (= 724 inside opposite display edge): (+ -300) => (+ -300)
2113 300 beyond display edge
2114 (= 724 inside opposite display edge): (- -300) => (+ 724)
2116 In the 3rd, 4th, and 6th examples, the returned value is relative to
2117 the opposite frame edge from the edge indicated in the input spec."
2118 (cond ((and (consp value) (eq '+ (car value))) ; e.g. (+ 300), (+ -300)
2119 value)
2120 ((natnump value) (list '+ value)) ; e.g. 300 => (+ 300)
2121 (t ; e.g. -300, (- 300), (- -300)
2122 (list '+ (- (if (eq 'left type) ; => (+ 124), (+ 124), (+ 724)
2123 (x-display-pixel-width)
2124 (x-display-pixel-height))
2125 (if (integerp value) (- value) (cadr value))
2126 (if (eq 'left type)
2127 (frame-pixel-width frame)
2128 (frame-pixel-height frame)))))))
2130 (defun frame-geom-spec-cons (spec &optional frame)
2131 "Return equivalent geometry spec for FRAME as a cons with car `+'.
2132 A geometry specification equivalent to SPEC for FRAME is returned,
2133 where the value is a cons with car `+', not numeric.
2134 SPEC is a frame geometry spec: (left . VALUE) or (top . VALUE).
2135 If VALUE is a number, then it is converted to a cons value, perhaps
2136 relative to the opposite frame edge from that in the original spec.
2137 FRAME defaults to the selected frame.
2139 Examples (measures in pixels) -
2140 Assuming display height=1024, frame height=600:
2141 top 300 below display top: (top . 300) => (top + 300)
2142 (top + 300) => (top + 300)
2143 bottom 300 above display bottom: (top - 300) => (top + 124)
2144 (top . -300) => (top + 124)
2145 top 300 above display top
2146 (= bottom 724 above display bottom): (top + -300) => (top + -300)
2147 bottom 300 below display bottom
2148 (= top 724 below display top): (top - -300) => (top + 724)
2150 In the 3rd, 4th, and 6th examples, the returned value is relative to
2151 the opposite frame edge from the edge indicated in the input spec."
2152 (cons (car spec) (frame-geom-value-cons (car spec) (cdr spec) frame)))
2154 (defun delete-other-frames (&optional frame)
2155 "Delete all frames on FRAME's terminal, except FRAME.
2156 If FRAME uses another frame's minibuffer, the minibuffer frame is
2157 left untouched. Do not delete any of FRAME's child frames. If
2158 FRAME is a child frame, delete its siblings only. FRAME must be
2159 a live frame and defaults to the selected one."
2160 (interactive)
2161 (setq frame (window-normalize-frame frame))
2162 (let ((minibuffer-frame (window-frame (minibuffer-window frame)))
2163 (this (next-frame frame t))
2164 (parent (frame-parent frame))
2165 next)
2166 ;; In a first round consider minibuffer-less frames only.
2167 (while (not (eq this frame))
2168 (setq next (next-frame this t))
2169 (unless (or (eq (window-frame (minibuffer-window this)) this)
2170 ;; When FRAME is a child frame, delete its siblings
2171 ;; only.
2172 (and parent (not (eq (frame-parent this) parent)))
2173 ;; Do not delete a child frame of FRAME.
2174 (eq (frame-parent this) frame))
2175 (delete-frame this))
2176 (setq this next))
2177 ;; In a second round consider all remaining frames.
2178 (setq this (next-frame frame t))
2179 (while (not (eq this frame))
2180 (setq next (next-frame this t))
2181 (unless (or (eq this minibuffer-frame)
2182 ;; When FRAME is a child frame, delete its siblings
2183 ;; only.
2184 (and parent (not (eq (frame-parent this) parent)))
2185 ;; Do not delete a child frame of FRAME.
2186 (eq (frame-parent this) frame))
2187 (delete-frame this))
2188 (setq this next))))
2191 ;;; Window dividers.
2192 (defgroup window-divider nil
2193 "Window dividers."
2194 :version "25.1"
2195 :group 'frames
2196 :group 'windows)
2198 (defcustom window-divider-default-places 'right-only
2199 "Default positions of window dividers.
2200 Possible values are `bottom-only' (dividers on the bottom of each
2201 window only), `right-only' (dividers on the right of each window
2202 only), and t (dividers on the bottom and on the right of each
2203 window). The default is `right-only'.
2205 The value takes effect if and only if dividers are enabled by
2206 `window-divider-mode'.
2208 To position dividers on frames individually, use the frame
2209 parameters `bottom-divider-width' and `right-divider-width'."
2210 :type '(choice (const :tag "Bottom only" bottom-only)
2211 (const :tag "Right only" right-only)
2212 (const :tag "Bottom and right" t))
2213 :initialize 'custom-initialize-default
2214 :set (lambda (symbol value)
2215 (set-default symbol value)
2216 (when window-divider-mode
2217 (window-divider-mode-apply t)))
2218 :version "25.1")
2220 (defun window-divider-width-valid-p (value)
2221 "Return non-nil if VALUE is a positive number."
2222 (and (numberp value) (> value 0)))
2224 (defcustom window-divider-default-bottom-width 6
2225 "Default width of dividers on bottom of windows.
2226 The value must be a positive integer and takes effect when bottom
2227 dividers are displayed by `window-divider-mode'.
2229 To adjust bottom dividers for frames individually, use the frame
2230 parameter `bottom-divider-width'."
2231 :type '(restricted-sexp
2232 :tag "Default width of bottom dividers"
2233 :match-alternatives (window-divider-width-valid-p))
2234 :initialize 'custom-initialize-default
2235 :set (lambda (symbol value)
2236 (set-default symbol value)
2237 (when window-divider-mode
2238 (window-divider-mode-apply t)))
2239 :version "25.1")
2241 (defcustom window-divider-default-right-width 6
2242 "Default width of dividers on the right of windows.
2243 The value must be a positive integer and takes effect when right
2244 dividers are displayed by `window-divider-mode'.
2246 To adjust right dividers for frames individually, use the frame
2247 parameter `right-divider-width'."
2248 :type '(restricted-sexp
2249 :tag "Default width of right dividers"
2250 :match-alternatives (window-divider-width-valid-p))
2251 :initialize 'custom-initialize-default
2252 :set (lambda (symbol value)
2253 (set-default symbol value)
2254 (when window-divider-mode
2255 (window-divider-mode-apply t)))
2256 :version "25.1")
2258 (defun window-divider-mode-apply (enable)
2259 "Apply window divider places and widths to all frames.
2260 If ENABLE is nil, apply default places and widths. Else reset
2261 all divider widths to zero."
2262 (let ((bottom (if (and enable
2263 (memq window-divider-default-places
2264 '(bottom-only t)))
2265 window-divider-default-bottom-width
2267 (right (if (and enable
2268 (memq window-divider-default-places
2269 '(right-only t)))
2270 window-divider-default-right-width
2271 0)))
2272 (modify-all-frames-parameters
2273 (list (cons 'bottom-divider-width bottom)
2274 (cons 'right-divider-width right)))
2275 (setq default-frame-alist
2276 (assq-delete-all
2277 'bottom-divider-width default-frame-alist))
2278 (setq default-frame-alist
2279 (assq-delete-all
2280 'right-divider-width default-frame-alist))
2281 (when (> bottom 0)
2282 (setq default-frame-alist
2283 (cons
2284 (cons 'bottom-divider-width bottom)
2285 default-frame-alist)))
2286 (when (> right 0)
2287 (setq default-frame-alist
2288 (cons
2289 (cons 'right-divider-width right)
2290 default-frame-alist)))))
2292 (define-minor-mode window-divider-mode
2293 "Display dividers between windows (Window Divider mode).
2295 The option `window-divider-default-places' specifies on which
2296 side of a window dividers are displayed. The options
2297 `window-divider-default-bottom-width' and
2298 `window-divider-default-right-width' specify their respective
2299 widths."
2300 :group 'window-divider
2301 :global t
2302 (window-divider-mode-apply window-divider-mode))
2304 ;; Blinking cursor
2306 (defvar blink-cursor-idle-timer nil
2307 "Timer started after `blink-cursor-delay' seconds of Emacs idle time.
2308 The function `blink-cursor-start' is called when the timer fires.")
2310 (defvar blink-cursor-timer nil
2311 "Timer started from `blink-cursor-start'.
2312 This timer calls `blink-cursor-timer-function' every
2313 `blink-cursor-interval' seconds.")
2315 (defgroup cursor nil
2316 "Displaying text cursors."
2317 :version "21.1"
2318 :group 'frames)
2320 (defcustom blink-cursor-delay 0.5
2321 "Seconds of idle time before the first blink of the cursor.
2322 Values smaller than 0.2 sec are treated as 0.2 sec."
2323 :type 'number
2324 :group 'cursor
2325 :set (lambda (symbol value)
2326 (set-default symbol value)
2327 (when blink-cursor-idle-timer (blink-cursor--start-idle-timer))))
2329 (defcustom blink-cursor-interval 0.5
2330 "Length of cursor blink interval in seconds."
2331 :type 'number
2332 :group 'cursor
2333 :set (lambda (symbol value)
2334 (set-default symbol value)
2335 (when blink-cursor-timer (blink-cursor--start-timer))))
2337 (defcustom blink-cursor-blinks 10
2338 "How many times to blink before using a solid cursor on NS, X, and MS-Windows.
2339 Use 0 or negative value to blink forever."
2340 :version "24.4"
2341 :type 'integer
2342 :group 'cursor)
2344 (defvar blink-cursor-blinks-done 1
2345 "Number of blinks done since we started blinking on NS, X, and MS-Windows.")
2347 (defun blink-cursor--start-idle-timer ()
2348 "Start the `blink-cursor-idle-timer'."
2349 (when blink-cursor-idle-timer (cancel-timer blink-cursor-idle-timer))
2350 (setq blink-cursor-idle-timer
2351 ;; The 0.2 sec limitation from below is to avoid erratic
2352 ;; behavior (or downright failure to display the cursor
2353 ;; during command execution) if they set blink-cursor-delay
2354 ;; to a very small or even zero value.
2355 (run-with-idle-timer (max 0.2 blink-cursor-delay)
2356 :repeat #'blink-cursor-start)))
2358 (defun blink-cursor--start-timer ()
2359 "Start the `blink-cursor-timer'."
2360 (when blink-cursor-timer (cancel-timer blink-cursor-timer))
2361 (setq blink-cursor-timer
2362 (run-with-timer blink-cursor-interval blink-cursor-interval
2363 #'blink-cursor-timer-function)))
2365 (defun blink-cursor-start ()
2366 "Timer function called from the timer `blink-cursor-idle-timer'.
2367 This starts the timer `blink-cursor-timer', which makes the cursor blink
2368 if appropriate. It also arranges to cancel that timer when the next
2369 command starts, by installing a pre-command hook."
2370 (when (null blink-cursor-timer)
2371 ;; Set up the timer first, so that if this signals an error,
2372 ;; blink-cursor-end is not added to pre-command-hook.
2373 (setq blink-cursor-blinks-done 1)
2374 (blink-cursor--start-timer)
2375 (add-hook 'pre-command-hook 'blink-cursor-end)
2376 (internal-show-cursor nil nil)))
2378 (defun blink-cursor-timer-function ()
2379 "Timer function of timer `blink-cursor-timer'."
2380 (internal-show-cursor nil (not (internal-show-cursor-p)))
2381 ;; Suspend counting blinks when the w32 menu-bar menu is displayed,
2382 ;; since otherwise menu tooltips will behave erratically.
2383 (or (and (fboundp 'w32--menu-bar-in-use)
2384 (w32--menu-bar-in-use))
2385 (setq blink-cursor-blinks-done (1+ blink-cursor-blinks-done)))
2386 ;; Each blink is two calls to this function.
2387 (when (and (> blink-cursor-blinks 0)
2388 (<= (* 2 blink-cursor-blinks) blink-cursor-blinks-done))
2389 (blink-cursor-suspend)
2390 (add-hook 'post-command-hook 'blink-cursor-check)))
2392 (defun blink-cursor-end ()
2393 "Stop cursor blinking.
2394 This is installed as a pre-command hook by `blink-cursor-start'.
2395 When run, it cancels the timer `blink-cursor-timer' and removes
2396 itself as a pre-command hook."
2397 (remove-hook 'pre-command-hook 'blink-cursor-end)
2398 (internal-show-cursor nil t)
2399 (when blink-cursor-timer
2400 (cancel-timer blink-cursor-timer)
2401 (setq blink-cursor-timer nil)))
2403 (defun blink-cursor-suspend ()
2404 "Suspend cursor blinking.
2405 This is called when no frame has focus and timers can be suspended.
2406 Timers are restarted by `blink-cursor-check', which is called when a
2407 frame receives focus."
2408 (blink-cursor-end)
2409 (when blink-cursor-idle-timer
2410 (cancel-timer blink-cursor-idle-timer)
2411 (setq blink-cursor-idle-timer nil)))
2413 (defun blink-cursor--should-blink ()
2414 "Determine whether we should be blinking.
2415 Returns whether we have any focused non-TTY frame."
2416 (and blink-cursor-mode
2417 (let ((frame-list (frame-list))
2418 (any-graphical-focused nil))
2419 (while frame-list
2420 (let ((frame (pop frame-list)))
2421 (when (and (display-graphic-p frame) (frame-focus-state frame))
2422 (setf any-graphical-focused t)
2423 (setf frame-list nil))))
2424 any-graphical-focused)))
2426 (defun blink-cursor-check ()
2427 "Check if cursor blinking shall be restarted.
2428 This is done when a frame gets focus. Blink timers may be
2429 stopped by `blink-cursor-suspend'. Internally calls
2430 `blink-cursor--should-blink' and returns its result."
2431 (let ((should-blink (blink-cursor--should-blink)))
2432 (when (and should-blink (not blink-cursor-idle-timer))
2433 (remove-hook 'post-command-hook 'blink-cursor-check)
2434 (blink-cursor--start-idle-timer))
2435 should-blink))
2437 (defun blink-cursor--rescan-frames (&optional _ign)
2438 "Called when the set of focused frames changes or when we delete a frame."
2439 (unless (blink-cursor-check)
2440 (blink-cursor-suspend)))
2442 (define-minor-mode blink-cursor-mode
2443 "Toggle cursor blinking (Blink Cursor mode).
2445 If the value of `blink-cursor-blinks' is positive (10 by default),
2446 the cursor stops blinking after that number of blinks, if Emacs
2447 gets no input during that time.
2449 See also `blink-cursor-interval' and `blink-cursor-delay'.
2451 This command is effective only on graphical frames. On text-only
2452 terminals, cursor blinking is controlled by the terminal."
2453 :init-value (not (or noninteractive
2454 no-blinking-cursor
2455 (eq system-type 'ms-dos)
2456 (not (memq window-system '(x w32 ns)))))
2457 :initialize 'custom-initialize-delay
2458 :group 'cursor
2459 :global t
2460 (blink-cursor-suspend)
2461 (remove-hook 'after-delete-frame-functions #'blink-cursor--rescan-frames)
2462 (remove-function after-focus-change-function #'blink-cursor--rescan-frames)
2463 (when blink-cursor-mode
2464 (add-function :after after-focus-change-function #'blink-cursor--rescan-frames)
2465 (add-hook 'after-delete-frame-functions #'blink-cursor--rescan-frames)
2466 (blink-cursor--start-idle-timer)))
2469 ;; Frame maximization/fullscreen
2471 (defun toggle-frame-maximized (&optional frame)
2472 "Toggle maximization state of FRAME.
2473 Maximize selected frame or un-maximize if it is already maximized.
2475 If the frame is in fullscreen state, don't change its state, but
2476 set the frame's `fullscreen-restore' parameter to `maximized', so
2477 the frame will be maximized after disabling fullscreen state.
2479 Note that with some window managers you may have to set
2480 `frame-resize-pixelwise' to non-nil in order to make a frame
2481 appear truly maximized. In addition, you may have to set
2482 `x-frame-normalize-before-maximize' in order to enable
2483 transitions from one fullscreen state to another.
2485 See also `toggle-frame-fullscreen'."
2486 (interactive)
2487 (let ((fullscreen (frame-parameter frame 'fullscreen)))
2488 (cond
2489 ((memq fullscreen '(fullscreen fullboth))
2490 (set-frame-parameter frame 'fullscreen-restore 'maximized))
2491 ((eq fullscreen 'maximized)
2492 (set-frame-parameter frame 'fullscreen nil))
2494 (set-frame-parameter frame 'fullscreen 'maximized)))))
2496 (defun toggle-frame-fullscreen (&optional frame)
2497 "Toggle fullscreen state of FRAME.
2498 Make selected frame fullscreen or restore its previous size
2499 if it is already fullscreen.
2501 Before making the frame fullscreen remember the current value of
2502 the frame's `fullscreen' parameter in the `fullscreen-restore'
2503 parameter of the frame. That value is used to restore the
2504 frame's fullscreen state when toggling fullscreen the next time.
2506 Note that with some window managers you may have to set
2507 `frame-resize-pixelwise' to non-nil in order to make a frame
2508 appear truly fullscreen. In addition, you may have to set
2509 `x-frame-normalize-before-maximize' in order to enable
2510 transitions from one fullscreen state to another.
2512 See also `toggle-frame-maximized'."
2513 (interactive)
2514 (let ((fullscreen (frame-parameter frame 'fullscreen)))
2515 (if (memq fullscreen '(fullscreen fullboth))
2516 (let ((fullscreen-restore (frame-parameter frame 'fullscreen-restore)))
2517 (if (memq fullscreen-restore '(maximized fullheight fullwidth))
2518 (set-frame-parameter frame 'fullscreen fullscreen-restore)
2519 (set-frame-parameter frame 'fullscreen nil)))
2520 (modify-frame-parameters
2521 frame `((fullscreen . fullboth) (fullscreen-restore . ,fullscreen))))
2522 ;; Manipulating a frame without waiting for the fullscreen
2523 ;; animation to complete can cause a crash, or other unexpected
2524 ;; behavior, on macOS (bug#28496).
2525 (when (featurep 'cocoa) (sleep-for 0.5))))
2528 ;;;; Key bindings
2530 (define-key ctl-x-5-map "2" 'make-frame-command)
2531 (define-key ctl-x-5-map "1" 'delete-other-frames)
2532 (define-key ctl-x-5-map "0" 'delete-frame)
2533 (define-key ctl-x-5-map "o" 'other-frame)
2534 (define-key global-map [f11] 'toggle-frame-fullscreen)
2535 (define-key global-map [(meta f10)] 'toggle-frame-maximized)
2536 (define-key esc-map [f10] 'toggle-frame-maximized)
2539 ;; Misc.
2541 ;; Only marked as obsolete in 24.3.
2542 (define-obsolete-variable-alias 'automatic-hscrolling
2543 'auto-hscroll-mode "22.1")
2545 (make-variable-buffer-local 'show-trailing-whitespace)
2547 ;; Defined in dispnew.c.
2548 (make-obsolete-variable
2549 'window-system-version "it does not give useful information." "24.3")
2551 ;; Variables whose change of value should trigger redisplay of the
2552 ;; current buffer.
2553 ;; To test whether a given variable needs to be added to this list,
2554 ;; write a simple interactive function that changes the variable's
2555 ;; value and bind that function to a simple key, like F5. If typing
2556 ;; F5 then produces the correct effect, the variable doesn't need
2557 ;; to be in this list; otherwise, it does.
2558 (mapc (lambda (var)
2559 ;; Using symbol-function here tells the watcher machinery to
2560 ;; call the C function set-buffer-redisplay directly, thus
2561 ;; avoiding a potential GC.
2562 (add-variable-watcher var (symbol-function 'set-buffer-redisplay)))
2563 '(line-spacing
2564 overline-margin
2565 line-prefix
2566 wrap-prefix
2567 truncate-lines
2568 display-line-numbers
2569 display-line-numbers-width
2570 display-line-numbers-current-absolute
2571 display-line-numbers-widen
2572 bidi-paragraph-direction
2573 bidi-display-reordering))
2575 (provide 'frame)
2577 ;;; frame.el ends here