(MISC): Augment.
[emacs.git] / lisp / frame.el
blob2486cf7bdfc8fe3239e09cf60d7797c32fd81a1d
1 ;;; frame.el --- multi-frame management independent of window systems.
3 ;; Copyright (C) 1993, 1994, 1996, 1997, 2000 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: internal
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Code:
27 (defvar frame-creation-function nil
28 "Window-system dependent function to call to create a new frame.
29 The window system startup file should set this to its frame creation
30 function, which should take an alist of parameters as its argument.")
32 ;;; The initial value given here for used to ask for a minibuffer.
33 ;;; But that's not necessary, because the default is to have one.
34 ;;; By not specifying it here, we let an X resource specify it.
35 (defcustom initial-frame-alist nil
36 "*Alist of frame parameters for creating the initial X window frame.
37 You can set this in your `.emacs' file; for example,
38 (setq initial-frame-alist '((top . 1) (left . 1) (width . 80) (height . 55)))
39 Parameters specified here supersede the values given in `default-frame-alist'.
41 If the value calls for a frame without a minibuffer, and you have not created
42 a minibuffer frame on your own, one is created according to
43 `minibuffer-frame-alist'.
45 You can specify geometry-related options for just the initial frame
46 by setting this variable in your `.emacs' file; however, they won't
47 take effect until Emacs reads `.emacs', which happens after first creating
48 the frame. If you want the frame to have the proper geometry as soon
49 as it appears, you need to use this three-step process:
50 * Specify X resources to give the geometry you want.
51 * Set `default-frame-alist' to override these options so that they
52 don't affect subsequent frames.
53 * Set `initial-frame-alist' in a way that matches the X resources,
54 to override what you put in `default-frame-alist'."
55 :type '(repeat (cons :format "%v"
56 (symbol :tag "Parameter")
57 (sexp :tag "Value")))
58 :group 'frames)
60 (defcustom minibuffer-frame-alist '((width . 80) (height . 2))
61 "*Alist of frame parameters for initially creating a minibuffer frame.
62 You can set this in your `.emacs' file; for example,
63 (setq minibuffer-frame-alist
64 '((top . 1) (left . 1) (width . 80) (height . 2)))
65 Parameters specified here supersede the values given in
66 `default-frame-alist', for a minibuffer frame."
67 :type '(repeat (cons :format "%v"
68 (symbol :tag "Parameter")
69 (sexp :tag "Value")))
70 :group 'frames)
72 (defcustom pop-up-frame-alist nil
73 "*Alist of frame parameters used when creating pop-up frames.
74 Pop-up frames are used for completions, help, and the like.
75 This variable can be set in your init file, like this:
76 (setq pop-up-frame-alist '((width . 80) (height . 20)))
77 These supersede the values given in `default-frame-alist',
78 for pop-up frames."
79 :type '(repeat (cons :format "%v"
80 (symbol :tag "Parameter")
81 (sexp :tag "Value")))
82 :group 'frames)
84 (setq pop-up-frame-function
85 (function (lambda ()
86 (make-frame pop-up-frame-alist))))
88 (defcustom special-display-frame-alist
89 '((height . 14) (width . 80) (unsplittable . t))
90 "*Alist of frame parameters used when creating special frames.
91 Special frames are used for buffers whose names are in
92 `special-display-buffer-names' and for buffers whose names match
93 one of the regular expressions in `special-display-regexps'.
94 This variable can be set in your init file, like this:
95 (setq special-display-frame-alist '((width . 80) (height . 20)))
96 These supersede the values given in `default-frame-alist'."
97 :type '(repeat (cons :format "%v"
98 (symbol :tag "Parameter")
99 (sexp :tag "Value")))
100 :group 'frames)
102 (defun special-display-popup-frame (buffer &optional args)
103 "Display BUFFER in its own frame, reusing an existing window if any.
104 Return the window chosen.
105 Currently we do not insist on selecting the window within its frame.
106 If ARGS is an alist, use it as a list of frame parameter specs.
107 If ARGS is a list whose car is a symbol,
108 use (car ARGS) as a function to do the work.
109 Pass it BUFFER as first arg, and (cdr ARGS) gives the rest of the args."
110 (if (and args (symbolp (car args)))
111 (apply (car args) buffer (cdr args))
112 (let ((window (get-buffer-window buffer t)))
113 (if window
114 ;; If we have a window already, make it visible.
115 (let ((frame (window-frame window)))
116 (make-frame-visible frame)
117 (raise-frame frame)
118 window)
119 ;; If no window yet, make one in a new frame.
120 (let ((frame (make-frame (append args special-display-frame-alist))))
121 (set-window-buffer (frame-selected-window frame) buffer)
122 (set-window-dedicated-p (frame-selected-window frame) t)
123 (frame-selected-window frame))))))
125 (defun handle-delete-frame (event)
126 "Handle delete-frame events from the X server."
127 (interactive "e")
128 (let ((frame (posn-window (event-start event)))
129 (i 0)
130 (tail (frame-list)))
131 (while tail
132 (and (frame-visible-p (car tail))
133 (not (eq (car tail) frame))
134 (setq i (1+ i)))
135 (setq tail (cdr tail)))
136 (if (> i 0)
137 (delete-frame frame t)
138 ;; Gildea@x.org says it is ok to ask questions before terminating.
139 (save-buffers-kill-emacs))))
141 ;;;; Arrangement of frames at startup
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.
147 ;;; 2) If no frames have been opened, we open an initial text frame.
149 ;;; 3) Once the init file is done, we apply any newly set parameters
150 ;;; in initial-frame-alist to the frame.
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)
158 ;;; If we create the initial frame, this is it.
159 (defvar frame-initial-frame nil)
161 ;; Record the parameters used in frame-initialize to make the initial frame.
162 (defvar frame-initial-frame-alist)
164 (defvar frame-initial-geometry-arguments nil)
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 window-system (not noninteractive) (not (eq window-system 'pc)))
173 (progn
174 ;; Turn on special-display processing only if there's a window system.
175 (setq special-display-function 'special-display-popup-frame)
177 ;; If there is no frame with a minibuffer besides the terminal
178 ;; frame, then we need to create the opening frame. Make sure
179 ;; it has a minibuffer, but let initial-frame-alist omit the
180 ;; minibuffer spec.
181 (or (delq terminal-frame (minibuffer-frame-list))
182 (progn
183 (setq frame-initial-frame-alist
184 (append initial-frame-alist default-frame-alist nil))
185 (or (assq 'horizontal-scroll-bars frame-initial-frame-alist)
186 (setq frame-initial-frame-alist
187 (cons '(horizontal-scroll-bars . t)
188 frame-initial-frame-alist)))
189 (setq default-minibuffer-frame
190 (setq frame-initial-frame
191 (make-frame frame-initial-frame-alist)))
192 ;; Delete any specifications for window geometry parameters
193 ;; so that we won't reapply them in frame-notice-user-settings.
194 ;; It would be wrong to reapply them then,
195 ;; because that would override explicit user resizing.
196 (setq initial-frame-alist
197 (frame-remove-geometry-params initial-frame-alist))))
198 ;; At this point, we know that we have a frame open, so we
199 ;; can delete the terminal frame.
200 (delete-frame terminal-frame)
201 (setq terminal-frame nil))
203 ;; No, we're not running a window system. Use make-terminal-frame if
204 ;; we support that feature, otherwise arrange to cause errors.
205 (or (eq window-system 'pc)
206 (setq frame-creation-function
207 (if (fboundp 'tty-create-frame-with-faces)
208 'tty-create-frame-with-faces
209 (function
210 (lambda (parameters)
211 (error
212 "Can't create multiple frames without a window system"))))))))
214 (defvar frame-notice-user-settings t
215 "Non-nil means function `frame-notice-user-settings' wasn't run yet.")
217 ;;; startup.el calls this function after loading the user's init
218 ;;; file. Now default-frame-alist and initial-frame-alist contain
219 ;;; information to which we must react; do what needs to be done.
220 (defun frame-notice-user-settings ()
221 "Act on user's init file settings of frame parameters.
222 React to settings of `default-frame-alist', `initial-frame-alist' there."
223 ;; Make menu-bar-mode and default-frame-alist consistent.
224 (when (boundp 'menu-bar-mode)
225 (let ((default (assq 'menu-bar-lines default-frame-alist)))
226 (if default
227 (setq menu-bar-mode (not (eq (cdr default) 0)))
228 (setq default-frame-alist
229 (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0))
230 default-frame-alist)))))
232 ;; Make tool-bar-mode and default-frame-alist consistent. Don't do
233 ;; it in batch mode since that would leave a tool-bar-lines
234 ;; parameter in default-frame-alist in a dumped Emacs, which is not
235 ;; what we want.
236 (when (and (boundp 'tool-bar-mode)
237 (not noninteractive))
238 (let ((default (assq 'tool-bar-lines default-frame-alist)))
239 (if default
240 (setq tool-bar-mode (not (eq (cdr default) 0)))
241 (setq default-frame-alist
242 (cons (cons 'tool-bar-lines (if tool-bar-mode 1 0))
243 default-frame-alist)))))
245 ;; Creating and deleting frames may shift the selected frame around,
246 ;; and thus the current buffer. Protect against that. We don't
247 ;; want to use save-excursion here, because that may also try to set
248 ;; the buffer of the selected window, which fails when the selected
249 ;; window is the minibuffer.
250 (let ((old-buffer (current-buffer)))
252 (when (and frame-notice-user-settings
253 (null frame-initial-frame))
254 ;; This case happens when we don't have a window system, and
255 ;; also for MS-DOS frames.
256 (let ((parms (frame-parameters frame-initial-frame)))
257 ;; Don't change the frame names.
258 (setq parms (delq (assq 'name parms) parms))
259 ;; Can't modify the minibuffer parameter, so don't try.
260 (setq parms (delq (assq 'minibuffer parms) parms))
261 (modify-frame-parameters nil
262 (if (null window-system)
263 (append initial-frame-alist
264 default-frame-alist
265 parms
266 nil)
267 ;; initial-frame-alist and
268 ;; default-frame-alist were already
269 ;; applied in pc-win.el.
270 parms))
271 (if (null window-system) ;; MS-DOS does this differently in pc-win.el
272 (let ((newparms (frame-parameters))
273 (frame (selected-frame)))
274 (tty-handle-reverse-video frame newparms)
275 ;; If we changed the background color, we need to update
276 ;; the background-mode parameter, and maybe some faces,
277 ;; too.
278 (when (assq 'background-color newparms)
279 (unless (or (assq 'background-mode initial-frame-alist)
280 (assq 'background-mode default-frame-alist))
281 (frame-set-background-mode frame))
282 (face-set-after-frame-default frame))))))
284 ;; If the initial frame is still around, apply initial-frame-alist
285 ;; and default-frame-alist to it.
286 (when (frame-live-p frame-initial-frame)
288 ;; When tool-bar has been switched off, correct the frame size
289 ;; by the lines added in x-create-frame for the tool-bar and
290 ;; switch `tool-bar-mode' off.
291 (when (and (display-graphic-p)
292 (or (eq 0 (cdr (assq 'tool-bar-lines initial-frame-alist)))
293 (eq 0 (cdr (assq 'tool-bar-lines default-frame-alist)))))
294 (let* ((char-height (frame-char-height frame-initial-frame))
295 (image-height 24)
296 (margin (cond ((and (consp tool-bar-button-margin)
297 (integerp (cdr tool-bar-button-margin))
298 (> tool-bar-button-margin 0))
299 (cdr tool-bar-button-margin))
300 ((and (integerp tool-bar-button-margin)
301 (> tool-bar-button-margin 0))
302 tool-bar-button-margin)
303 (t 0)))
304 (relief (if (and (integerp tool-bar-button-relief)
305 (> tool-bar-button-relief 0))
306 tool-bar-button-relief 3))
307 (lines (/ (+ image-height
308 (* 2 margin)
309 (* 2 relief)
310 (1- char-height))
311 char-height))
312 (height (frame-parameter frame-initial-frame 'height)))
313 (modify-frame-parameters frame-initial-frame
314 (list (cons 'height (- height lines))))
315 (tool-bar-mode -1)))
318 ;; The initial frame we create above always has a minibuffer.
319 ;; If the user wants to remove it, or make it a minibuffer-only
320 ;; frame, then we'll have to delete the current frame and make a
321 ;; new one; you can't remove or add a root window to/from an
322 ;; existing frame.
324 ;; NOTE: default-frame-alist was nil when we created the
325 ;; existing frame. We need to explicitly include
326 ;; default-frame-alist in the parameters of the screen we
327 ;; create here, so that its new value, gleaned from the user's
328 ;; .emacs file, will be applied to the existing screen.
329 (if (not (eq (cdr (or (assq 'minibuffer initial-frame-alist)
330 (assq 'minibuffer default-frame-alist)
331 '(minibuffer . t)))
333 ;; Create the new frame.
334 (let (parms new)
335 ;; If the frame isn't visible yet, wait till it is.
336 ;; If the user has to position the window,
337 ;; Emacs doesn't know its real position until
338 ;; the frame is seen to be visible.
339 (while (not (cdr (assq 'visibility
340 (frame-parameters frame-initial-frame))))
341 (sleep-for 1))
342 (setq parms (frame-parameters frame-initial-frame))
344 ;; Get rid of `name' unless it was specified explicitly before.
345 (or (assq 'name frame-initial-frame-alist)
346 (setq parms (delq (assq 'name parms) parms)))
348 (setq parms (append initial-frame-alist
349 default-frame-alist
350 parms
351 nil))
353 ;; Get rid of `reverse', because that was handled
354 ;; when we first made the frame.
355 (setq parms (cons '(reverse) (delq (assq 'reverse parms) parms)))
357 (if (assq 'height frame-initial-geometry-arguments)
358 (setq parms (assq-delete-all 'height parms)))
359 (if (assq 'width frame-initial-geometry-arguments)
360 (setq parms (assq-delete-all 'width parms)))
361 (if (assq 'left frame-initial-geometry-arguments)
362 (setq parms (assq-delete-all 'left parms)))
363 (if (assq 'top frame-initial-geometry-arguments)
364 (setq parms (assq-delete-all 'top parms)))
365 (setq new
366 (make-frame
367 ;; Use the geometry args that created the existing
368 ;; frame, rather than the parms we get for it.
369 (append frame-initial-geometry-arguments
370 '((user-size . t) (user-position . t))
371 parms)))
372 ;; The initial frame, which we are about to delete, may be
373 ;; the only frame with a minibuffer. If it is, create a
374 ;; new one.
375 (or (delq frame-initial-frame (minibuffer-frame-list))
376 (make-initial-minibuffer-frame nil))
378 ;; If the initial frame is serving as a surrogate
379 ;; minibuffer frame for any frames, we need to wean them
380 ;; onto a new frame. The default-minibuffer-frame
381 ;; variable must be handled similarly.
382 (let ((users-of-initial
383 (filtered-frame-list
384 (function (lambda (frame)
385 (and (not (eq frame frame-initial-frame))
386 (eq (window-frame
387 (minibuffer-window frame))
388 frame-initial-frame)))))))
389 (if (or users-of-initial
390 (eq default-minibuffer-frame frame-initial-frame))
392 ;; Choose an appropriate frame. Prefer frames which
393 ;; are only minibuffers.
394 (let* ((new-surrogate
395 (car
396 (or (filtered-frame-list
397 (function
398 (lambda (frame)
399 (eq (cdr (assq 'minibuffer
400 (frame-parameters frame)))
401 'only))))
402 (minibuffer-frame-list))))
403 (new-minibuffer (minibuffer-window new-surrogate)))
405 (if (eq default-minibuffer-frame frame-initial-frame)
406 (setq default-minibuffer-frame new-surrogate))
408 ;; Wean the frames using frame-initial-frame as
409 ;; their minibuffer frame.
410 (mapcar
411 (function
412 (lambda (frame)
413 (modify-frame-parameters
414 frame (list (cons 'minibuffer new-minibuffer)))))
415 users-of-initial))))
417 ;; Redirect events enqueued at this frame to the new frame.
418 ;; Is this a good idea?
419 (redirect-frame-focus frame-initial-frame new)
421 ;; Finally, get rid of the old frame.
422 (delete-frame frame-initial-frame t))
424 ;; Otherwise, we don't need all that rigamarole; just apply
425 ;; the new parameters.
426 (let (newparms allparms tail)
427 (setq allparms (append initial-frame-alist
428 default-frame-alist nil))
429 (if (assq 'height frame-initial-geometry-arguments)
430 (setq allparms (assq-delete-all 'height allparms)))
431 (if (assq 'width frame-initial-geometry-arguments)
432 (setq allparms (assq-delete-all 'width allparms)))
433 (if (assq 'left frame-initial-geometry-arguments)
434 (setq allparms (assq-delete-all 'left allparms)))
435 (if (assq 'top frame-initial-geometry-arguments)
436 (setq allparms (assq-delete-all 'top allparms)))
437 (setq tail allparms)
438 ;; Find just the parms that have changed since we first
439 ;; made this frame. Those are the ones actually set by
440 ;; the init file. For those parms whose values we already knew
441 ;; (such as those spec'd by command line options)
442 ;; it is undesirable to specify the parm again
443 ;; once the user has seen the frame and been able to alter it
444 ;; manually.
445 (while tail
446 (let (newval oldval)
447 (setq oldval (assq (car (car tail))
448 frame-initial-frame-alist))
449 (setq newval (cdr (assq (car (car tail)) allparms)))
450 (or (and oldval (eq (cdr oldval) newval))
451 (setq newparms
452 (cons (cons (car (car tail)) newval) newparms))))
453 (setq tail (cdr tail)))
454 (setq newparms (nreverse newparms))
455 (modify-frame-parameters frame-initial-frame
456 newparms)
457 ;; If we changed the background color,
458 ;; we need to update the background-mode parameter
459 ;; and maybe some faces too.
460 (when (assq 'background-color newparms)
461 (unless (assq 'background-mode newparms)
462 (frame-set-background-mode frame-initial-frame))
463 (face-set-after-frame-default frame-initial-frame)))))
465 ;; Restore the original buffer.
466 (set-buffer old-buffer)
468 ;; Make sure the initial frame can be GC'd if it is ever deleted.
469 ;; Make sure frame-notice-user-settings does nothing if called twice.
470 (setq frame-notice-user-settings nil)
471 (setq frame-initial-frame nil)))
473 (defun make-initial-minibuffer-frame (display)
474 (let ((parms (append minibuffer-frame-alist '((minibuffer . only)))))
475 (if display
476 (make-frame-on-display display parms)
477 (make-frame parms))))
479 ;;;; Creation of additional frames, and other frame miscellanea
481 (defun get-other-frame ()
482 "Return some frame other than the current frame.
483 Create one if necessary. Note that the minibuffer frame, if separate,
484 is not considered (see `next-frame')."
485 (let ((s (if (equal (next-frame (selected-frame)) (selected-frame))
486 (make-frame)
487 (next-frame (selected-frame)))))
490 (defun next-multiframe-window ()
491 "Select the next window, regardless of which frame it is on."
492 (interactive)
493 (select-window (next-window (selected-window)
494 (> (minibuffer-depth) 0)
495 t)))
497 (defun previous-multiframe-window ()
498 "Select the previous window, regardless of which frame it is on."
499 (interactive)
500 (select-window (previous-window (selected-window)
501 (> (minibuffer-depth) 0)
502 t)))
504 (defun make-frame-on-display (display &optional parameters)
505 "Make a frame on display DISPLAY.
506 The optional second argument PARAMETERS specifies additional frame parameters."
507 (interactive "sMake frame on display: ")
508 (or (string-match "\\`[^:]*:[0-9]+\\(\\.[0-9]+\\)?\\'" display)
509 (error "Invalid display, not HOST:SERVER or HOST:SERVER.SCREEN"))
510 (make-frame (cons (cons 'display display) parameters)))
512 (defun make-frame-command ()
513 "Make a new frame, and select it if the terminal displays only one frame."
514 (interactive)
515 (if (and window-system (not (eq window-system 'pc)))
516 (make-frame)
517 (select-frame (make-frame))))
519 (defvar before-make-frame-hook nil
520 "Functions to run before a frame is created.")
522 (defvar after-make-frame-functions nil
523 "Functions to run after a frame is created.
524 The functions are run with one arg, the newly created frame.")
526 (defvar after-setting-font-hook nil
527 "Functions to run after a frame's font has been changed.")
529 ;; Alias, kept temporarily.
530 (defalias 'new-frame 'make-frame)
532 (defun make-frame (&optional parameters)
533 "Return a newly created frame displaying the current buffer.
534 Optional argument PARAMETERS is an alist of parameters for the new frame.
535 Each element of PARAMETERS should have the form (NAME . VALUE), for example:
537 (name . STRING) The frame should be named STRING.
539 (width . NUMBER) The frame should be NUMBER characters in width.
540 (height . NUMBER) The frame should be NUMBER text lines high.
542 You cannot specify either `width' or `height', you must use neither or both.
544 (minibuffer . t) The frame should have a minibuffer.
545 (minibuffer . nil) The frame should have no minibuffer.
546 (minibuffer . only) The frame should contain only a minibuffer.
547 (minibuffer . WINDOW) The frame should use WINDOW as its minibuffer window.
549 Before the frame is created (via `frame-creation-function'), functions on the
550 hook `before-make-frame-hook' are run. After the frame is created, functions
551 on `after-make-frame-functions' are run with one arg, the newly created frame."
552 (interactive)
553 (run-hooks 'before-make-frame-hook)
554 (let ((frame (funcall frame-creation-function parameters)))
555 (run-hook-with-args 'after-make-frame-functions frame)
556 frame))
558 (defun filtered-frame-list (predicate)
559 "Return a list of all live frames which satisfy PREDICATE."
560 (let* ((frames (frame-list))
561 (list frames))
562 (while (consp frames)
563 (unless (funcall predicate (car frames))
564 (setcar frames nil))
565 (setq frames (cdr frames)))
566 (delq nil list)))
568 (defun minibuffer-frame-list ()
569 "Return a list of all frames with their own minibuffers."
570 (filtered-frame-list
571 (function (lambda (frame)
572 (eq frame (window-frame (minibuffer-window frame)))))))
574 (defun frames-on-display-list (&optional display)
575 "Return a list of all frames on DISPLAY.
576 DISPLAY is a name of a display, a string of the form HOST:SERVER.SCREEN.
577 If DISPLAY is omitted or nil, it defaults to the selected frame's display."
578 (let* ((display (or display (frame-parameter nil 'display)))
579 (func #'(lambda (frame)
580 (eq (frame-parameter frame 'display) display))))
581 (filtered-frame-list func)))
583 (defun framep-on-display (&optional display)
584 "Return the type of frames on DISPLAY.
585 DISPLAY may be a display name or a frame. If it is a frame, its type is
586 returned.
587 If DISPLAY is omitted or nil, it defaults to the selected frame's display.
588 All frames on a given display are of the same type."
589 (or (framep display)
590 (framep (car (frames-on-display-list display)))))
592 (defun frame-remove-geometry-params (param-list)
593 "Return the parameter list PARAM-LIST, but with geometry specs removed.
594 This deletes all bindings in PARAM-LIST for `top', `left', `width',
595 `height', `user-size' and `user-position' parameters.
596 Emacs uses this to avoid overriding explicit moves and resizings from
597 the user during startup."
598 (setq param-list (cons nil param-list))
599 (let ((tail param-list))
600 (while (consp (cdr tail))
601 (if (and (consp (car (cdr tail)))
602 (memq (car (car (cdr tail)))
603 '(height width top left user-position user-size)))
604 (progn
605 (setq frame-initial-geometry-arguments
606 (cons (car (cdr tail)) frame-initial-geometry-arguments))
607 (setcdr tail (cdr (cdr tail))))
608 (setq tail (cdr tail)))))
609 (setq frame-initial-geometry-arguments
610 (nreverse frame-initial-geometry-arguments))
611 (cdr param-list))
614 (defcustom focus-follows-mouse t
615 "*Non-nil if window system changes focus when you move the mouse."
616 :type 'boolean
617 :group 'frames
618 :version "20.3")
620 (defun other-frame (arg)
621 "Select the ARG'th different visible frame, and raise it.
622 All frames are arranged in a cyclic order.
623 This command selects the frame ARG steps away in that order.
624 A negative ARG moves in the opposite order."
625 (interactive "p")
626 (let ((frame (selected-frame)))
627 (while (> arg 0)
628 (setq frame (next-frame frame))
629 (while (not (eq (frame-visible-p frame) t))
630 (setq frame (next-frame frame)))
631 (setq arg (1- arg)))
632 (while (< arg 0)
633 (setq frame (previous-frame frame))
634 (while (not (eq (frame-visible-p frame) t))
635 (setq frame (previous-frame frame)))
636 (setq arg (1+ arg)))
637 (select-frame frame)
638 (raise-frame frame)
639 ;; Ensure, if possible, that frame gets input focus.
640 (when (eq window-system 'w32)
641 (w32-focus-frame frame))
642 (cond (focus-follows-mouse
643 (unless (eq window-system 'w32)
644 (set-mouse-position (selected-frame) (1- (frame-width)) 0)))
646 (when (eq window-system 'x)
647 (x-focus-frame frame))))))
649 (defun make-frame-names-alist ()
650 (let* ((current-frame (selected-frame))
651 (falist
652 (cons
653 (cons (frame-parameter current-frame 'name) current-frame) nil))
654 (frame (next-frame nil t)))
655 (while (not (eq frame current-frame))
656 (progn
657 (setq falist (cons (cons (frame-parameter frame 'name) frame) falist))
658 (setq frame (next-frame frame t))))
659 falist))
661 (defvar frame-name-history nil)
662 (defun select-frame-by-name (name)
663 "Select the frame whose name is NAME and raise it.
664 If there is no frame by that name, signal an error."
665 (interactive
666 (let* ((frame-names-alist (make-frame-names-alist))
667 (default (car (car frame-names-alist)))
668 (input (completing-read
669 (format "Select Frame (default %s): " default)
670 frame-names-alist nil t nil 'frame-name-history)))
671 (if (= (length input) 0)
672 (list default)
673 (list input))))
674 (let* ((frame-names-alist (make-frame-names-alist))
675 (frame (cdr (assoc name frame-names-alist))))
676 (or frame
677 (error "There is no frame named `%s'" name))
678 (make-frame-visible frame)
679 (raise-frame frame)
680 (select-frame frame)
681 ;; Ensure, if possible, that frame gets input focus.
682 (if (eq window-system 'w32)
683 (w32-focus-frame frame)
684 (when focus-follows-mouse
685 (set-mouse-position (selected-frame) (1- (frame-width)) 0)))))
687 ;;;; Frame configurations
689 (defun current-frame-configuration ()
690 "Return a list describing the positions and states of all frames.
691 Its car is `frame-configuration'.
692 Each element of the cdr is a list of the form (FRAME ALIST WINDOW-CONFIG),
693 where
694 FRAME is a frame object,
695 ALIST is an association list specifying some of FRAME's parameters, and
696 WINDOW-CONFIG is a window configuration object for FRAME."
697 (cons 'frame-configuration
698 (mapcar (function
699 (lambda (frame)
700 (list frame
701 (frame-parameters frame)
702 (current-window-configuration frame))))
703 (frame-list))))
705 (defun set-frame-configuration (configuration &optional nodelete)
706 "Restore the frames to the state described by CONFIGURATION.
707 Each frame listed in CONFIGURATION has its position, size, window
708 configuration, and other parameters set as specified in CONFIGURATION.
709 Ordinarily, this function deletes all existing frames not
710 listed in CONFIGURATION. But if optional second argument NODELETE
711 is given and non-nil, the unwanted frames are iconified instead."
712 (or (frame-configuration-p configuration)
713 (signal 'wrong-type-argument
714 (list 'frame-configuration-p configuration)))
715 (let ((config-alist (cdr configuration))
716 frames-to-delete)
717 (mapcar (function
718 (lambda (frame)
719 (let ((parameters (assq frame config-alist)))
720 (if parameters
721 (progn
722 (modify-frame-parameters
723 frame
724 ;; Since we can't set a frame's minibuffer status,
725 ;; we might as well omit the parameter altogether.
726 (let* ((parms (nth 1 parameters))
727 (mini (assq 'minibuffer parms)))
728 (if mini (setq parms (delq mini parms)))
729 parms))
730 (set-window-configuration (nth 2 parameters)))
731 (setq frames-to-delete (cons frame frames-to-delete))))))
732 (frame-list))
733 (if nodelete
734 ;; Note: making frames invisible here was tried
735 ;; but led to some strange behavior--each time the frame
736 ;; was made visible again, the window manager asked afresh
737 ;; for where to put it.
738 (mapcar 'iconify-frame frames-to-delete)
739 (mapcar 'delete-frame frames-to-delete))))
741 ;;;; Convenience functions for accessing and interactively changing
742 ;;;; frame parameters.
744 (defun frame-height (&optional frame)
745 "Return number of lines available for display on FRAME.
746 If FRAME is omitted, describe the currently selected frame."
747 (cdr (assq 'height (frame-parameters frame))))
749 (defun frame-width (&optional frame)
750 "Return number of columns available for display on FRAME.
751 If FRAME is omitted, describe the currently selected frame."
752 (cdr (assq 'width (frame-parameters frame))))
754 (defalias 'set-default-font 'set-frame-font)
755 (defun set-frame-font (font-name)
756 "Set the font of the selected frame to FONT-NAME.
757 When called interactively, prompt for the name of the font to use.
758 To get the frame's current default font, use `frame-parameters'."
759 (interactive
760 (list
761 (let ((completion-ignore-case t))
762 (completing-read "Font name: "
763 (mapcar #'list
764 ;; x-list-fonts will fail with an error
765 ;; if this frame doesn't support fonts.
766 (x-list-fonts "*" nil (selected-frame)))))))
767 (modify-frame-parameters (selected-frame)
768 (list (cons 'font font-name)))
769 (run-hooks 'after-setting-font-hook 'after-setting-font-hooks))
771 (defun set-background-color (color-name)
772 "Set the background color of the selected frame to COLOR-NAME.
773 When called interactively, prompt for the name of the color to use.
774 To get the frame's current background color, use `frame-parameters'."
775 (interactive (list (facemenu-read-color)))
776 (modify-frame-parameters (selected-frame)
777 (list (cons 'background-color color-name))))
779 (defun set-foreground-color (color-name)
780 "Set the foreground color of the selected frame to COLOR-NAME.
781 When called interactively, prompt for the name of the color to use.
782 To get the frame's current foreground color, use `frame-parameters'."
783 (interactive (list (facemenu-read-color)))
784 (modify-frame-parameters (selected-frame)
785 (list (cons 'foreground-color color-name))))
787 (defun set-cursor-color (color-name)
788 "Set the text cursor color of the selected frame to COLOR-NAME.
789 When called interactively, prompt for the name of the color to use.
790 To get the frame's current cursor color, use `frame-parameters'."
791 (interactive (list (facemenu-read-color)))
792 (modify-frame-parameters (selected-frame)
793 (list (cons 'cursor-color color-name))))
795 (defun set-mouse-color (color-name)
796 "Set the color of the mouse pointer of the selected frame to COLOR-NAME.
797 When called interactively, prompt for the name of the color to use.
798 To get the frame's current mouse color, use `frame-parameters'."
799 (interactive (list (facemenu-read-color)))
800 (modify-frame-parameters (selected-frame)
801 (list (cons 'mouse-color
802 (or color-name
803 (cdr (assq 'mouse-color
804 (frame-parameters))))))))
806 (defun set-border-color (color-name)
807 "Set the color of the border of the selected frame to COLOR-NAME.
808 When called interactively, prompt for the name of the color to use.
809 To get the frame's current border color, use `frame-parameters'."
810 (interactive (list (facemenu-read-color)))
811 (modify-frame-parameters (selected-frame)
812 (list (cons 'border-color color-name))))
814 (defun auto-raise-mode (arg)
815 "Toggle whether or not the selected frame should auto-raise.
816 With arg, turn auto-raise mode on if and only if arg is positive.
817 Note that this controls Emacs's own auto-raise feature.
818 Some window managers allow you to enable auto-raise for certain windows.
819 You can use that for Emacs windows if you wish, but if you do,
820 that is beyond the control of Emacs and this command has no effect on it."
821 (interactive "P")
822 (if (null arg)
823 (setq arg
824 (if (cdr (assq 'auto-raise (frame-parameters (selected-frame))))
825 -1 1)))
826 (if (> arg 0)
827 (raise-frame (selected-frame)))
828 (modify-frame-parameters (selected-frame)
829 (list (cons 'auto-raise (> arg 0)))))
831 (defun auto-lower-mode (arg)
832 "Toggle whether or not the selected frame should auto-lower.
833 With arg, turn auto-lower mode on if and only if arg is positive.
834 Note that this controls Emacs's own auto-lower feature.
835 Some window managers allow you to enable auto-lower for certain windows.
836 You can use that for Emacs windows if you wish, but if you do,
837 that is beyond the control of Emacs and this command has no effect on it."
838 (interactive "P")
839 (if (null arg)
840 (setq arg
841 (if (cdr (assq 'auto-lower (frame-parameters (selected-frame))))
842 -1 1)))
843 (modify-frame-parameters (selected-frame)
844 (list (cons 'auto-lower (> arg 0)))))
845 (defun set-frame-name (name)
846 "Set the name of the selected frame to NAME.
847 When called interactively, prompt for the name of the frame.
848 The frame name is displayed on the modeline if the terminal displays only
849 one frame, otherwise the name is displayed on the frame's caption bar."
850 (interactive "sFrame name: ")
851 (modify-frame-parameters (selected-frame)
852 (list (cons 'name name))))
854 ;;;; Frame/display capabilities.
855 (defun display-mouse-p (&optional display)
856 "Return non-nil if DISPLAY has a mouse available.
857 DISPLAY can be a display name, a frame, or nil (meaning the selected
858 frame's display)."
859 (let ((frame-type (framep-on-display display)))
860 (cond
861 ((eq frame-type 'pc)
862 (msdos-mouse-p))
863 ((eq system-type 'windows-nt)
864 (> w32-num-mouse-buttons 0))
865 ((memq frame-type '(x mac))
866 t) ;; We assume X and Mac *always* have a pointing device
868 (or (and (featurep 'xt-mouse)
869 xterm-mouse-mode)
870 ;; t-mouse is distributed with the GPM package. It doesn't have
871 ;; a toggle.
872 (featurep 't-mouse))))))
874 (defun display-popup-menus-p (&optional display)
875 "Return non-nil if popup menus are supported on DISPLAY.
876 DISPLAY can be a display name, a frame, or nil (meaning the selected
877 frame's display).
878 Support for popup menus requires that the mouse be available."
879 (and
880 (let ((frame-type (framep-on-display display)))
881 (memq frame-type '(x w32 pc mac)))
882 (display-mouse-p display)))
884 (defun display-graphic-p (&optional display)
885 "Return non-nil if DISPLAY is a graphic display.
886 Graphical displays are those which are capable of displaying several
887 frames and several different fonts at once. This is true for displays
888 that use a window system such as X, and false for text-only terminals.
889 DISPLAY can be a display name, a frame, or nil (meaning the selected
890 frame's display)."
891 (not (null (memq (framep-on-display display) '(x w32 mac)))))
893 (defalias 'display-multi-frame-p 'display-graphic-p)
894 (defalias 'display-multi-font-p 'display-graphic-p)
896 (defun display-selections-p (&optional display)
897 "Return non-nil if DISPLAY supports selections.
898 A selection is a way to transfer text or other data between programs
899 via special system buffers called `selection' or `cut buffer' or
900 `clipboard'.
901 DISPLAY can be a display name, a frame, or nil (meaning the selected
902 frame's display)."
903 (let ((frame-type (framep-on-display display)))
904 (cond
905 ((eq frame-type 'pc)
906 ;; MS-DOG frames support selections when Emacs runs inside
907 ;; the Windows' DOS Box.
908 (not (null dos-windows-version)))
909 ((memq frame-type '(x w32 mac))
910 t) ;; FIXME?
912 nil))))
914 (defun display-screens (&optional display)
915 "Return the number of screens associated with DISPLAY."
916 (let ((frame-type (framep-on-display display)))
917 (cond
918 ((memq frame-type '(x w32))
919 (x-display-screens display))
920 (t ;; FIXME: is this correct for the Mac?
921 1))))
923 (defun display-pixel-height (&optional display)
924 "Return the height of DISPLAY's screen in pixels.
925 For character terminals, each character counts as a single pixel."
926 (let ((frame-type (framep-on-display display)))
927 (cond
928 ((memq frame-type '(x w32 mac))
929 (x-display-pixel-height display))
931 (frame-height (if (framep display) display (selected-frame)))))))
933 (defun display-pixel-width (&optional display)
934 "Return the width of DISPLAY's screen in pixels.
935 For character terminals, each character counts as a single pixel."
936 (let ((frame-type (framep-on-display display)))
937 (cond
938 ((memq frame-type '(x w32 mac))
939 (x-display-pixel-width display))
941 (frame-width (if (framep display) display (selected-frame)))))))
943 (defun display-mm-height (&optional display)
944 "Return the height of DISPLAY's screen in millimeters.
945 If the information is unavailable, value is nil."
946 (and (memq (framep-on-display display) '(x w32 mac))
947 (x-display-mm-height display)))
949 (defun display-mm-width (&optional display)
950 "Return the width of DISPLAY's screen in millimeters.
951 If the information is unavailable, value is nil."
952 (and (memq (framep-on-display display) '(x w32 mac))
953 (x-display-mm-width display)))
955 (defun display-backing-store (&optional display)
956 "Return the backing store capability of DISPLAY's screen.
957 The value may be `always', `when-mapped', `not-useful', or nil if
958 the question is inapplicable to a certain kind of display."
959 (let ((frame-type (framep-on-display display)))
960 (cond
961 ((memq frame-type '(x w32 mac))
962 (x-display-backing-store display))
964 'not-useful))))
966 (defun display-save-under (&optional display)
967 "Return non-nil if DISPLAY's screen supports the SaveUnder feature."
968 (let ((frame-type (framep-on-display display)))
969 (cond
970 ((memq frame-type '(x w32 mac))
971 (x-display-save-under display))
973 'not-useful))))
975 (defun display-planes (&optional display)
976 "Return the number of planes supported by DISPLAY."
977 (let ((frame-type (framep-on-display display)))
978 (cond
979 ((memq frame-type '(x w32 mac))
980 (x-display-planes display))
981 ((eq frame-type 'pc)
984 (truncate (log (length (tty-color-alist)) 2))))))
986 (defun display-color-cells (&optional display)
987 "Return the number of color cells supported by DISPLAY."
988 (let ((frame-type (framep-on-display display)))
989 (cond
990 ((memq frame-type '(x w32 mac))
991 (x-display-color-cells display))
992 ((eq frame-type 'pc)
995 (length (tty-color-alist))))))
997 (defun display-visual-class (&optional display)
998 "Returns the visual class of DISPLAY.
999 The value is one of the symbols `static-gray', `gray-scale',
1000 `static-color', `pseudo-color', `true-color', or `direct-color'."
1001 (let ((frame-type (framep-on-display display)))
1002 (cond
1003 ((memq frame-type '(x w32 mac))
1004 (x-display-visual-class display))
1005 ((and (memq frame-type '(pc t))
1006 (tty-display-color-p display))
1007 'static-color)
1009 'static-gray))))
1012 ;;;; Aliases for backward compatibility with Emacs 18.
1013 (defalias 'screen-height 'frame-height)
1014 (defalias 'screen-width 'frame-width)
1016 (defun set-screen-width (cols &optional pretend)
1017 "Obsolete function to change the size of the screen to COLS columns.
1018 Optional second arg non-nil means that redisplay should use COLS columns
1019 but that the idea of the actual width of the frame should not be changed.
1020 This function is provided only for compatibility with Emacs 18; new code
1021 should use `set-frame-width instead'."
1022 (set-frame-width (selected-frame) cols pretend))
1024 (defun set-screen-height (lines &optional pretend)
1025 "Obsolete function to change the height of the screen to LINES lines.
1026 Optional second arg non-nil means that redisplay should use LINES lines
1027 but that the idea of the actual height of the screen should not be changed.
1028 This function is provided only for compatibility with Emacs 18; new code
1029 should use `set-frame-height' instead."
1030 (set-frame-height (selected-frame) lines pretend))
1032 (defun delete-other-frames (&optional frame)
1033 "Delete all frames except FRAME.
1034 FRAME nil or omitted means delete all frames except the selected frame."
1035 (interactive)
1036 (unless frame
1037 (setq frame (selected-frame)))
1038 (mapcar 'delete-frame (delq frame (frame-list))))
1041 (make-obsolete 'screen-height 'frame-height) ;before 19.15
1042 (make-obsolete 'screen-width 'frame-width) ;before 19.15
1043 (make-obsolete 'set-screen-width 'set-frame-width) ;before 19.15
1044 (make-obsolete 'set-screen-height 'set-frame-height) ;before 19.15
1047 ;;; Highlighting trailing whitespace.
1049 (make-variable-buffer-local 'show-trailing-whitespace)
1051 (defcustom show-trailing-whitespace nil
1052 "*Non-nil means highlight trailing whitespace in face `trailing-whitespace'."
1053 :tag "Highlight trailing whitespace."
1054 :set #'(lambda (symbol value) (set-default symbol value))
1055 :type 'boolean
1056 :group 'font-lock)
1060 ;;; Scrolling
1062 (defgroup scrolling nil
1063 "Scrolling windows."
1064 :version "21.1"
1065 :group 'frames)
1067 (defcustom automatic-hscrolling t
1068 "*Allow or disallow autmatic scrolling windows horizontally.
1069 If non-nil, windows are automatically scrolled horizontally to make
1070 point visible."
1071 :version "21.1"
1072 :type 'boolean
1073 :group 'scrolling)
1076 ;;; Blinking cursor
1078 (defgroup cursor nil
1079 "Displaying text cursors."
1080 :version "21.1"
1081 :group 'frames)
1083 (defcustom blink-cursor-delay 0.5
1084 "*Seconds of idle time after which cursor starts to blink."
1085 :tag "Delay in seconds."
1086 :type 'number
1087 :group 'cursor)
1089 (defcustom blink-cursor-interval 0.5
1090 "*Length of cursor blink interval in seconds."
1091 :tag "Blink interval in seconds."
1092 :type 'number
1093 :group 'cursor)
1095 (defvar blink-cursor-idle-timer nil
1096 "Timer started after `blink-cursor-delay' seconds of Emacs idle time.
1097 The function `blink-cursor-start' is called when the timer fires.")
1099 (defvar blink-cursor-timer nil
1100 "Timer started from `blink-cursor-start'.
1101 This timer calls `blink-cursor' every `blink-cursor-interval' seconds.")
1103 (defvar blink-cursor-mode nil
1104 "Non-nil means blinking cursor is active.")
1106 (defun blink-cursor-mode (arg)
1107 "Toggle blinking cursor mode.
1108 With a numeric argument, turn blinking cursor mode on iff ARG is positive.
1109 When blinking cursor mode is enabled, the cursor of the selected
1110 window blinks."
1111 (interactive "P")
1112 (let ((on-p (if (null arg)
1113 (not blink-cursor-mode)
1114 (> (prefix-numeric-value arg) 0))))
1115 (if blink-cursor-idle-timer
1116 (cancel-timer blink-cursor-idle-timer))
1117 (if blink-cursor-timer
1118 (cancel-timer blink-cursor-timer))
1119 (setq blink-cursor-idle-timer nil
1120 blink-cursor-timer nil
1121 blink-cursor-mode nil)
1122 (if on-p
1123 (progn
1124 ;; Hide the cursor.
1125 ;(internal-show-cursor nil nil)
1126 (setq blink-cursor-idle-timer
1127 (run-with-idle-timer blink-cursor-delay
1128 blink-cursor-delay
1129 'blink-cursor-start))
1130 (setq blink-cursor-mode t))
1131 (internal-show-cursor nil t))))
1133 ;; Note that this is really initialized from startup.el before
1134 ;; the init-file is read.
1136 (defcustom blink-cursor nil
1137 "*Non-nil means blinking cursor mode is active."
1138 :group 'cursor
1139 :tag "Blinking cursor"
1140 :type 'boolean
1141 :set #'(lambda (symbol value)
1142 (set-default symbol value)
1143 (blink-cursor-mode (or value 0))))
1145 (defun blink-cursor-start ()
1146 "Timer function called from the timer `blink-cursor-idle-timer'.
1147 This starts the timer `blink-cursor-timer', which makes the cursor blink
1148 if appropriate. It also arranges to cancel that timer when the next
1149 command starts, by installing a pre-command hook."
1150 (when (null blink-cursor-timer)
1151 (add-hook 'pre-command-hook 'blink-cursor-end)
1152 (setq blink-cursor-timer
1153 (run-with-timer blink-cursor-interval blink-cursor-interval
1154 'blink-cursor-timer-function))))
1156 (defun blink-cursor-timer-function ()
1157 "Timer function of timer `blink-cursor-timer'."
1158 (internal-show-cursor nil (not (internal-show-cursor-p))))
1160 (defun blink-cursor-end ()
1161 "Stop cursor blinking.
1162 This is installed as a pre-command hook by `blink-cursor-start'.
1163 When run, it cancels the timer `blink-cursor-timer' and removes
1164 itself as a pre-command hook."
1165 (remove-hook 'pre-command-hook 'blink-cursor-end)
1166 (internal-show-cursor nil t)
1167 (cancel-timer blink-cursor-timer)
1168 (setq blink-cursor-timer nil))
1172 ;;; Busy-cursor.
1174 (defcustom busy-cursor t
1175 "*Non-nil means show a busy-cursor when running under a window system."
1176 :tag "Busy-cursor"
1177 :type 'boolean
1178 :group 'cursor
1179 :get #'(lambda (symbol) display-busy-cursor)
1180 :set #'(lambda (symbol value)
1181 (set-default symbol value)
1182 (setq display-busy-cursor value)))
1184 (defcustom busy-cursor-delay-seconds 1
1185 "*Seconds to wait before displaying a busy-cursor."
1186 :tag "Busy-cursor delay"
1187 :type 'number
1188 :group 'cursor
1189 :get #'(lambda (symbol) busy-cursor-delay)
1190 :set #'(lambda (symbol value)
1191 (set-default symbol value)
1192 (setq busy-cursor-delay value)))
1195 (defcustom show-cursor-in-non-selected-windows t
1196 "*Non-nil means show a hollow box cursor in non-selected-windows.
1197 If nil, don't show a cursor except in the selected window.
1198 Setting this variable directly has no effect; use custom instead
1199 (or set the variable `cursor-in-non-selected-windows')."
1200 :tag "Cursor in non-selected windows"
1201 :type 'boolean
1202 :group 'cursor
1203 :get #'(lambda (symbol) cursor-in-non-selected-windows)
1204 :set #'(lambda (symbol value)
1205 (set-default symbol value)
1206 (setq cursor-in-non-selected-windows value)
1207 (force-mode-line-update t)))
1210 ;;;; Key bindings
1212 (define-key ctl-x-5-map "2" 'make-frame-command)
1213 (define-key ctl-x-5-map "1" 'delete-other-frames)
1214 (define-key ctl-x-5-map "0" 'delete-frame)
1215 (define-key ctl-x-5-map "o" 'other-frame)
1217 (provide 'frame)
1219 ;;; frame.el ends here