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