Fix docstrings, declarations in iter-defun
[emacs.git] / lisp / frame.el
blobc81ee9bfa615773adccd2b727fae422cdd15f10c
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 ;; If we changed the background color, we need to update
263 ;; the background-mode parameter, and maybe some faces,
264 ;; too.
265 (when (assq 'background-color newparms)
266 (unless (or (assq 'background-mode initial-frame-alist)
267 (assq 'background-mode default-frame-alist))
268 (frame-set-background-mode frame))
269 (face-set-after-frame-default frame))))))
271 ;; If the initial frame is still around, apply initial-frame-alist
272 ;; and default-frame-alist to it.
273 (when (frame-live-p frame-initial-frame)
274 ;; When tool-bar has been switched off, correct the frame size
275 ;; by the lines added in x-create-frame for the tool-bar and
276 ;; switch `tool-bar-mode' off.
277 (when (display-graphic-p)
278 (let* ((init-lines
279 (assq 'tool-bar-lines initial-frame-alist))
280 (other-lines
281 (or (assq 'tool-bar-lines window-system-frame-alist)
282 (assq 'tool-bar-lines default-frame-alist)))
283 (lines (or init-lines other-lines))
284 (height (tool-bar-height frame-initial-frame t)))
285 ;; Adjust frame top if either zero (nil) tool bar lines have
286 ;; been requested in the most relevant of the frame's alists
287 ;; or tool bar mode has been explicitly turned off in the
288 ;; user's init file.
289 (when (and (> height 0)
290 (or (and lines
291 (or (null (cdr lines))
292 (eq 0 (cdr lines))))
293 (not tool-bar-mode)))
294 (let* ((initial-top
295 (cdr (assq 'top frame-initial-geometry-arguments)))
296 (top (frame-parameter frame-initial-frame 'top)))
297 (when (and (consp initial-top) (eq '- (car initial-top)))
298 (let ((adjusted-top
299 (cond
300 ((and (consp top) (eq '+ (car top)))
301 (list '+ (+ (cadr top) height)))
302 ((and (consp top) (eq '- (car top)))
303 (list '- (- (cadr top) height)))
304 (t (+ top height)))))
305 (modify-frame-parameters
306 frame-initial-frame `((top . ,adjusted-top))))))
307 ;; Reset `tool-bar-mode' when zero tool bar lines have been
308 ;; requested for the window-system or default frame alists.
309 (when (and tool-bar-mode
310 (and other-lines
311 (or (null (cdr other-lines))
312 (eq 0 (cdr other-lines)))))
313 (tool-bar-mode -1)))))
315 ;; The initial frame we create above always has a minibuffer.
316 ;; If the user wants to remove it, or make it a minibuffer-only
317 ;; frame, then we'll have to delete the current frame and make a
318 ;; new one; you can't remove or add a root window to/from an
319 ;; existing frame.
321 ;; NOTE: default-frame-alist was nil when we created the
322 ;; existing frame. We need to explicitly include
323 ;; default-frame-alist in the parameters of the screen we
324 ;; create here, so that its new value, gleaned from the user's
325 ;; init file, will be applied to the existing screen.
326 (if (not (eq (cdr (or (assq 'minibuffer initial-frame-alist)
327 (assq 'minibuffer window-system-frame-alist)
328 (assq 'minibuffer default-frame-alist)
329 '(minibuffer . t)))
331 ;; Create the new frame.
332 (let (parms new)
333 ;; MS-Windows needs this to avoid inflooping below.
334 (if (eq system-type 'windows-nt)
335 (sit-for 0 t))
336 ;; If the frame isn't visible yet, wait till it is.
337 ;; If the user has to position the window,
338 ;; Emacs doesn't know its real position until
339 ;; the frame is seen to be visible.
340 (while (not (cdr (assq 'visibility
341 (frame-parameters frame-initial-frame))))
342 (sleep-for 1))
343 (setq parms (frame-parameters frame-initial-frame))
345 ;; Get rid of `name' unless it was specified explicitly before.
346 (or (assq 'name frame-initial-frame-alist)
347 (setq parms (delq (assq 'name parms) parms)))
348 ;; An explicit parent-id is a request to XEmbed the frame.
349 (or (assq 'parent-id frame-initial-frame-alist)
350 (setq parms (delq (assq 'parent-id parms) parms)))
352 (setq parms (append initial-frame-alist
353 window-system-frame-alist
354 default-frame-alist
355 parms
356 nil))
358 ;; Get rid of `reverse', because that was handled
359 ;; when we first made the frame.
360 (setq parms (cons '(reverse) (delq (assq 'reverse parms) parms)))
362 (if (assq 'height frame-initial-geometry-arguments)
363 (setq parms (assq-delete-all 'height parms)))
364 (if (assq 'width frame-initial-geometry-arguments)
365 (setq parms (assq-delete-all 'width parms)))
366 (if (assq 'left frame-initial-geometry-arguments)
367 (setq parms (assq-delete-all 'left parms)))
368 (if (assq 'top frame-initial-geometry-arguments)
369 (setq parms (assq-delete-all 'top parms)))
370 (setq new
371 (make-frame
372 ;; Use the geometry args that created the existing
373 ;; frame, rather than the parms we get for it.
374 (append frame-initial-geometry-arguments
375 '((user-size . t) (user-position . t))
376 parms)))
377 ;; The initial frame, which we are about to delete, may be
378 ;; the only frame with a minibuffer. If it is, create a
379 ;; new one.
380 (or (delq frame-initial-frame (minibuffer-frame-list))
381 (make-initial-minibuffer-frame nil))
383 ;; If the initial frame is serving as a surrogate
384 ;; minibuffer frame for any frames, we need to wean them
385 ;; onto a new frame. The default-minibuffer-frame
386 ;; variable must be handled similarly.
387 (let ((users-of-initial
388 (filtered-frame-list
389 (lambda (frame)
390 (and (not (eq frame frame-initial-frame))
391 (eq (window-frame
392 (minibuffer-window frame))
393 frame-initial-frame))))))
394 (if (or users-of-initial
395 (eq default-minibuffer-frame frame-initial-frame))
397 ;; Choose an appropriate frame. Prefer frames which
398 ;; are only minibuffers.
399 (let* ((new-surrogate
400 (car
401 (or (filtered-frame-list
402 (lambda (frame)
403 (eq (cdr (assq 'minibuffer
404 (frame-parameters frame)))
405 'only)))
406 (minibuffer-frame-list))))
407 (new-minibuffer (minibuffer-window new-surrogate)))
409 (if (eq default-minibuffer-frame frame-initial-frame)
410 (setq default-minibuffer-frame new-surrogate))
412 ;; Wean the frames using frame-initial-frame as
413 ;; their minibuffer frame.
414 (dolist (frame users-of-initial)
415 (modify-frame-parameters
416 frame (list (cons 'minibuffer new-minibuffer)))))))
418 ;; Redirect events enqueued at this frame to the new frame.
419 ;; Is this a good idea?
420 (redirect-frame-focus frame-initial-frame new)
422 ;; Finally, get rid of the old frame.
423 (delete-frame frame-initial-frame t))
425 ;; Otherwise, we don't need all that rigmarole; just apply
426 ;; the new parameters.
427 (let (newparms allparms tail)
428 (setq allparms (append initial-frame-alist
429 window-system-frame-alist
430 default-frame-alist nil))
431 (if (assq 'height frame-initial-geometry-arguments)
432 (setq allparms (assq-delete-all 'height allparms)))
433 (if (assq 'width frame-initial-geometry-arguments)
434 (setq allparms (assq-delete-all 'width allparms)))
435 (if (assq 'left frame-initial-geometry-arguments)
436 (setq allparms (assq-delete-all 'left allparms)))
437 (if (assq 'top frame-initial-geometry-arguments)
438 (setq allparms (assq-delete-all 'top allparms)))
439 (setq tail allparms)
440 ;; Find just the parms that have changed since we first
441 ;; made this frame. Those are the ones actually set by
442 ;; the init file. For those parms whose values we already knew
443 ;; (such as those spec'd by command line options)
444 ;; it is undesirable to specify the parm again
445 ;; once the user has seen the frame and been able to alter it
446 ;; manually.
447 (let (newval oldval)
448 (dolist (entry tail)
449 (setq oldval (assq (car entry) frame-initial-frame-alist))
450 (setq newval (cdr (assq (car entry) allparms)))
451 (or (and oldval (eq (cdr oldval) newval))
452 (setq newparms
453 (cons (cons (car entry) newval) newparms)))))
454 (setq newparms (nreverse newparms))
456 (let ((new-bg (assq 'background-color newparms)))
457 ;; If the `background-color' parameter is changed, apply
458 ;; it first, then make sure that the `background-mode'
459 ;; parameter and other faces are updated, before applying
460 ;; the other parameters.
461 (when new-bg
462 (modify-frame-parameters frame-initial-frame
463 (list new-bg))
464 (unless (assq 'background-mode newparms)
465 (frame-set-background-mode frame-initial-frame))
466 (face-set-after-frame-default frame-initial-frame)
467 (setq newparms (delq new-bg newparms)))
469 (when (numberp (car frame-size-history))
470 (setq frame-size-history
471 (cons (1- (car frame-size-history))
472 (cons
473 (list frame-initial-frame
474 "frame-notice-user-settings"
475 nil newparms)
476 (cdr frame-size-history)))))
478 (modify-frame-parameters frame-initial-frame newparms)))))
480 ;; Restore the original buffer.
481 (set-buffer old-buffer)
483 ;; Make sure the initial frame can be GC'd if it is ever deleted.
484 ;; Make sure frame-notice-user-settings does nothing if called twice.
485 (setq frame-notice-user-settings nil)
486 (setq frame-initial-frame nil)))
488 (defun make-initial-minibuffer-frame (display)
489 (let ((parms (append minibuffer-frame-alist '((minibuffer . only)))))
490 (if display
491 (make-frame-on-display display parms)
492 (make-frame parms))))
494 ;;;; Creation of additional frames, and other frame miscellanea
496 (defun modify-all-frames-parameters (alist)
497 "Modify all current and future frames' parameters according to ALIST.
498 This changes `default-frame-alist' and possibly `initial-frame-alist'.
499 Furthermore, this function removes all parameters in ALIST from
500 `window-system-default-frame-alist'.
501 See help of `modify-frame-parameters' for more information."
502 (dolist (frame (frame-list))
503 (modify-frame-parameters frame alist))
505 (dolist (pair alist) ;; conses to add/replace
506 ;; initial-frame-alist needs setting only when
507 ;; frame-notice-user-settings is true.
508 (and frame-notice-user-settings
509 (setq initial-frame-alist
510 (assq-delete-all (car pair) initial-frame-alist)))
511 (setq default-frame-alist
512 (assq-delete-all (car pair) default-frame-alist))
513 ;; Remove any similar settings from the window-system specific
514 ;; parameters---they would override default-frame-alist.
515 (dolist (w window-system-default-frame-alist)
516 (setcdr w (assq-delete-all (car pair) (cdr w)))))
518 (and frame-notice-user-settings
519 (setq initial-frame-alist (append initial-frame-alist alist)))
520 (setq default-frame-alist (append default-frame-alist alist)))
522 (defun get-other-frame ()
523 "Return some frame other than the current frame.
524 Create one if necessary. Note that the minibuffer frame, if separate,
525 is not considered (see `next-frame')."
526 (if (equal (next-frame) (selected-frame)) (make-frame) (next-frame)))
528 (defun next-multiframe-window ()
529 "Select the next window, regardless of which frame it is on."
530 (interactive)
531 (select-window (next-window (selected-window)
532 (> (minibuffer-depth) 0)
534 (select-frame-set-input-focus (selected-frame)))
536 (defun previous-multiframe-window ()
537 "Select the previous window, regardless of which frame it is on."
538 (interactive)
539 (select-window (previous-window (selected-window)
540 (> (minibuffer-depth) 0)
542 (select-frame-set-input-focus (selected-frame)))
544 (defun window-system-for-display (display)
545 "Return the window system for DISPLAY.
546 Return nil if we don't know how to interpret DISPLAY."
547 ;; MS-Windows doesn't know how to create a GUI frame in a -nw session.
548 (if (and (eq system-type 'windows-nt)
549 (null (window-system))
550 (not (daemonp)))
552 (cl-loop for descriptor in display-format-alist
553 for pattern = (car descriptor)
554 for system = (cdr descriptor)
555 when (string-match-p pattern display) return system)))
557 (defun make-frame-on-display (display &optional parameters)
558 "Make a frame on display DISPLAY.
559 The optional argument PARAMETERS specifies additional frame parameters."
560 (interactive "sMake frame on display: ")
561 (make-frame (cons (cons 'display display) parameters)))
563 (declare-function x-close-connection "xfns.c" (terminal))
565 (defun close-display-connection (display)
566 "Close the connection to a display, deleting all its associated frames.
567 For DISPLAY, specify either a frame or a display name (a string).
568 If DISPLAY is nil, that stands for the selected frame's display."
569 (interactive
570 (list
571 (let* ((default (frame-parameter nil 'display))
572 (display (completing-read
573 (format "Close display (default %s): " default)
574 (delete-dups
575 (mapcar (lambda (frame)
576 (frame-parameter frame 'display))
577 (frame-list)))
578 nil t nil nil
579 default)))
580 (if (zerop (length display)) default display))))
581 (let ((frames (delq nil
582 (mapcar (lambda (frame)
583 (if (equal display
584 (frame-parameter frame 'display))
585 frame))
586 (frame-list)))))
587 (if (and (consp frames)
588 (not (y-or-n-p (if (cdr frames)
589 (format "Delete %s frames? " (length frames))
590 (format "Delete %s ? " (car frames))))))
591 (error "Abort!")
592 (mapc 'delete-frame frames)
593 (x-close-connection display))))
595 (defun make-frame-command ()
596 "Make a new frame, on the same terminal as the selected frame.
597 If the terminal is a text-only terminal, this also selects the
598 new frame."
599 (interactive)
600 (if (display-graphic-p)
601 (make-frame)
602 (select-frame (make-frame))))
604 (defvar before-make-frame-hook nil
605 "Functions to run before a frame is created.")
607 (defvar after-make-frame-functions nil
608 "Functions to run after a frame is created.
609 The functions are run with one arg, the newly created frame.")
611 (defvar after-setting-font-hook nil
612 "Functions to run after a frame's font has been changed.")
614 ;; Alias, kept temporarily.
615 (define-obsolete-function-alias 'new-frame 'make-frame "22.1")
617 (defvar frame-inherited-parameters '()
618 "Parameters `make-frame' copies from the `selected-frame' to the new frame.")
620 (defvar x-display-name)
622 (defun make-frame (&optional parameters)
623 "Return a newly created frame displaying the current buffer.
624 Optional argument PARAMETERS is an alist of frame parameters for
625 the new frame. Each element of PARAMETERS should have the
626 form (NAME . VALUE), for example:
628 (name . STRING) The frame should be named STRING.
630 (width . NUMBER) The frame should be NUMBER characters in width.
631 (height . NUMBER) The frame should be NUMBER text lines high.
633 You cannot specify either `width' or `height', you must specify
634 neither or both.
636 (minibuffer . t) The frame should have a minibuffer.
637 (minibuffer . nil) The frame should have no minibuffer.
638 (minibuffer . only) The frame should contain only a minibuffer.
639 (minibuffer . WINDOW) The frame should use WINDOW as its minibuffer window.
641 (window-system . nil) The frame should be displayed on a terminal device.
642 (window-system . x) The frame should be displayed in an X window.
644 (display . \":0\") The frame should appear on display :0.
646 (terminal . TERMINAL) The frame should use the terminal object TERMINAL.
648 In addition, any parameter specified in `default-frame-alist',
649 but not present in PARAMETERS, is applied.
651 Before creating the frame (via `frame-creation-function-alist'),
652 this function runs the hook `before-make-frame-hook'. After
653 creating the frame, it runs the hook `after-make-frame-functions'
654 with one arg, the newly created frame.
656 If a display parameter is supplied and a window-system is not,
657 guess the window-system from the display.
659 On graphical displays, this function does not itself make the new
660 frame the selected frame. However, the window system may select
661 the new frame according to its own rules."
662 (interactive)
663 (let* ((display (cdr (assq 'display parameters)))
664 (w (cond
665 ((assq 'terminal parameters)
666 (let ((type (terminal-live-p
667 (cdr (assq 'terminal parameters)))))
668 (cond
669 ((eq t type) nil)
670 ((null type) (error "Terminal %s does not exist"
671 (cdr (assq 'terminal parameters))))
672 (t type))))
673 ((assq 'window-system parameters)
674 (cdr (assq 'window-system parameters)))
675 (display
676 (or (window-system-for-display display)
677 (error "Don't know how to interpret display %S"
678 display)))
679 (t window-system)))
680 (oldframe (selected-frame))
681 (params parameters)
682 frame)
684 (unless (get w 'window-system-initialized)
685 (funcall (gui-method window-system-initialization w) display)
686 (setq x-display-name display)
687 (put w 'window-system-initialized t))
689 ;; Add parameters from `window-system-default-frame-alist'.
690 (dolist (p (cdr (assq w window-system-default-frame-alist)))
691 (unless (assq (car p) params)
692 (push p params)))
693 ;; Add parameters from `default-frame-alist'.
694 (dolist (p default-frame-alist)
695 (unless (assq (car p) params)
696 (push p params)))
697 ;; Now make the frame.
698 (run-hooks 'before-make-frame-hook)
700 ;; (setq frame-size-history '(1000))
702 (setq frame
703 (funcall (gui-method frame-creation-function w) params))
704 (normal-erase-is-backspace-setup-frame frame)
705 ;; Inherit the original frame's parameters.
706 (dolist (param frame-inherited-parameters)
707 (unless (assq param parameters) ;Overridden by explicit parameters.
708 (let ((val (frame-parameter oldframe param)))
709 (when val (set-frame-parameter frame param val)))))
711 (when (numberp (car frame-size-history))
712 (setq frame-size-history
713 (cons (1- (car frame-size-history))
714 (cons (list frame "make-frame")
715 (cdr frame-size-history)))))
717 ;; We can run `window-configuration-change-hook' for this frame now.
718 (frame-after-make-frame frame t)
719 (run-hook-with-args 'after-make-frame-functions frame)
720 frame))
722 (defun filtered-frame-list (predicate)
723 "Return a list of all live frames which satisfy PREDICATE."
724 (let* ((frames (frame-list))
725 (list frames))
726 (while (consp frames)
727 (unless (funcall predicate (car frames))
728 (setcar frames nil))
729 (setq frames (cdr frames)))
730 (delq nil list)))
732 (defun minibuffer-frame-list ()
733 "Return a list of all frames with their own minibuffers."
734 (filtered-frame-list
735 (lambda (frame)
736 (eq frame (window-frame (minibuffer-window frame))))))
738 ;; Used to be called `terminal-id' in termdev.el.
739 (defun get-device-terminal (device)
740 "Return the terminal corresponding to DEVICE.
741 DEVICE can be a terminal, a frame, nil (meaning the selected frame's terminal),
742 the name of an X display device (HOST.SERVER.SCREEN) or a tty device file."
743 (cond
744 ((or (null device) (framep device))
745 (frame-terminal device))
746 ((stringp device)
747 (let ((f (car (filtered-frame-list
748 (lambda (frame)
749 (or (equal (frame-parameter frame 'display) device)
750 (equal (frame-parameter frame 'tty) device)))))))
751 (or f (error "Display %s does not exist" device))
752 (frame-terminal f)))
753 ((terminal-live-p device) device)
755 (error "Invalid argument %s in `get-device-terminal'" device))))
757 (defun frames-on-display-list (&optional device)
758 "Return a list of all frames on DEVICE.
760 DEVICE should be a terminal, a frame,
761 or a name of an X display or tty (a string of the form
762 HOST:SERVER.SCREEN).
764 If DEVICE is omitted or nil, it defaults to the selected
765 frame's terminal device."
766 (let* ((terminal (get-device-terminal device))
767 (func #'(lambda (frame)
768 (eq (frame-terminal frame) terminal))))
769 (filtered-frame-list func)))
771 (defun framep-on-display (&optional terminal)
772 "Return the type of frames on TERMINAL.
773 TERMINAL may be a terminal id, a display name or a frame. If it
774 is a frame, its type is returned. If TERMINAL is omitted or nil,
775 it defaults to the selected frame's terminal device. All frames
776 on a given display are of the same type."
777 (or (terminal-live-p terminal)
778 (framep terminal)
779 (framep (car (frames-on-display-list terminal)))))
781 (defun frame-remove-geometry-params (param-list)
782 "Return the parameter list PARAM-LIST, but with geometry specs removed.
783 This deletes all bindings in PARAM-LIST for `top', `left', `width',
784 `height', `user-size' and `user-position' parameters.
785 Emacs uses this to avoid overriding explicit moves and resizings from
786 the user during startup."
787 (setq param-list (cons nil param-list))
788 (let ((tail param-list))
789 (while (consp (cdr tail))
790 (if (and (consp (car (cdr tail)))
791 (memq (car (car (cdr tail)))
792 '(height width top left user-position user-size)))
793 (progn
794 (setq frame-initial-geometry-arguments
795 (cons (car (cdr tail)) frame-initial-geometry-arguments))
796 (setcdr tail (cdr (cdr tail))))
797 (setq tail (cdr tail)))))
798 (setq frame-initial-geometry-arguments
799 (nreverse frame-initial-geometry-arguments))
800 (cdr param-list))
802 (declare-function x-focus-frame "frame.c" (frame))
804 (defun select-frame-set-input-focus (frame &optional norecord)
805 "Select FRAME, raise it, and set input focus, if possible.
806 If `mouse-autoselect-window' is non-nil, also move mouse pointer
807 to FRAME's selected window. Otherwise, if `focus-follows-mouse'
808 is non-nil, move mouse cursor to FRAME.
810 Optional argument NORECORD means to neither change the order of
811 recently selected windows nor the buffer list."
812 (select-frame frame norecord)
813 (raise-frame frame)
814 ;; Ensure, if possible, that FRAME gets input focus.
815 (when (memq (window-system frame) '(x w32 ns))
816 (x-focus-frame frame))
817 ;; Move mouse cursor if necessary.
818 (cond
819 (mouse-autoselect-window
820 (let ((edges (window-inside-edges (frame-selected-window frame))))
821 ;; Move mouse cursor into FRAME's selected window to avoid that
822 ;; Emacs mouse-autoselects another window.
823 (set-mouse-position frame (nth 2 edges) (nth 1 edges))))
824 (focus-follows-mouse
825 ;; Move mouse cursor into FRAME to avoid that another frame gets
826 ;; selected by the window manager.
827 (set-mouse-position frame (1- (frame-width frame)) 0))))
829 (defun other-frame (arg)
830 "Select the ARGth different visible frame on current display, and raise it.
831 All frames are arranged in a cyclic order.
832 This command selects the frame ARG steps away in that order.
833 A negative ARG moves in the opposite order.
835 To make this command work properly, you must tell Emacs
836 how the system (or the window manager) generally handles
837 focus-switching between windows. If moving the mouse onto a window
838 selects it (gives it focus), set `focus-follows-mouse' to t.
839 Otherwise, that variable should be nil."
840 (interactive "p")
841 (let ((frame (selected-frame)))
842 (while (> arg 0)
843 (setq frame (next-frame frame))
844 (while (not (eq (frame-visible-p frame) t))
845 (setq frame (next-frame frame)))
846 (setq arg (1- arg)))
847 (while (< arg 0)
848 (setq frame (previous-frame frame))
849 (while (not (eq (frame-visible-p frame) t))
850 (setq frame (previous-frame frame)))
851 (setq arg (1+ arg)))
852 (select-frame-set-input-focus frame)))
854 (defun iconify-or-deiconify-frame ()
855 "Iconify the selected frame, or deiconify if it's currently an icon."
856 (interactive)
857 (if (eq (cdr (assq 'visibility (frame-parameters))) t)
858 (iconify-frame)
859 (make-frame-visible)))
861 (defun suspend-frame ()
862 "Do whatever is right to suspend the current frame.
863 Calls `suspend-emacs' if invoked from the controlling tty device,
864 `suspend-tty' from a secondary tty device, and
865 `iconify-or-deiconify-frame' from an X frame."
866 (interactive)
867 (let ((type (framep (selected-frame))))
868 (cond
869 ((memq type '(x ns w32)) (iconify-or-deiconify-frame))
870 ((eq type t)
871 (if (controlling-tty-p)
872 (suspend-emacs)
873 (suspend-tty)))
874 (t (suspend-emacs)))))
876 (defun make-frame-names-alist ()
877 ;; Only consider the frames on the same display.
878 (let* ((current-frame (selected-frame))
879 (falist
880 (cons
881 (cons (frame-parameter current-frame 'name) current-frame) nil))
882 (frame (next-frame nil 0)))
883 (while (not (eq frame current-frame))
884 (progn
885 (push (cons (frame-parameter frame 'name) frame) falist)
886 (setq frame (next-frame frame 0))))
887 falist))
889 (defvar frame-name-history nil)
890 (defun select-frame-by-name (name)
891 "Select the frame on the current terminal whose name is NAME and raise it.
892 If there is no frame by that name, signal an error."
893 (interactive
894 (let* ((frame-names-alist (make-frame-names-alist))
895 (default (car (car frame-names-alist)))
896 (input (completing-read
897 (format "Select Frame (default %s): " default)
898 frame-names-alist nil t nil 'frame-name-history)))
899 (if (= (length input) 0)
900 (list default)
901 (list input))))
902 (let* ((frame-names-alist (make-frame-names-alist))
903 (frame (cdr (assoc name frame-names-alist))))
904 (if frame
905 (select-frame-set-input-focus frame)
906 (error "There is no frame named `%s'" name))))
909 ;;;; Background mode.
911 (defcustom frame-background-mode nil
912 "The brightness of the background.
913 Set this to the symbol `dark' if your background color is dark,
914 `light' if your background is light, or nil (automatic by default)
915 if you want Emacs to examine the brightness for you.
917 If you change this without using customize, you should use
918 `frame-set-background-mode' to update existing frames;
919 e.g. (mapc 'frame-set-background-mode (frame-list))."
920 :group 'faces
921 :set #'(lambda (var value)
922 (set-default var value)
923 (mapc 'frame-set-background-mode (frame-list)))
924 :initialize 'custom-initialize-changed
925 :type '(choice (const dark)
926 (const light)
927 (const :tag "automatic" nil)))
929 (declare-function x-get-resource "frame.c"
930 (attribute class &optional component subclass))
932 ;; Only used if window-system is not null.
933 (declare-function x-display-grayscale-p "xfns.c" (&optional terminal))
935 (defvar inhibit-frame-set-background-mode nil)
937 (defun frame-set-background-mode (frame &optional keep-face-specs)
938 "Set up display-dependent faces on FRAME.
939 Display-dependent faces are those which have different definitions
940 according to the `background-mode' and `display-type' frame parameters.
942 If optional arg KEEP-FACE-SPECS is non-nil, don't recalculate
943 face specs for the new background mode."
944 (unless inhibit-frame-set-background-mode
945 (let* ((frame-default-bg-mode (frame-terminal-default-bg-mode frame))
946 (bg-color (frame-parameter frame 'background-color))
947 (tty-type (tty-type frame))
948 (default-bg-mode
949 (if (or (window-system frame)
950 (and tty-type
951 (string-match "^\\(xterm\\|\\rxvt\\|dtterm\\|eterm\\)"
952 tty-type)))
953 'light
954 'dark))
955 (non-default-bg-mode (if (eq default-bg-mode 'light) 'dark 'light))
956 (bg-mode
957 (cond (frame-default-bg-mode)
958 ((equal bg-color "unspecified-fg") ; inverted colors
959 non-default-bg-mode)
960 ((not (color-values bg-color frame))
961 default-bg-mode)
962 ((>= (apply '+ (color-values bg-color frame))
963 ;; Just looking at the screen, colors whose
964 ;; values add up to .6 of the white total
965 ;; still look dark to me.
966 (* (apply '+ (color-values "white" frame)) .6))
967 'light)
968 (t 'dark)))
969 (display-type
970 (cond ((null (window-system frame))
971 (if (tty-display-color-p frame) 'color 'mono))
972 ((display-color-p frame)
973 'color)
974 ((x-display-grayscale-p frame)
975 'grayscale)
976 (t 'mono)))
977 (old-bg-mode
978 (frame-parameter frame 'background-mode))
979 (old-display-type
980 (frame-parameter frame 'display-type)))
982 (unless (and (eq bg-mode old-bg-mode) (eq display-type old-display-type))
983 (let ((locally-modified-faces nil)
984 ;; Prevent face-spec-recalc from calling this function
985 ;; again, resulting in a loop (bug#911).
986 (inhibit-frame-set-background-mode t)
987 (params (list (cons 'background-mode bg-mode)
988 (cons 'display-type display-type))))
989 (if keep-face-specs
990 (modify-frame-parameters frame params)
991 ;; If we are recomputing face specs, first collect a list
992 ;; of faces that don't match their face-specs. These are
993 ;; the faces modified on FRAME, and we avoid changing them
994 ;; below. Use a negative list to avoid consing (we assume
995 ;; most faces are unmodified).
996 (dolist (face (face-list))
997 (and (not (get face 'face-override-spec))
998 (not (face-spec-match-p face
999 (face-user-default-spec face)
1000 (selected-frame)))
1001 (push face locally-modified-faces)))
1002 ;; Now change to the new frame parameters
1003 (modify-frame-parameters frame params)
1004 ;; For all unmodified named faces, choose face specs
1005 ;; matching the new frame parameters.
1006 (dolist (face (face-list))
1007 (unless (memq face locally-modified-faces)
1008 (face-spec-recalc face frame)))))))))
1010 (defun frame-terminal-default-bg-mode (frame)
1011 "Return the default background mode of FRAME.
1012 This checks the `frame-background-mode' variable, the X resource
1013 named \"backgroundMode\" (if FRAME is an X frame), and finally
1014 the `background-mode' terminal parameter."
1015 (or frame-background-mode
1016 (let ((bg-resource
1017 (and (window-system frame)
1018 (x-get-resource "backgroundMode" "BackgroundMode"))))
1019 (if bg-resource
1020 (intern (downcase bg-resource))))
1021 (terminal-parameter frame 'background-mode)))
1024 ;;;; Frame configurations
1026 (defun current-frame-configuration ()
1027 "Return a list describing the positions and states of all frames.
1028 Its car is `frame-configuration'.
1029 Each element of the cdr is a list of the form (FRAME ALIST WINDOW-CONFIG),
1030 where
1031 FRAME is a frame object,
1032 ALIST is an association list specifying some of FRAME's parameters, and
1033 WINDOW-CONFIG is a window configuration object for FRAME."
1034 (cons 'frame-configuration
1035 (mapcar (lambda (frame)
1036 (list frame
1037 (frame-parameters frame)
1038 (current-window-configuration frame)))
1039 (frame-list))))
1041 (defun set-frame-configuration (configuration &optional nodelete)
1042 "Restore the frames to the state described by CONFIGURATION.
1043 Each frame listed in CONFIGURATION has its position, size, window
1044 configuration, and other parameters set as specified in CONFIGURATION.
1045 However, this function does not restore deleted frames.
1047 Ordinarily, this function deletes all existing frames not
1048 listed in CONFIGURATION. But if optional second argument NODELETE
1049 is given and non-nil, the unwanted frames are iconified instead."
1050 (or (frame-configuration-p configuration)
1051 (signal 'wrong-type-argument
1052 (list 'frame-configuration-p configuration)))
1053 (let ((config-alist (cdr configuration))
1054 frames-to-delete)
1055 (dolist (frame (frame-list))
1056 (let ((parameters (assq frame config-alist)))
1057 (if parameters
1058 (progn
1059 (modify-frame-parameters
1060 frame
1061 ;; Since we can't set a frame's minibuffer status,
1062 ;; we might as well omit the parameter altogether.
1063 (let* ((parms (nth 1 parameters))
1064 (mini (assq 'minibuffer parms))
1065 (name (assq 'name parms))
1066 (explicit-name (cdr (assq 'explicit-name parms))))
1067 (when mini (setq parms (delq mini parms)))
1068 ;; Leave name in iff it was set explicitly.
1069 ;; This should fix the behavior reported in
1070 ;; http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg01632.html
1071 (when (and name (not explicit-name))
1072 (setq parms (delq name parms)))
1073 parms))
1074 (set-window-configuration (nth 2 parameters)))
1075 (setq frames-to-delete (cons frame frames-to-delete)))))
1076 (mapc (if nodelete
1077 ;; Note: making frames invisible here was tried
1078 ;; but led to some strange behavior--each time the frame
1079 ;; was made visible again, the window manager asked afresh
1080 ;; for where to put it.
1081 'iconify-frame
1082 'delete-frame)
1083 frames-to-delete)))
1085 ;;;; Convenience functions for accessing and interactively changing
1086 ;;;; frame parameters.
1088 (defun frame-height (&optional frame)
1089 "Return number of lines available for display on FRAME.
1090 If FRAME is omitted, describe the currently selected frame.
1091 Exactly what is included in the return value depends on the
1092 window-system and toolkit in use - see `frame-pixel-height' for
1093 more details. The lines are in units of the default font height.
1095 The result is roughly related to the frame pixel height via
1096 height in pixels = height in lines * `frame-char-height'.
1097 However, this is only approximate, and is complicated e.g. by the
1098 fact that individual window lines and menu bar lines can have
1099 differing font heights."
1100 (cdr (assq 'height (frame-parameters frame))))
1102 (defun frame-width (&optional frame)
1103 "Return number of columns available for display on FRAME.
1104 If FRAME is omitted, describe the currently selected frame."
1105 (cdr (assq 'width (frame-parameters frame))))
1107 (declare-function x-list-fonts "xfaces.c"
1108 (pattern &optional face frame maximum width))
1110 (define-obsolete-function-alias 'set-default-font 'set-frame-font "23.1")
1112 (defun set-frame-font (font &optional keep-size frames)
1113 "Set the default font to FONT.
1114 When called interactively, prompt for the name of a font, and use
1115 that font on the selected frame. When called from Lisp, FONT
1116 should be a font name (a string), a font object, font entity, or
1117 font spec.
1119 If KEEP-SIZE is nil, keep the number of frame lines and columns
1120 fixed. If KEEP-SIZE is non-nil (or with a prefix argument), try
1121 to keep the current frame size fixed (in pixels) by adjusting the
1122 number of lines and columns.
1124 If FRAMES is nil, apply the font to the selected frame only.
1125 If FRAMES is non-nil, it should be a list of frames to act upon,
1126 or t meaning all existing graphical frames.
1127 Also, if FRAMES is non-nil, alter the user's Customization settings
1128 as though the font-related attributes of the `default' face had been
1129 \"set in this session\", so that the font is applied to future frames."
1130 (interactive
1131 (let* ((completion-ignore-case t)
1132 (font (completing-read "Font name: "
1133 ;; x-list-fonts will fail with an error
1134 ;; if this frame doesn't support fonts.
1135 (x-list-fonts "*" nil (selected-frame))
1136 nil nil nil nil
1137 (frame-parameter nil 'font))))
1138 (list font current-prefix-arg nil)))
1139 (when (or (stringp font) (fontp font))
1140 (let* ((this-frame (selected-frame))
1141 ;; FRAMES nil means affect the selected frame.
1142 (frame-list (cond ((null frames)
1143 (list this-frame))
1144 ((eq frames t)
1145 (frame-list))
1146 (t frames)))
1147 height width)
1148 (dolist (f frame-list)
1149 (when (display-multi-font-p f)
1150 (if keep-size
1151 (setq height (* (frame-parameter f 'height)
1152 (frame-char-height f))
1153 width (* (frame-parameter f 'width)
1154 (frame-char-width f))))
1155 ;; When set-face-attribute is called for :font, Emacs
1156 ;; guesses the best font according to other face attributes
1157 ;; (:width, :weight, etc.) so reset them too (Bug#2476).
1158 (set-face-attribute 'default f
1159 :width 'normal :weight 'normal
1160 :slant 'normal :font font)
1161 (if keep-size
1162 (modify-frame-parameters
1164 (list (cons 'height (round height (frame-char-height f)))
1165 (cons 'width (round width (frame-char-width f))))))))
1166 (when frames
1167 ;; Alter the user's Custom setting of the `default' face, but
1168 ;; only for font-related attributes.
1169 (let ((specs (cadr (assq 'user (get 'default 'theme-face))))
1170 (attrs '(:family :foundry :slant :weight :height :width))
1171 (new-specs nil))
1172 (if (null specs) (setq specs '((t nil))))
1173 (dolist (spec specs)
1174 ;; Each SPEC has the form (DISPLAY ATTRIBUTE-PLIST)
1175 (let ((display (nth 0 spec))
1176 (plist (copy-tree (nth 1 spec))))
1177 ;; Alter only DISPLAY conditions matching this frame.
1178 (when (or (memq display '(t default))
1179 (face-spec-set-match-display display this-frame))
1180 (dolist (attr attrs)
1181 (setq plist (plist-put plist attr
1182 (face-attribute 'default attr)))))
1183 (push (list display plist) new-specs)))
1184 (setq new-specs (nreverse new-specs))
1185 (put 'default 'customized-face new-specs)
1186 (custom-push-theme 'theme-face 'default 'user 'set new-specs)
1187 (put 'default 'face-modified nil))))
1188 (run-hooks 'after-setting-font-hook 'after-setting-font-hooks)))
1190 (defun set-frame-parameter (frame parameter value)
1191 "Set frame parameter PARAMETER to VALUE on FRAME.
1192 If FRAME is nil, it defaults to the selected frame.
1193 See `modify-frame-parameters'."
1194 (modify-frame-parameters frame (list (cons parameter value))))
1196 (defun set-background-color (color-name)
1197 "Set the background color of the selected frame to COLOR-NAME.
1198 When called interactively, prompt for the name of the color to use.
1199 To get the frame's current background color, use `frame-parameters'."
1200 (interactive (list (read-color "Background color: ")))
1201 (modify-frame-parameters (selected-frame)
1202 (list (cons 'background-color color-name)))
1203 (or window-system
1204 (face-set-after-frame-default (selected-frame))))
1206 (defun set-foreground-color (color-name)
1207 "Set the foreground color of the selected frame to COLOR-NAME.
1208 When called interactively, prompt for the name of the color to use.
1209 To get the frame's current foreground color, use `frame-parameters'."
1210 (interactive (list (read-color "Foreground color: ")))
1211 (modify-frame-parameters (selected-frame)
1212 (list (cons 'foreground-color color-name)))
1213 (or window-system
1214 (face-set-after-frame-default (selected-frame))))
1216 (defun set-cursor-color (color-name)
1217 "Set the text cursor color of the selected frame to COLOR-NAME.
1218 When called interactively, prompt for the name of the color to use.
1219 This works by setting the `cursor-color' frame parameter on the
1220 selected frame.
1222 You can also set the text cursor color, for all frames, by
1223 customizing the `cursor' face."
1224 (interactive (list (read-color "Cursor color: ")))
1225 (modify-frame-parameters (selected-frame)
1226 (list (cons 'cursor-color color-name))))
1228 (defun set-mouse-color (color-name)
1229 "Set the color of the mouse pointer of the selected frame to COLOR-NAME.
1230 When called interactively, prompt for the name of the color to use.
1231 To get the frame's current mouse color, use `frame-parameters'."
1232 (interactive (list (read-color "Mouse color: ")))
1233 (modify-frame-parameters (selected-frame)
1234 (list (cons 'mouse-color
1235 (or color-name
1236 (cdr (assq 'mouse-color
1237 (frame-parameters))))))))
1239 (defun set-border-color (color-name)
1240 "Set the color of the border of the selected frame to COLOR-NAME.
1241 When called interactively, prompt for the name of the color to use.
1242 To get the frame's current border color, use `frame-parameters'."
1243 (interactive (list (read-color "Border color: ")))
1244 (modify-frame-parameters (selected-frame)
1245 (list (cons 'border-color color-name))))
1247 (define-minor-mode auto-raise-mode
1248 "Toggle whether or not selected frames should auto-raise.
1249 With a prefix argument ARG, enable Auto Raise mode if ARG is
1250 positive, and disable it otherwise. If called from Lisp, enable
1251 the mode if ARG is omitted or nil.
1253 Auto Raise mode does nothing under most window managers, which
1254 switch focus on mouse clicks. It only has an effect if your
1255 window manager switches focus on mouse movement (in which case
1256 you should also change `focus-follows-mouse' to t). Then,
1257 enabling Auto Raise mode causes any graphical Emacs frame which
1258 acquires focus to be automatically raised.
1260 Note that this minor mode controls Emacs's own auto-raise
1261 feature. Window managers that switch focus on mouse movement
1262 often have their own auto-raise feature."
1263 :variable (frame-parameter nil 'auto-raise)
1264 (if (frame-parameter nil 'auto-raise)
1265 (raise-frame)))
1267 (define-minor-mode auto-lower-mode
1268 "Toggle whether or not the selected frame should auto-lower.
1269 With a prefix argument ARG, enable Auto Lower 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 Lower 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 Lower Mode causes any graphical Emacs frame which
1278 loses focus to be automatically lowered.
1280 Note that this minor mode controls Emacs's own auto-lower
1281 feature. Window managers that switch focus on mouse movement
1282 often have their own features for raising or lowering frames."
1283 :variable (frame-parameter nil 'auto-lower))
1285 (defun set-frame-name (name)
1286 "Set the name of the selected frame to NAME.
1287 When called interactively, prompt for the name of the frame.
1288 On text terminals, the frame name is displayed on the mode line.
1289 On graphical displays, it is displayed on the frame's title bar."
1290 (interactive "sFrame name: ")
1291 (modify-frame-parameters (selected-frame)
1292 (list (cons 'name name))))
1294 (defun frame-current-scroll-bars (&optional frame)
1295 "Return the current scroll-bar types for frame FRAME.
1296 Value is a cons (VERTICAL . HORIZ0NTAL) where VERTICAL specifies
1297 the current location of the vertical scroll-bars (`left', `right'
1298 or nil), and HORIZONTAL specifies the current location of the
1299 horizontal scroll bars (`bottom' or nil). FRAME must specify a
1300 live frame and defaults to the selected one."
1301 (let* ((frame (window-normalize-frame frame))
1302 (vertical (frame-parameter frame 'vertical-scroll-bars))
1303 (horizontal (frame-parameter frame 'horizontal-scroll-bars)))
1304 (unless (memq vertical '(left right nil))
1305 (setq vertical default-frame-scroll-bars))
1306 (cons vertical (and horizontal 'bottom))))
1308 (defun frame-monitor-attributes (&optional frame)
1309 "Return the attributes of the physical monitor dominating FRAME.
1310 If FRAME is omitted or nil, describe the currently selected frame.
1312 A frame is dominated by a physical monitor when either the
1313 largest area of the frame resides in the monitor, or the monitor
1314 is the closest to the frame if the frame does not intersect any
1315 physical monitors.
1317 See `display-monitor-attributes-list' for the list of attribute
1318 keys and their meanings."
1319 (or frame (setq frame (selected-frame)))
1320 (cl-loop for attributes in (display-monitor-attributes-list frame)
1321 for frames = (cdr (assq 'frames attributes))
1322 if (memq frame frames) return attributes))
1325 ;;;; Frame/display capabilities.
1327 (declare-function msdos-mouse-p "dosfns.c")
1329 (defun display-mouse-p (&optional display)
1330 "Return non-nil if DISPLAY has a mouse available.
1331 DISPLAY can be a display name, a frame, or nil (meaning the selected
1332 frame's display)."
1333 (let ((frame-type (framep-on-display display)))
1334 (cond
1335 ((eq frame-type 'pc)
1336 (msdos-mouse-p))
1337 ((eq frame-type 'w32)
1338 (with-no-warnings
1339 (> w32-num-mouse-buttons 0)))
1340 ((memq frame-type '(x ns))
1341 t) ;; We assume X and NeXTstep *always* have a pointing device
1343 (or (and (featurep 'xt-mouse)
1344 xterm-mouse-mode)
1345 ;; t-mouse is distributed with the GPM package. It doesn't have
1346 ;; a toggle.
1347 (featurep 't-mouse)
1348 ;; No way to check whether a w32 console has a mouse, assume
1349 ;; it always does.
1350 (boundp 'w32-use-full-screen-buffer))))))
1352 (defun display-popup-menus-p (&optional display)
1353 "Return non-nil if popup menus are supported on DISPLAY.
1354 DISPLAY can be a display name, a frame, or nil (meaning the selected
1355 frame's display).
1356 Support for popup menus requires that the mouse be available."
1357 (display-mouse-p display))
1359 (defun display-graphic-p (&optional display)
1360 "Return non-nil if DISPLAY is a graphic display.
1361 Graphical displays are those which are capable of displaying several
1362 frames and several different fonts at once. This is true for displays
1363 that use a window system such as X, and false for text-only terminals.
1364 DISPLAY can be a display name, a frame, or nil (meaning the selected
1365 frame's display)."
1366 (not (null (memq (framep-on-display display) '(x w32 ns)))))
1368 (defun display-images-p (&optional display)
1369 "Return non-nil if DISPLAY can display images.
1371 DISPLAY can be a display name, a frame, or nil (meaning the selected
1372 frame's display)."
1373 (and (display-graphic-p display)
1374 (fboundp 'image-mask-p)
1375 (fboundp 'image-size)))
1377 (defalias 'display-multi-frame-p 'display-graphic-p)
1378 (defalias 'display-multi-font-p 'display-graphic-p)
1380 (defun display-selections-p (&optional display)
1381 "Return non-nil if DISPLAY supports selections.
1382 A selection is a way to transfer text or other data between programs
1383 via special system buffers called `selection' or `clipboard'.
1384 DISPLAY can be a display name, a frame, or nil (meaning the selected
1385 frame's display)."
1386 (let ((frame-type (framep-on-display display)))
1387 (cond
1388 ((eq frame-type 'pc)
1389 ;; MS-DOS frames support selections when Emacs runs inside
1390 ;; a Windows DOS Box.
1391 (with-no-warnings
1392 (not (null dos-windows-version))))
1393 ((memq frame-type '(x w32 ns))
1396 nil))))
1398 (declare-function x-display-screens "xfns.c" (&optional terminal))
1400 (defun display-screens (&optional display)
1401 "Return the number of screens associated with DISPLAY.
1402 DISPLAY should be either a frame or a display name (a string).
1403 If DISPLAY is omitted or nil, it defaults to the selected frame's display."
1404 (let ((frame-type (framep-on-display display)))
1405 (cond
1406 ((memq frame-type '(x w32 ns))
1407 (x-display-screens display))
1409 1))))
1411 (declare-function x-display-pixel-height "xfns.c" (&optional terminal))
1413 (defun display-pixel-height (&optional display)
1414 "Return the height of DISPLAY's screen in pixels.
1415 DISPLAY can be a display name or a frame.
1416 If DISPLAY is omitted or nil, it defaults to the selected frame's display.
1418 For character terminals, each character counts as a single pixel.
1420 For graphical terminals, note that on \"multi-monitor\" setups this
1421 refers to the pixel height for all physical monitors associated
1422 with DISPLAY. To get information for each physical monitor, use
1423 `display-monitor-attributes-list'."
1424 (let ((frame-type (framep-on-display display)))
1425 (cond
1426 ((memq frame-type '(x w32 ns))
1427 (x-display-pixel-height display))
1429 (frame-height (if (framep display) display (selected-frame)))))))
1431 (declare-function x-display-pixel-width "xfns.c" (&optional terminal))
1433 (defun display-pixel-width (&optional display)
1434 "Return the width 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 width 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-width display))
1449 (frame-width (if (framep display) display (selected-frame)))))))
1451 (defcustom display-mm-dimensions-alist nil
1452 "Alist for specifying screen dimensions in millimeters.
1453 The functions `display-mm-height' and `display-mm-width' consult
1454 this list before asking the system.
1456 Each element has the form (DISPLAY . (WIDTH . HEIGHT)), e.g.
1457 \(\":0.0\" . (287 . 215)).
1459 If `display' is t, it specifies dimensions for all graphical displays
1460 not explicitly specified."
1461 :version "22.1"
1462 :type '(alist :key-type (choice (string :tag "Display name")
1463 (const :tag "Default" t))
1464 :value-type (cons :tag "Dimensions"
1465 (integer :tag "Width")
1466 (integer :tag "Height")))
1467 :group 'frames)
1469 (declare-function x-display-mm-height "xfns.c" (&optional terminal))
1471 (defun display-mm-height (&optional display)
1472 "Return the height of DISPLAY's screen in millimeters.
1473 If the information is unavailable, this function returns nil.
1474 DISPLAY can be a display name or a frame.
1475 If DISPLAY is omitted or nil, it defaults to the selected frame's display.
1477 You can override what the system thinks the result should be by
1478 adding an entry to `display-mm-dimensions-alist'.
1480 For graphical terminals, note that on \"multi-monitor\" setups this
1481 refers to the height in millimeters for all physical monitors
1482 associated with DISPLAY. To get information for each physical
1483 monitor, use `display-monitor-attributes-list'."
1484 (and (memq (framep-on-display display) '(x w32 ns))
1485 (or (cddr (assoc (or display (frame-parameter nil 'display))
1486 display-mm-dimensions-alist))
1487 (cddr (assoc t display-mm-dimensions-alist))
1488 (x-display-mm-height display))))
1490 (declare-function x-display-mm-width "xfns.c" (&optional terminal))
1492 (defun display-mm-width (&optional display)
1493 "Return the width of DISPLAY's screen in millimeters.
1494 If the information is unavailable, this function returns nil.
1495 DISPLAY can be a display name or a frame.
1496 If DISPLAY is omitted or nil, it defaults to the selected frame's display.
1498 You can override what the system thinks the result should be by
1499 adding an entry to `display-mm-dimensions-alist'.
1501 For graphical terminals, note that on \"multi-monitor\" setups this
1502 refers to the width in millimeters for all physical monitors
1503 associated with DISPLAY. To get information for each physical
1504 monitor, use `display-monitor-attributes-list'."
1505 (and (memq (framep-on-display display) '(x w32 ns))
1506 (or (cadr (assoc (or display (frame-parameter nil 'display))
1507 display-mm-dimensions-alist))
1508 (cadr (assoc t display-mm-dimensions-alist))
1509 (x-display-mm-width display))))
1511 (declare-function x-display-backing-store "xfns.c" (&optional terminal))
1513 ;; In NS port, the return value may be `buffered', `retained', or
1514 ;; `non-retained'. See src/nsfns.m.
1515 (defun display-backing-store (&optional display)
1516 "Return the backing store capability of DISPLAY's screen.
1517 The value may be `always', `when-mapped', `not-useful', or nil if
1518 the question is inapplicable to a certain kind of display.
1519 DISPLAY can be a display name or a frame.
1520 If DISPLAY is omitted or nil, it defaults to the selected frame's display."
1521 (let ((frame-type (framep-on-display display)))
1522 (cond
1523 ((memq frame-type '(x w32 ns))
1524 (x-display-backing-store display))
1526 'not-useful))))
1528 (declare-function x-display-save-under "xfns.c" (&optional terminal))
1530 (defun display-save-under (&optional display)
1531 "Return non-nil if DISPLAY's screen supports the SaveUnder feature.
1532 DISPLAY can be a display name or a frame.
1533 If DISPLAY is omitted or nil, it defaults to the selected frame's display."
1534 (let ((frame-type (framep-on-display display)))
1535 (cond
1536 ((memq frame-type '(x w32 ns))
1537 (x-display-save-under display))
1539 'not-useful))))
1541 (declare-function x-display-planes "xfns.c" (&optional terminal))
1543 (defun display-planes (&optional display)
1544 "Return the number of planes supported by DISPLAY.
1545 DISPLAY can be a display name or a frame.
1546 If DISPLAY is omitted or nil, it defaults to the selected frame's display."
1547 (let ((frame-type (framep-on-display display)))
1548 (cond
1549 ((memq frame-type '(x w32 ns))
1550 (x-display-planes display))
1551 ((eq frame-type 'pc)
1554 (truncate (log (length (tty-color-alist)) 2))))))
1556 (declare-function x-display-color-cells "xfns.c" (&optional terminal))
1558 (defun display-color-cells (&optional display)
1559 "Return the number of color cells supported by DISPLAY.
1560 DISPLAY can be a display name or a frame.
1561 If DISPLAY is omitted or nil, it defaults to the selected frame's display."
1562 (let ((frame-type (framep-on-display display)))
1563 (cond
1564 ((memq frame-type '(x w32 ns))
1565 (x-display-color-cells display))
1566 ((eq frame-type 'pc)
1569 (tty-display-color-cells display)))))
1571 (declare-function x-display-visual-class "xfns.c" (&optional terminal))
1573 (defun display-visual-class (&optional display)
1574 "Return the visual class of DISPLAY.
1575 The value is one of the symbols `static-gray', `gray-scale',
1576 `static-color', `pseudo-color', `true-color', or `direct-color'.
1577 DISPLAY can be a display name or a frame.
1578 If DISPLAY is omitted or nil, it defaults to the selected frame's display."
1579 (let ((frame-type (framep-on-display display)))
1580 (cond
1581 ((memq frame-type '(x w32 ns))
1582 (x-display-visual-class display))
1583 ((and (memq frame-type '(pc t))
1584 (tty-display-color-p display))
1585 'static-color)
1587 'static-gray))))
1589 (declare-function x-display-monitor-attributes-list "xfns.c"
1590 (&optional terminal))
1591 (declare-function w32-display-monitor-attributes-list "w32fns.c"
1592 (&optional display))
1593 (declare-function ns-display-monitor-attributes-list "nsfns.m"
1594 (&optional terminal))
1596 (defun display-monitor-attributes-list (&optional display)
1597 "Return a list of physical monitor attributes on DISPLAY.
1598 DISPLAY can be a display name, a terminal name, or a frame.
1599 If DISPLAY is omitted or nil, it defaults to the selected frame's display.
1600 Each element of the list represents the attributes of a physical
1601 monitor. The first element corresponds to the primary monitor.
1603 The attributes for a physical monitor are represented as an alist
1604 of attribute keys and values as follows:
1606 geometry -- Position and size in pixels in the form of (X Y WIDTH HEIGHT)
1607 workarea -- Position and size of the work area in pixels in the
1608 form of (X Y WIDTH HEIGHT)
1609 mm-size -- Width and height in millimeters in the form of
1610 (WIDTH HEIGHT)
1611 frames -- List of frames dominated by the physical monitor
1612 name (*) -- Name of the physical monitor as a string
1613 source (*) -- Source of multi-monitor information as a string
1615 where X, Y, WIDTH, and HEIGHT are integers. X and Y are coordinates
1616 of the top-left corner, and might be negative for monitors other than
1617 the primary one. Keys labeled with (*) are optional.
1619 The \"work area\" is a measure of the \"usable\" display space.
1620 It may be less than the total screen size, owing to space taken up
1621 by window manager features (docks, taskbars, etc.). The precise
1622 details depend on the platform and environment.
1624 The `source' attribute describes the source from which the information
1625 was obtained. On X, this may be one of: \"Gdk\", \"XRandr\", \"Xinerama\",
1626 or \"fallback\".
1628 A frame is dominated by a physical monitor when either the
1629 largest area of the frame resides in the monitor, or the monitor
1630 is the closest to the frame if the frame does not intersect any
1631 physical monitors. Every (non-tooltip) frame (including invisible ones)
1632 in a graphical display is dominated by exactly one physical
1633 monitor at a time, though it can span multiple (or no) physical
1634 monitors."
1635 (let ((frame-type (framep-on-display display)))
1636 (cond
1637 ((eq frame-type 'x)
1638 (x-display-monitor-attributes-list display))
1639 ((eq frame-type 'w32)
1640 (w32-display-monitor-attributes-list display))
1641 ((eq frame-type 'ns)
1642 (ns-display-monitor-attributes-list display))
1644 (let ((geometry (list 0 0 (display-pixel-width display)
1645 (display-pixel-height display))))
1646 `(((geometry . ,geometry)
1647 (workarea . ,geometry)
1648 (mm-size . (,(display-mm-width display)
1649 ,(display-mm-height display)))
1650 (frames . ,(frames-on-display-list display)))))))))
1653 ;;;; Frame geometry values
1655 (defun frame-geom-value-cons (type value &optional frame)
1656 "Return equivalent geometry value for FRAME as a cons with car `+'.
1657 A geometry value equivalent to VALUE for FRAME is returned,
1658 where the value is a cons with car `+', not numeric.
1659 TYPE is the car of the original geometry spec (TYPE . VALUE).
1660 It is `top' or `left', depending on which edge VALUE is related to.
1661 VALUE is the cdr of a frame geometry spec: (left/top . VALUE).
1662 If VALUE is a number, then it is converted to a cons value, perhaps
1663 relative to the opposite frame edge from that in the original spec.
1664 FRAME defaults to the selected frame.
1666 Examples (measures in pixels) -
1667 Assuming display height/width=1024, frame height/width=600:
1668 300 inside display edge: 300 => (+ 300)
1669 (+ 300) => (+ 300)
1670 300 inside opposite display edge: (- 300) => (+ 124)
1671 -300 => (+ 124)
1672 300 beyond display edge
1673 (= 724 inside opposite display edge): (+ -300) => (+ -300)
1674 300 beyond display edge
1675 (= 724 inside opposite display edge): (- -300) => (+ 724)
1677 In the 3rd, 4th, and 6th examples, the returned value is relative to
1678 the opposite frame edge from the edge indicated in the input spec."
1679 (cond ((and (consp value) (eq '+ (car value))) ; e.g. (+ 300), (+ -300)
1680 value)
1681 ((natnump value) (list '+ value)) ; e.g. 300 => (+ 300)
1682 (t ; e.g. -300, (- 300), (- -300)
1683 (list '+ (- (if (eq 'left type) ; => (+ 124), (+ 124), (+ 724)
1684 (x-display-pixel-width)
1685 (x-display-pixel-height))
1686 (if (integerp value) (- value) (cadr value))
1687 (if (eq 'left type)
1688 (frame-pixel-width frame)
1689 (frame-pixel-height frame)))))))
1691 (defun frame-geom-spec-cons (spec &optional frame)
1692 "Return equivalent geometry spec for FRAME as a cons with car `+'.
1693 A geometry specification equivalent to SPEC for FRAME is returned,
1694 where the value is a cons with car `+', not numeric.
1695 SPEC is a frame geometry spec: (left . VALUE) or (top . VALUE).
1696 If VALUE is a number, then it is converted to a cons value, perhaps
1697 relative to the opposite frame edge from that in the original spec.
1698 FRAME defaults to the selected frame.
1700 Examples (measures in pixels) -
1701 Assuming display height=1024, frame height=600:
1702 top 300 below display top: (top . 300) => (top + 300)
1703 (top + 300) => (top + 300)
1704 bottom 300 above display bottom: (top - 300) => (top + 124)
1705 (top . -300) => (top + 124)
1706 top 300 above display top
1707 (= bottom 724 above display bottom): (top + -300) => (top + -300)
1708 bottom 300 below display bottom
1709 (= top 724 below display top): (top - -300) => (top + 724)
1711 In the 3rd, 4th, and 6th examples, the returned value is relative to
1712 the opposite frame edge from the edge indicated in the input spec."
1713 (cons (car spec) (frame-geom-value-cons (car spec) (cdr spec) frame)))
1716 (defun delete-other-frames (&optional frame)
1717 "Delete all frames on the current terminal, except FRAME.
1718 If FRAME uses another frame's minibuffer, the minibuffer frame is
1719 left untouched. FRAME nil or omitted means use the selected frame."
1720 (interactive)
1721 (unless frame
1722 (setq frame (selected-frame)))
1723 (let* ((mini-frame (window-frame (minibuffer-window frame)))
1724 (frames (delq mini-frame (delq frame (frame-list)))))
1725 ;; Only consider frames on the same terminal.
1726 (dolist (frame (prog1 frames (setq frames nil)))
1727 (if (eq (frame-terminal) (frame-terminal frame))
1728 (push frame frames)))
1729 ;; Delete mon-minibuffer-only frames first, because `delete-frame'
1730 ;; signals an error when trying to delete a mini-frame that's
1731 ;; still in use by another frame.
1732 (dolist (frame frames)
1733 (unless (eq (frame-parameter frame 'minibuffer) 'only)
1734 (delete-frame frame)))
1735 ;; Delete minibuffer-only frames.
1736 (dolist (frame frames)
1737 (when (eq (frame-parameter frame 'minibuffer) 'only)
1738 (delete-frame frame)))))
1740 ;; miscellaneous obsolescence declarations
1741 (define-obsolete-variable-alias 'delete-frame-hook
1742 'delete-frame-functions "22.1")
1745 ;; Blinking cursor
1747 (defgroup cursor nil
1748 "Displaying text cursors."
1749 :version "21.1"
1750 :group 'frames)
1752 (defcustom blink-cursor-delay 0.5
1753 "Seconds of idle time after which cursor starts to blink."
1754 :type 'number
1755 :group 'cursor)
1757 (defcustom blink-cursor-interval 0.5
1758 "Length of cursor blink interval in seconds."
1759 :type 'number
1760 :group 'cursor)
1762 (defcustom blink-cursor-blinks 10
1763 "How many times to blink before using a solid cursor on NS, X, and MS-Windows.
1764 Use 0 or negative value to blink forever."
1765 :version "24.4"
1766 :type 'integer
1767 :group 'cursor)
1769 (defvar blink-cursor-blinks-done 1
1770 "Number of blinks done since we started blinking on NS, X, and MS-Windows.")
1772 (defvar blink-cursor-idle-timer nil
1773 "Timer started after `blink-cursor-delay' seconds of Emacs idle time.
1774 The function `blink-cursor-start' is called when the timer fires.")
1776 (defvar blink-cursor-timer nil
1777 "Timer started from `blink-cursor-start'.
1778 This timer calls `blink-cursor-timer-function' every
1779 `blink-cursor-interval' seconds.")
1781 (defun blink-cursor-start ()
1782 "Timer function called from the timer `blink-cursor-idle-timer'.
1783 This starts the timer `blink-cursor-timer', which makes the cursor blink
1784 if appropriate. It also arranges to cancel that timer when the next
1785 command starts, by installing a pre-command hook."
1786 (when (null blink-cursor-timer)
1787 ;; Set up the timer first, so that if this signals an error,
1788 ;; blink-cursor-end is not added to pre-command-hook.
1789 (setq blink-cursor-blinks-done 1)
1790 (setq blink-cursor-timer
1791 (run-with-timer blink-cursor-interval blink-cursor-interval
1792 'blink-cursor-timer-function))
1793 (add-hook 'pre-command-hook 'blink-cursor-end)
1794 (internal-show-cursor nil nil)))
1796 (defun blink-cursor-timer-function ()
1797 "Timer function of timer `blink-cursor-timer'."
1798 (internal-show-cursor nil (not (internal-show-cursor-p)))
1799 ;; Each blink is two calls to this function.
1800 (setq blink-cursor-blinks-done (1+ blink-cursor-blinks-done))
1801 (when (and (> blink-cursor-blinks 0)
1802 (<= (* 2 blink-cursor-blinks) blink-cursor-blinks-done))
1803 (blink-cursor-suspend)
1804 (add-hook 'post-command-hook 'blink-cursor-check)))
1807 (defun blink-cursor-end ()
1808 "Stop cursor blinking.
1809 This is installed as a pre-command hook by `blink-cursor-start'.
1810 When run, it cancels the timer `blink-cursor-timer' and removes
1811 itself as a pre-command hook."
1812 (remove-hook 'pre-command-hook 'blink-cursor-end)
1813 (internal-show-cursor nil t)
1814 (when blink-cursor-timer
1815 (cancel-timer blink-cursor-timer)
1816 (setq blink-cursor-timer nil)))
1818 (defun blink-cursor-suspend ()
1819 "Suspend cursor blinking.
1820 This is called when no frame has focus and timers can be suspended.
1821 Timers are restarted by `blink-cursor-check', which is called when a
1822 frame receives focus."
1823 (blink-cursor-end)
1824 (when blink-cursor-idle-timer
1825 (cancel-timer blink-cursor-idle-timer)
1826 (setq blink-cursor-idle-timer nil)))
1828 (defun blink-cursor-check ()
1829 "Check if cursor blinking shall be restarted.
1830 This is done when a frame gets focus. Blink timers may be stopped by
1831 `blink-cursor-suspend'."
1832 (when (and blink-cursor-mode
1833 (not blink-cursor-idle-timer))
1834 (remove-hook 'post-command-hook 'blink-cursor-check)
1835 (setq blink-cursor-idle-timer
1836 (run-with-idle-timer blink-cursor-delay
1837 blink-cursor-delay
1838 'blink-cursor-start))))
1840 (define-obsolete-variable-alias 'blink-cursor 'blink-cursor-mode "22.1")
1842 (define-minor-mode blink-cursor-mode
1843 "Toggle cursor blinking (Blink Cursor mode).
1844 With a prefix argument ARG, enable Blink Cursor mode if ARG is
1845 positive, and disable it otherwise. If called from Lisp, enable
1846 the mode if ARG is omitted or nil.
1848 If the value of `blink-cursor-blinks' is positive (10 by default),
1849 the cursor stops blinking after that number of blinks, if Emacs
1850 gets no input during that time.
1852 See also `blink-cursor-interval' and `blink-cursor-delay'.
1854 This command is effective only on graphical frames. On text-only
1855 terminals, cursor blinking is controlled by the terminal."
1856 :init-value (not (or noninteractive
1857 no-blinking-cursor
1858 (eq system-type 'ms-dos)
1859 (not (memq window-system '(x w32 ns)))))
1860 :initialize 'custom-initialize-delay
1861 :group 'cursor
1862 :global t
1863 (blink-cursor-suspend)
1864 (remove-hook 'focus-in-hook #'blink-cursor-check)
1865 (remove-hook 'focus-out-hook #'blink-cursor-suspend)
1866 (when blink-cursor-mode
1867 (add-hook 'focus-in-hook #'blink-cursor-check)
1868 (add-hook 'focus-out-hook #'blink-cursor-suspend)
1869 (setq blink-cursor-idle-timer
1870 (run-with-idle-timer blink-cursor-delay
1871 blink-cursor-delay
1872 #'blink-cursor-start))))
1875 ;; Frame maximization/fullscreen
1877 (defun toggle-frame-maximized ()
1878 "Toggle maximization state of selected frame.
1879 Maximize selected frame or un-maximize if it is already maximized.
1881 If the frame is in fullscreen state, don't change its state, but
1882 set the frame's `fullscreen-restore' parameter to `maximized', so
1883 the frame will be maximized after disabling fullscreen state.
1885 Note that with some window managers you may have to set
1886 `frame-resize-pixelwise' to non-nil in order to make a frame
1887 appear truly maximized. In addition, you may have to set
1888 `x-frame-normalize-before-maximize' in order to enable
1889 transitions from one fullscreen state to another.
1891 See also `toggle-frame-fullscreen'."
1892 (interactive)
1893 (let ((fullscreen (frame-parameter nil 'fullscreen)))
1894 (cond
1895 ((memq fullscreen '(fullscreen fullboth))
1896 (set-frame-parameter nil 'fullscreen-restore 'maximized))
1897 ((eq fullscreen 'maximized)
1898 (set-frame-parameter nil 'fullscreen nil))
1900 (set-frame-parameter nil 'fullscreen 'maximized)))))
1902 (defun toggle-frame-fullscreen ()
1903 "Toggle fullscreen state of selected frame.
1904 Make selected frame fullscreen or restore its previous size if it
1905 is already fullscreen.
1907 Before making the frame fullscreen remember the current value of
1908 the frame's `fullscreen' parameter in the `fullscreen-restore'
1909 parameter of the frame. That value is used to restore the
1910 frame's fullscreen state when toggling fullscreen the next time.
1912 Note that with some window managers you may have to set
1913 `frame-resize-pixelwise' to non-nil in order to make a frame
1914 appear truly fullscreen. In addition, you may have to set
1915 `x-frame-normalize-before-maximize' in order to enable
1916 transitions from one fullscreen state to another.
1918 See also `toggle-frame-maximized'."
1919 (interactive)
1920 (let ((fullscreen (frame-parameter nil 'fullscreen)))
1921 (if (memq fullscreen '(fullscreen fullboth))
1922 (let ((fullscreen-restore (frame-parameter nil 'fullscreen-restore)))
1923 (if (memq fullscreen-restore '(maximized fullheight fullwidth))
1924 (set-frame-parameter nil 'fullscreen fullscreen-restore)
1925 (set-frame-parameter nil 'fullscreen nil)))
1926 (modify-frame-parameters
1927 nil `((fullscreen . fullboth) (fullscreen-restore . ,fullscreen))))))
1929 ;;;; Key bindings
1931 (define-key ctl-x-5-map "2" 'make-frame-command)
1932 (define-key ctl-x-5-map "1" 'delete-other-frames)
1933 (define-key ctl-x-5-map "0" 'delete-frame)
1934 (define-key ctl-x-5-map "o" 'other-frame)
1935 (define-key global-map [f11] 'toggle-frame-fullscreen)
1936 (define-key global-map [(meta f10)] 'toggle-frame-maximized)
1937 (define-key esc-map [f10] 'toggle-frame-maximized)
1940 ;; Misc.
1942 ;; Only marked as obsolete in 24.3.
1943 (define-obsolete-variable-alias 'automatic-hscrolling
1944 'auto-hscroll-mode "22.1")
1946 (make-variable-buffer-local 'show-trailing-whitespace)
1948 ;; Defined in dispnew.c.
1949 (make-obsolete-variable
1950 'window-system-version "it does not give useful information." "24.3")
1952 (provide 'frame)
1954 ;;; frame.el ends here