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