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