1 ;;; frame.el --- multi-frame management independent of window systems.
3 ;; Copyright (C) 1993-4, 1997 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995, 1996 Ben Wing.
6 ;; Maintainer: SXEmacs Development Team
7 ;; Keywords: internal, dumped
9 ;; This file is part of SXEmacs.
11 ;; SXEmacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; SXEmacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Synched up with: FSF 19.30.
28 ;; This file is dumped with SXEmacs.
33 "Support for Emacs frames and window systems."
36 ; No need for `frame-creation-function'.
38 ;;; The initial value given here for this must ask for a minibuffer.
39 ;;; There must always exist a frame with a minibuffer, and after we
40 ;;; delete the terminal frame, this will be the only frame.
41 (defcustom initial-frame-plist '(minibuffer t)
42 "Plist of frame properties for creating the initial X window frame.
43 You can set this in your `.emacs' file; for example,
44 (setq initial-frame-plist '(top 1 left 1 width 80 height 55))
45 Properties specified here supersede the values given in `default-frame-plist'.
46 The format of this can also be an alist for backward compatibility.
48 If the value calls for a frame without a minibuffer, and you have not created
49 a minibuffer frame on your own, one is created according to
50 `minibuffer-frame-plist'.
52 You can specify geometry-related options for just the initial frame
53 by setting this variable in your `.emacs' file; however, they won't
54 take effect until Emacs reads `.emacs', which happens after first creating
55 the frame. If you want the frame to have the proper geometry as soon
56 as it appears, you need to use this three-step process:
57 * Specify X resources to give the geometry you want.
58 * Set `default-frame-plist' to override these options so that they
59 don't affect subsequent frames.
60 * Set `initial-frame-plist' in a way that matches the X resources,
61 to override what you put in `default-frame-plist'."
65 (defcustom minibuffer-frame-plist '(width 80 height 2 menubar-visible-p nil
66 default-toolbar-visible-p nil)
67 "Plist of frame properties for initially creating a minibuffer frame.
68 You can set this in your `.emacs' file; for example,
69 (setq minibuffer-frame-plist '(top 1 left 1 width 80 height 2))
70 Properties specified here supersede the values given in
71 `default-frame-plist'.
72 The format of this can also be an alist for backward compatibility."
76 (defcustom pop-up-frame-plist nil
77 "Plist of frame properties used when creating pop-up frames.
78 Pop-up frames are used for completions, help, and the like.
79 This variable can be set in your init file, like this:
80 (setq pop-up-frame-plist '(width 80 height 20))
81 These supersede the values given in `default-frame-plist'.
82 The format of this can also be an alist for backward compatibility."
86 (setq pop-up-frame-function
88 (make-frame pop-up-frame-plist))))
90 (defcustom special-display-frame-plist '(height 14 width 80 unsplittable t)
91 "*Plist of frame properties used when creating special frames.
92 Special frames are used for buffers whose names are in
93 `special-display-buffer-names' and for buffers whose names match
94 one of the regular expressions in `special-display-regexps'.
95 This variable can be set in your init file, like this:
96 (setq special-display-frame-plist '(width 80 height 20))
97 These supersede the values given in `default-frame-plist'.
98 The format of this can also be an alist for backward compatibility."
102 (defun safe-alist-to-plist (cruftiness)
103 (if (consp (car cruftiness))
104 (alist-to-plist cruftiness)
107 ;; Display BUFFER in its own frame, reusing an existing window if any.
108 ;; Return the window chosen.
109 ;; Currently we do not insist on selecting the window within its frame.
110 ;; If ARGS is a plist, use it as a list of frame property specs.
111 ;; #### Change, not compatible with FSF: This stuff is all so incredibly
112 ;; junky anyway that I doubt it makes any difference.
113 ;; If ARGS is a list whose car is t,
114 ;; use (cadr ARGS) as a function to do the work.
115 ;; Pass it BUFFER as first arg, and (cddr ARGS) gives the rest of the args.
116 (defun special-display-popup-frame (buffer &optional args)
117 ;; if we can't display simultaneous multiple frames, just return
118 ;; nil and let the normal behavior take over.
119 (and (device-on-window-system-p)
120 (if (and args (eq t (car args)))
121 (apply (cadr args) buffer (cddr args))
122 (let ((window (get-buffer-window buffer t)))
124 ;; If we have a window already, make it visible.
125 (let ((frame (window-frame window)))
126 (make-frame-visible frame)
129 ;; If no window yet, make one in a new frame.
131 (make-frame (append (safe-alist-to-plist args)
133 special-display-frame-plist)))))
134 (set-window-buffer (frame-selected-window frame) buffer)
135 (set-window-dedicated-p (frame-selected-window frame) t)
136 (frame-selected-window frame)))))))
138 (setq special-display-function 'special-display-popup-frame)
140 ;;; Handle delete-frame events from the X server.
141 ;(defun handle-delete-frame (event)
143 ; (let ((frame (posn-window (event-start event)))
145 ; (tail (frame-list)))
147 ; (and (frame-visible-p (car tail))
148 ; (not (eq (car tail) frame))
150 ; (setq tail (cdr tail)))
152 ; (delete-frame frame t)
156 ;;;; Arrangement of frames at startup
158 ;;; 1) Load the window system startup file from the lisp library and read the
159 ;;; high-priority arguments (-q and the like). The window system startup
160 ;;; file should create any frames specified in the window system defaults.
162 ;;; 2) If no frames have been opened, we open an initial text frame.
164 ;;; 3) Once the init file is done, we apply any newly set properties
165 ;;; in initial-frame-plist to the frame.
167 ;; These are now called explicitly at the proper times,
168 ;; since that is easier to understand.
169 ;; Actually using hooks within Emacs is bad for future maintenance. --rms.
170 ;; (add-hook 'before-init-hook 'frame-initialize)
171 ;; (add-hook 'window-setup-hook 'frame-notice-user-settings)
173 ;;; If we create the initial frame, this is it.
174 (defvar frame-initial-frame nil)
176 ;; Record the properties used in frame-initialize to make the initial frame.
177 (defvar frame-initial-frame-plist)
179 (defvar frame-initial-geometry-arguments nil)
181 (defun canonicalize-frame-plists ()
182 (setq initial-frame-plist (safe-alist-to-plist initial-frame-plist))
183 (setq default-frame-plist (safe-alist-to-plist default-frame-plist)))
185 ;;; startup.el calls this function before loading the user's init
186 ;;; file - if there is no frame with a minibuffer open now, create
187 ;;; one to display messages while loading the init file.
188 (defun frame-initialize ()
189 ;; In batch mode, we actually use the initial terminal device for output.
190 (canonicalize-frame-plists)
191 (if (not (noninteractive))
193 ;; Don't call select-frame here - focus is a matter of WM policy.
195 ;; If there is no frame with a minibuffer besides the terminal
196 ;; frame, then we need to create the opening frame. Make sure
197 ;; it has a minibuffer, but let initial-frame-plist omit the
199 (or (delq terminal-frame (minibuffer-frame-list))
201 (setq frame-initial-frame-plist
202 (append initial-frame-plist default-frame-plist))
203 ;; FSFmacs has scroll-bar junk here that we don't need.
204 (setq default-minibuffer-frame
205 (setq frame-initial-frame
206 (make-frame initial-frame-plist
207 (car (delq terminal-device
209 ;; Delete any specifications for window geometry properties
210 ;; so that we won't reapply them in frame-notice-user-settings.
211 ;; It would be wrong to reapply them then,
212 ;; because that would override explicit user resizing.
213 (setq initial-frame-plist
214 (frame-remove-geometry-props initial-frame-plist))))
215 ;; At this point, we know that we have a frame open, so we
216 ;; can delete the terminal device.
217 ;; (delete-device terminal-device)
218 ;; Do it the same way Fkill_emacs does it. -slb
219 (delete-console terminal-console)
220 (setq terminal-frame nil)
222 ;; FSFmacs sets frame-creation-function here, but no need.
225 ;;; startup.el calls this function after loading the user's init
226 ;;; file. Now default-frame-plist and initial-frame-plist contain
227 ;;; information to which we must react; do what needs to be done.
228 (defun frame-notice-user-settings ()
230 ;; FSFmacs has menu-bar junk here that we don't need.
232 (canonicalize-frame-plists)
234 ;; Creating and deleting frames may shift the selected frame around,
235 ;; and thus the current buffer. Protect against that. We don't
236 ;; want to use save-excursion here, because that may also try to set
237 ;; the buffer of the selected window, which fails when the selected
238 ;; window is the minibuffer.
239 (let ((old-buffer (current-buffer)))
241 ;; If the initial frame is still around, apply initial-frame-plist
242 ;; and default-frame-plist to it.
243 (if (frame-live-p frame-initial-frame)
245 ;; The initial frame we create above always has a minibuffer.
246 ;; If the user wants to remove it, or make it a minibuffer-only
247 ;; frame, then we'll have to delete the selected frame and make a
248 ;; new one; you can't remove or add a root window to/from an
251 ;; NOTE: default-frame-plist was nil when we created the
252 ;; existing frame. We need to explicitly include
253 ;; default-frame-plist in the properties of the screen we
254 ;; create here, so that its new value, gleaned from the user's
255 ;; .emacs file, will be applied to the existing screen.
257 (or (and (lax-plist-member
258 initial-frame-plist 'minibuffer)
259 (list (lax-plist-get initial-frame-plist
261 (and (lax-plist-member default-frame-plist
263 (list (lax-plist-get default-frame-plist
267 ;; Create the new frame.
270 ;; If the frame isn't visible yet, wait till it is.
271 ;; If the user has to position the window,
272 ;; Emacs doesn't know its real position until
273 ;; the frame is seen to be visible.
275 (if (frame-property frame-initial-frame 'initially-unmapped)
277 (while (not (frame-visible-p frame-initial-frame))
279 (setq props (frame-properties frame-initial-frame))
280 ;; Get rid of `name' unless it was specified explicitly before.
281 (or (lax-plist-member frame-initial-frame-plist 'name)
282 (setq props (lax-plist-remprop props 'name)))
283 (setq props (append initial-frame-plist default-frame-plist
286 ;; Get rid of `reverse', because that was handled
287 ;; when we first made the frame.
288 (laxputf props 'reverse nil)
289 ;; Get rid of `window-id', otherwise make-frame will
290 ;; think we're trying to setup an external widget.
291 (laxremf props 'window-id)
292 (if (lax-plist-member frame-initial-geometry-arguments 'height)
293 (laxremf props 'height))
294 (if (lax-plist-member frame-initial-geometry-arguments 'width)
295 (laxremf props 'width))
296 (if (lax-plist-member frame-initial-geometry-arguments 'left)
297 (laxremf props 'left))
298 (if (lax-plist-member frame-initial-geometry-arguments 'top)
299 (laxremf props 'top))
301 ;; Now create the replacement initial frame.
303 ;; Use the geometry args that created the existing
304 ;; frame, rather than the props we get for it.
305 (append '(user-size t user-position t)
306 frame-initial-geometry-arguments
308 ;; The initial frame, which we are about to delete, may be
309 ;; the only frame with a minibuffer. If it is, create a
311 (or (delq frame-initial-frame (minibuffer-frame-list))
312 (make-initial-minibuffer-frame nil))
314 ;; If the initial frame is serving as a surrogate
315 ;; minibuffer frame for any frames, we need to wean them
316 ;; onto a new frame. The default-minibuffer-frame
317 ;; variable must be handled similarly.
318 (let ((users-of-initial
321 (and (not (eq frame frame-initial-frame))
323 (minibuffer-window frame))
324 frame-initial-frame))))))
325 (if (or users-of-initial
326 (eq default-minibuffer-frame frame-initial-frame))
328 ;; Choose an appropriate frame. Prefer frames which
329 ;; are only minibuffers.
330 (let* ((new-surrogate
332 (or (filtered-frame-list
335 (frame-property frame 'minibuffer))))
336 (minibuffer-frame-list))))
337 (new-minibuffer (minibuffer-window new-surrogate)))
339 (if (eq default-minibuffer-frame frame-initial-frame)
340 (setq default-minibuffer-frame new-surrogate))
342 ;; Wean the frames using frame-initial-frame as
343 ;; their minibuffer frame.
347 (set-frame-property frame 'minibuffer
351 ;; Redirect events enqueued at this frame to the new frame.
352 ;; Is this a good idea?
353 ;; Probably not, since this whole redirect-frame-focus
354 ;; stuff is a load of trash, and so is this function we're in.
356 ;(redirect-frame-focus frame-initial-frame new)
358 ;; Finally, get rid of the old frame.
359 (delete-frame frame-initial-frame t))
361 ;; Otherwise, we don't need all that rigamarole; just apply
362 ;; the new properties.
363 (let (newprops allprops tail)
364 (setq allprops (append initial-frame-plist
365 default-frame-plist))
366 (if (lax-plist-member frame-initial-geometry-arguments 'height)
367 (laxremf allprops 'height))
368 (if (lax-plist-member frame-initial-geometry-arguments 'width)
369 (remf allprops 'width))
370 (if (lax-plist-member frame-initial-geometry-arguments 'left)
371 (laxremf allprops 'left))
372 (if (lax-plist-member frame-initial-geometry-arguments 'top)
373 (laxremf allprops 'top))
375 ;; Find just the props that have changed since we first
376 ;; made this frame. Those are the ones actually set by
377 ;; the init file. For those props whose values we already knew
378 ;; (such as those spec'd by command line options)
379 ;; it is undesirable to specify the parm again
380 ;; once the user has seen the frame and been able to alter it
384 (setq oldval (lax-plist-get frame-initial-frame-plist
386 (setq newval (lax-plist-get allprops (car tail)))
387 (or (eq oldval newval)
388 (laxputf newprops (car tail) newval)))
389 (setq tail (cddr tail)))
390 (set-frame-properties frame-initial-frame newprops)
392 ;if (lax-plist-member newprops 'font)
393 ; (frame-update-faces frame-initial-frame))
397 ;; Restore the original buffer.
398 (set-buffer old-buffer)
400 ;; Make sure the initial frame can be GC'd if it is ever deleted.
401 ;; Make sure frame-notice-user-settings does nothing if called twice.
402 (setq frame-initial-frame nil)))
404 (defun make-initial-minibuffer-frame (device)
405 (let ((props (append '(minibuffer only)
406 (safe-alist-to-plist minibuffer-frame-plist))))
407 (make-frame props device)))
410 ;;;; Creation of additional frames, and other frame miscellanea
412 (defun get-other-frame ()
413 "Return some frame other than the selected frame, creating one if necessary."
414 (let* ((this (selected-frame))
415 ;; search visible frames first
416 (next (next-frame this 'visible-nomini)))
417 ;; then search iconified frames
419 (setq next (next-frame 'visible-iconic-nomini)))
421 ;; otherwise, make a new frame
425 (defun next-multiframe-window ()
426 "Select the next window, regardless of which frame it is on."
428 (select-window (next-window (selected-window)
429 (> (minibuffer-depth) 0)
432 (defun previous-multiframe-window ()
433 "Select the previous window, regardless of which frame it is on."
435 (select-window (previous-window (selected-window)
436 (> (minibuffer-depth) 0)
439 (defun make-frame-on-device (type connection &optional props)
440 "Create a frame of type TYPE on CONNECTION.
441 TYPE should be a symbol naming the device type, i.e. one of
443 x An X display. CONNECTION should be a standard display string
444 such as \"unix:0\", or nil for the display specified on the
445 command line or in the DISPLAY environment variable. Only if
446 support for X was compiled into XEmacs.
447 tty A standard TTY connection or terminal. CONNECTION should be
448 a TTY device name such as \"/dev/ttyp2\" (as determined by
449 the Unix command `tty') or nil for XEmacs' standard input
450 and output (usually the TTY in which XEmacs started). Only
451 if support for TTY's was compiled into XEmacs.
452 ns A connection to a machine running the NeXTstep windowing
453 system. Not currently implemented.
454 pc A direct-write MS-DOS frame. Not currently implemented.
456 PROPS should be a plist of properties, as in the call to `make-frame'.
458 If a connection to CONNECTION already exists, it is reused; otherwise,
459 a new connection is opened."
460 (make-frame props (make-device type connection props)))
462 ;; Alias, kept temporarily.
463 (defalias 'new-frame 'make-frame)
465 ; FSFmacs has make-frame here. We have it in C, so no need for
466 ; frame-creation-function.
468 (defun filtered-frame-list (predicate &optional device)
469 "Return a list of all live frames which satisfy PREDICATE.
470 If optional second arg DEVICE is non-nil, restrict the frames
471 returned to that device."
472 (let ((frames (if device (device-frame-list device)
475 (while (consp frames)
476 (if (funcall predicate (car frames))
477 (setq good-frames (cons (car frames) good-frames)))
478 (setq frames (cdr frames)))
481 (defun minibuffer-frame-list (&optional device)
482 "Return a list of all frames with their own minibuffers.
483 If optional second arg DEVICE is non-nil, restrict the frames
484 returned to that device."
487 (eq frame (window-frame (minibuffer-window frame))))
490 (defun frame-minibuffer-only-p (frame)
491 "Return non-nil if FRAME is a minibuffer-only frame."
492 (eq (frame-root-window frame) (minibuffer-window frame)))
494 (defun frame-remove-geometry-props (plist)
495 "Return the property list PLIST, but with geometry specs removed.
496 This deletes all bindings in PLIST for `top', `left', `width',
497 `height', `user-size' and `user-position' properties.
498 Emacs uses this to avoid overriding explicit moves and resizings from
499 the user during startup."
500 (setq plist (canonicalize-lax-plist (copy-sequence plist)))
501 (mapcar #'(lambda (property)
502 (if (lax-plist-member plist property)
504 (setq frame-initial-geometry-arguments
506 (cons (lax-plist-get plist property)
507 frame-initial-geometry-arguments)))
508 (setq plist (lax-plist-remprop plist property)))))
509 '(height width top left user-size user-position))
512 (defun other-frame (arg)
513 "Select the ARG'th different visible frame, and raise it.
514 All frames are arranged in a cyclic order.
515 This command selects the frame ARG steps away in that order.
516 A negative ARG moves in the opposite order.
518 This sets the window system focus, regardless of the value
519 of `focus-follows-mouse'."
521 (let ((frame (selected-frame)))
523 (setq frame (next-frame frame 'visible-nomini))
526 (setq frame (previous-frame frame 'visible-nomini))
530 ;this is a bad idea; you should in general never warp the
531 ;pointer unless the user asks for this. Furthermore,
532 ;our version of `set-mouse-position' takes a window,
534 ;(set-mouse-position (selected-frame) (1- (frame-width)) 0)
535 ;some weird FSFmacs randomness
536 ;(if (fboundp 'unfocus-frame)
540 ;; XEmacs-added utility functions
542 (defmacro save-selected-frame (&rest body)
543 "Execute forms in BODY, then restore the selected frame.
544 The value returned is the value of the last form in BODY."
545 (let ((old-frame (gensym "ssf")))
546 `(let ((,old-frame (selected-frame)))
549 (select-frame ,old-frame)))))
551 (defmacro with-selected-frame (frame &rest body)
552 "Execute forms in BODY with FRAME as the selected frame.
553 The value returned is the value of the last form in BODY."
554 `(save-selected-frame
555 (select-frame ,frame)
558 ; this is in C in FSFmacs
560 "Return a list of all frames on all devices/consoles."
561 ;; Lists are copies, so nconc is safe here.
562 (apply 'nconc (mapcar 'device-frame-list (device-list))))
564 (defun frame-type (&optional frame)
565 "Return the type of the specified frame (e.g. `x' or `tty').
566 This is equivalent to the type of the frame's device.
567 Value is `tty' for a tty frame (a character-only terminal),
568 `x' for a frame that is an X window,
569 `ns' for a frame that is a NeXTstep window (not yet implemented),
570 `stream' for a stream frame (which acts like a stdio stream), and
571 `dead' for a deleted frame."
572 (or frame (setq frame (selected-frame)))
573 (if (not (frame-live-p frame)) 'dead
574 (device-type (frame-device frame))))
576 (defun device-or-frame-p (object)
577 "Return non-nil if OBJECT is a device or frame."
581 (defun device-or-frame-type (device-or-frame)
582 "Return the type (e.g. `x' or `tty') of DEVICE-OR-FRAME.
583 DEVICE-OR-FRAME should be a device or a frame object. See `device-type'
584 for a description of the possible types."
585 (if (devicep device-or-frame)
586 (device-type device-or-frame)
587 (frame-type device-or-frame)))
589 (defun fw-frame (obj)
590 "Given a frame or window, return the associated frame.
591 Return nil otherwise."
592 (cond ((windowp obj) (window-frame obj))
597 ;;;; Frame configurations
599 (defun current-frame-configuration ()
600 "Return a list describing the positions and states of all frames.
601 Its car is `frame-configuration'.
602 Each element of the cdr is a list of the form (FRAME PLIST WINDOW-CONFIG),
604 FRAME is a frame object,
605 PLIST is a property list specifying some of FRAME's properties, and
606 WINDOW-CONFIG is a window configuration object for FRAME."
607 (cons 'frame-configuration
611 (frame-properties frame)
612 (current-window-configuration frame))))
615 (defun set-frame-configuration (configuration &optional nodelete)
616 "Restore the frames to the state described by CONFIGURATION.
617 Each frame listed in CONFIGURATION has its position, size, window
618 configuration, and other properties set as specified in CONFIGURATION.
619 Ordinarily, this function deletes all existing frames not
620 listed in CONFIGURATION. But if optional second argument NODELETE
621 is given and non-nil, the unwanted frames are iconified instead."
622 (or (frame-configuration-p configuration)
623 (signal 'wrong-type-argument
624 (list 'frame-configuration-p configuration)))
625 (let ((config-plist (cdr configuration))
627 (mapc (lambda (frame)
628 (let ((properties (assq frame config-plist)))
631 (set-frame-properties
633 ;; Since we can't set a frame's minibuffer status,
634 ;; we might as well omit the parameter altogether.
635 (lax-plist-remprop (nth 1 properties) 'minibuffer))
636 (set-window-configuration (nth 2 properties)))
637 (setq frames-to-delete (cons frame frames-to-delete)))))
640 ;; Note: making frames invisible here was tried
641 ;; but led to some strange behavior--each time the frame
642 ;; was made visible again, the window manager asked afresh
643 ;; for where to put it.
644 (mapc 'iconify-frame frames-to-delete)
645 (mapc 'delete-frame frames-to-delete))))
647 ; this function is in subr.el in FSFmacs.
648 ; that's because they don't always include frame.el, while we do.
650 (defun frame-configuration-p (object)
651 "Return non-nil if OBJECT seems to be a frame configuration.
652 Any list whose car is `frame-configuration' is assumed to be a frame
655 (eq (car object) 'frame-configuration)))
658 ;; FSFmacs has functions `frame-width', `frame-height' here.
659 ;; We have them in C.
661 ;; FSFmacs has weird functions `set-default-font', `set-background-color',
662 ;; `set-foreground-color' here. They don't do sensible things like
663 ;; set faces; instead they set frame properties (??!!) and call
664 ;; useless functions such as `frame-update-faces' and
665 ;; `frame-update-face-colors'.
667 ;; FSFmacs has functions `set-cursor-color', `set-mouse-color', and
668 ;; `set-border-color', which refer to frame properties.
669 ;; #### We need to use specifiers here.
671 ;(defun auto-raise-mode (arg)
672 ; "Toggle whether or not the selected frame should auto-raise.
673 ;With arg, turn auto-raise mode on if and only if arg is positive.
674 ;Note that this controls Emacs's own auto-raise feature.
675 ;Some window managers allow you to enable auto-raise for certain windows.
676 ;You can use that for Emacs windows if you wish, but if you do,
677 ;that is beyond the control of Emacs and this command has no effect on it."
681 ; (if (frame-property (selected-frame) 'auto-raise)
683 ; (set-frame-property (selected-frame) 'auto-raise (> arg 0)))
685 ;(defun auto-lower-mode (arg)
686 ; "Toggle whether or not the selected frame should auto-lower.
687 ;With arg, turn auto-lower mode on if and only if arg is positive.
688 ;Note that this controls Emacs's own auto-lower feature.
689 ;Some window managers allow you to enable auto-lower for certain windows.
690 ;You can use that for Emacs windows if you wish, but if you do,
691 ;that is beyond the control of Emacs and this command has no effect on it."
695 ; (if (frame-property (selected-frame) 'auto-lower)
697 ; (set-frame-property (selected-frame) 'auto-lower (> arg 0)))
699 ;; FSFmacs has silly functions `toggle-scroll-bar',
700 ;; `toggle-horizontal-scrollbar'
702 ;;; Iconifying emacs.
704 ;;; The function iconify-emacs replaces every non-iconified emacs window
705 ;;; with a *single* icon. Iconified emacs windows are left alone. When
706 ;;; emacs is in this globally-iconified state, de-iconifying any emacs icon
707 ;;; will uniconify all frames that were visible, and iconify all frames
708 ;;; that were not. This is done by temporarily changing the value of
709 ;;; `map-frame-hook' to `deiconify-emacs' (which should never be called
710 ;;; except from the map-frame-hook while emacs is iconified).
712 ;;; The title of the icon representing all emacs frames is controlled by
713 ;;; the variable `icon-name'. This is done by temporarily changing the
714 ;;; value of `frame-icon-title-format'. Unfortunately, this changes the
715 ;;; titles of all emacs icons, not just the "big" icon.
717 ;;; It would be nice if existing icons were removed and restored by
718 ;;; iconifying the emacs process, but I couldn't make that work yet.
720 (defvar icon-name nil) ; set this at run time, not load time.
722 (defvar iconification-data nil)
724 (defun iconify-emacs ()
725 "Replace every non-iconified FRAME with a *single* icon.
726 Iconified frames are left alone. When XEmacs is in this
727 globally-iconified state, de-iconifying any emacs icon will uniconify
728 all frames that were visible, and iconify all frames that were not."
730 (if iconification-data (error "already iconified?"))
731 (let* ((frames (frame-list))
733 (me (selected-frame))
736 (setq frame (car rest))
737 (setcar rest (cons frame (frame-visible-p frame)))
738 ; (if (memq (cdr (car rest)) '(icon nil))
740 ; (make-frame-visible frame) ; deiconify, and process the X event
741 ; (sleep-for 500 t) ; process X events; I really want to XSync() here
743 (or (eq frame me) (make-frame-invisible frame))
744 (setq rest (cdr rest)))
745 (or (boundp 'map-frame-hook) (setq map-frame-hook nil))
747 (setq icon-name (concat invocation-name " @ " (system-name))))
748 (setq iconification-data
749 (list frame-icon-title-format map-frame-hook frames)
750 frame-icon-title-format icon-name
751 map-frame-hook 'deiconify-emacs)
755 (defun deiconify-emacs (&optional ignore)
756 (or iconification-data (error "not iconified?"))
757 (setq frame-icon-title-format (car iconification-data)
758 map-frame-hook (car (cdr iconification-data))
759 iconification-data (car (cdr (cdr iconification-data))))
760 (while iconification-data
761 (let ((visibility (cdr (car iconification-data))))
762 (cond (visibility ;; JV (Note non-nil means visible in XEmacs)
763 (make-frame-visible (car (car iconification-data))))
764 ; (t ;; (eq visibility 'icon) ;; JV Not in XEmacs!!!
765 ; (make-frame-visible (car (car iconification-data)))
766 ; (sleep-for 500 t) ; process X events; I really want to XSync() here
767 ; (iconify-frame (car (car iconification-data))))
770 (setq iconification-data (cdr iconification-data))))
772 (defun suspend-or-iconify-emacs ()
773 "Call iconify-emacs if using a window system, otherwise suspend Emacs."
775 (cond ((device-on-window-system-p)
777 ((and (eq (device-type) 'tty)
778 (console-tty-controlling-process (selected-console)))
779 (suspend-console (selected-console)))
783 ;; This is quite a mouthful, but it should be descriptive, as it's
784 ;; bound to C-z. FSF takes the easy way out by binding C-z to
785 ;; different things depending on window-system. We can't do the same,
786 ;; because we allow simultaneous X and TTY consoles.
787 (defun suspend-emacs-or-iconify-frame ()
788 "Iconify the selected frame if using a window system, otherwise suspend Emacs."
790 (cond ((device-on-window-system-p)
792 ((and (eq (frame-type) 'tty)
793 (console-tty-controlling-process (selected-console)))
794 (suspend-console (selected-console)))
799 ;;; auto-raise and auto-lower
801 (defcustom auto-raise-frame nil
802 "*If true, frames will be raised to the top when selected.
803 Under X, most ICCCM-compliant window managers will have an option to do this
804 for you, but this variable is provided in case you're using a broken WM."
808 (defcustom auto-lower-frame nil
809 "*If true, frames will be lowered to the bottom when no longer selected.
810 Under X, most ICCCM-compliant window managers will have an option to do this
811 for you, but this variable is provided in case you're using a broken WM."
815 (defun default-select-frame-hook ()
816 "Implement the `auto-raise-frame' variable.
817 For use as the value of `select-frame-hook'."
818 (if auto-raise-frame (raise-frame (selected-frame))))
820 (defun default-deselect-frame-hook ()
821 "Implement the `auto-lower-frame' variable.
822 For use as the value of `deselect-frame-hook'."
823 (if auto-lower-frame (lower-frame (selected-frame)))
824 (highlight-extent nil nil))
826 (or select-frame-hook
827 (add-hook 'select-frame-hook 'default-select-frame-hook))
829 (or deselect-frame-hook
830 (add-hook 'deselect-frame-hook 'default-deselect-frame-hook))
833 ;;; Application-specific frame-management
835 (defcustom get-frame-for-buffer-default-frame-name nil
836 "*The default frame to select; see doc of `get-frame-for-buffer'."
840 (defcustom get-frame-for-buffer-default-instance-limit nil
841 "*The default instance limit for creating new frames;
842 see doc of `get-frame-for-buffer'."
846 (defun get-frame-name-for-buffer (buffer)
847 (let ((mode (and (get-buffer buffer)
848 (save-excursion (set-buffer buffer)
850 (or (get mode 'frame-name)
851 get-frame-for-buffer-default-frame-name)))
853 (defun get-frame-for-buffer-make-new-frame (buffer &optional frame-name plist)
854 (let* ((fr (make-frame plist))
855 (w (frame-root-window fr)))
857 ;; Make the one buffer being displayed in this newly created
858 ;; frame be the buffer of interest, instead of something
859 ;; random, so that it won't be shown in two-window mode.
860 ;; Avoid calling switch-to-buffer here, since that's something
861 ;; people might want to call this routine from.
863 ;; (If the root window doesn't have a buffer, then that means
864 ;; there is more than one window on the frame, which can only
865 ;; happen if the user has done something funny on the frame-
866 ;; creation-hook. If that's the case, leave it alone.)
868 (if (window-buffer w)
869 (set-window-buffer w buffer))
872 (defcustom get-frame-for-buffer-default-to-current nil
873 "*When non-nil, `get-frame-for-buffer' will default to the selected frame."
877 (defun get-frame-for-buffer-noselect (buffer
878 &optional not-this-window-p on-frame)
879 "Return a frame in which to display BUFFER.
880 This is a subroutine of `get-frame-for-buffer' (which see)."
883 ((or on-frame (eq (selected-window) (minibuffer-window)))
884 ;; don't switch frames if a frame was specified, or to list
885 ;; completions from the minibuffer, etc.
888 ((setq name (get-frame-name-for-buffer buffer))
890 ;; This buffer's mode expressed a preference for a frame of a particular
891 ;; name. That always takes priority.
893 (let ((limit (get name 'instance-limit))
894 (defaults (get name 'frame-defaults))
895 (matching-frames '())
896 frames frame already-visible)
897 ;; Sort the list so that iconic frames will be found last. They
898 ;; will be used too, but mapped frames take precedence. And
899 ;; fully visible frames come before occluded frames.
900 ;; Hidden frames come after really visible ones
904 (cond ((frame-totally-visible-p s2)
906 ((not (frame-visible-p s2))
907 (frame-visible-p s1))
908 ((eq (frame-visible-p s2) 'hidden)
909 (eq (frame-visible-p s1) t ))
910 ((not (frame-totally-visible-p s2))
911 (and (frame-visible-p s1)
912 (frame-totally-visible-p s1)))))))
913 ;; but the selected frame should come first, even if it's occluded,
914 ;; to minimize thrashing.
915 (setq frames (cons (selected-frame)
916 (delq (selected-frame) frames)))
918 (setq name (symbol-name name))
920 (setq frame (car frames))
921 (if (equal name (frame-name frame))
922 (if (get-buffer-window buffer frame)
923 (setq already-visible frame
925 (setq matching-frames (cons frame matching-frames))))
926 (setq frames (cdr frames)))
927 (cond (already-visible
929 ((or (null matching-frames)
930 (eq limit 0) ; means create with reckless abandon
931 (and limit (< (length matching-frames) limit)))
932 (get-frame-for-buffer-make-new-frame
935 (alist-to-plist (acons 'name name
936 (plist-to-alist defaults)))))
938 ;; do not switch any of the window/buffer associations in an
939 ;; existing frame; this function only picks a frame; the
940 ;; determination of which windows on it get reused is up to
941 ;; display-buffer itself.
942 ;; (or (window-dedicated-p (selected-window))
943 ;; (switch-to-buffer buffer))
944 (car matching-frames)))))
946 ((setq limit get-frame-for-buffer-default-instance-limit)
948 ;; This buffer's mode did not express a preference for a frame of a
949 ;; particular name, but the user wants a new frame rather than
950 ;; reusing the existing one.
952 (or (plist-get default-frame-plist 'name)
955 (sort (filtered-frame-list #'(lambda (x)
956 (or (frame-visible-p x)
957 (frame-iconified-p x))))
959 (cond ((and (frame-visible-p s1)
960 (not (frame-visible-p s2))))
961 ((and (eq (frame-visible-p s1) t)
962 (eq (frame-visible-p s2) 'hidden)))
963 ((and (frame-visible-p s2)
964 (not (frame-visible-p s1)))
966 ((and (equal (frame-name s1) defname)
967 (not (equal (frame-name s2) defname))))
968 ((and (equal (frame-name s2) defname)
969 (not (equal (frame-name s1) defname)))
971 ((frame-totally-visible-p s2)
974 ;; put the selected frame last. The user wants a new frame,
975 ;; so don't reuse the existing one unless forced to.
976 (setq frames (append (delq (selected-frame) frames) (list frames)))
977 (if (or (eq limit 0) ; means create with reckless abandon
978 (< (length frames) limit))
979 (get-frame-for-buffer-make-new-frame buffer)
983 (let ((w-list (windows-of-buffer buffer))
986 (second-choice (if get-frame-for-buffer-default-to-current
990 (while (and w-list (null first-choice))
993 (cond ((eq w (selected-window)) nil)
994 ((not (frame-visible-p f))
995 (if (null last-resort)
996 (setq last-resort f)))
997 ((eq f (selected-frame))
998 (setq first-choice f))
999 ((null second-choice)
1000 (setq second-choice f)))
1001 (setq w-list (cdr w-list)))
1002 (or first-choice second-choice last-resort)))
1004 (get-frame-for-buffer-default-to-current (selected-frame))
1008 ;; This buffer's mode did not express a preference for a frame of a
1009 ;; particular name. So try to find a frame already displaying this
1012 (let ((w (or (get-buffer-window buffer nil) ; check current first
1013 (get-buffer-window buffer 'visible) ; then visible
1014 (get-buffer-window buffer 0)))) ; then iconic
1016 ;; It's not in any window - return nil, meaning no frame has
1020 ;; Otherwise, return the frame of the buffer's window.
1021 (window-frame w))))))))
1024 ;; The pre-display-buffer-function is called for effect, so this needs to
1025 ;; actually select the frame it wants. Fdisplay_buffer() takes notice of
1026 ;; changes to the selected frame.
1027 (defun get-frame-for-buffer (buffer &optional not-this-window-p on-frame
1029 "Select and return a frame in which to display BUFFER.
1030 Normally, the buffer will simply be displayed in the selected frame.
1031 But if the symbol naming the major-mode of the buffer has a 'frame-name
1032 property (which should be a symbol), then the buffer will be displayed in
1033 a frame of that name. If there is no frame of that name, then one is
1036 If the major-mode doesn't have a 'frame-name property, then the frame
1037 named by `get-frame-for-buffer-default-frame-name' will be used. If
1038 that is nil (the default) then the currently selected frame will used.
1040 If the frame-name symbol has an 'instance-limit property (an integer)
1041 then each time a buffer of the mode in question is displayed, a new frame
1042 with that name will be created, until there are `instance-limit' of them.
1043 If instance-limit is 0, then a new frame will be created each time.
1045 If a buffer is already displayed in a frame, then `instance-limit' is
1046 ignored, and that frame is used.
1048 If the frame-name symbol has a 'frame-defaults property, then that is
1049 prepended to the `default-frame-plist' when creating a frame for the
1052 This function may be used as the value of `pre-display-buffer-function',
1053 to cause the `display-buffer' function and its callers to exhibit the
1055 (let ((frame (get-frame-for-buffer-noselect
1056 buffer not-this-window-p on-frame)))
1057 (if (not (eq frame (selected-frame)))
1059 (select-frame frame)
1060 (or (frame-visible-p frame)
1061 ;; If the frame was already visible, just focus on it.
1062 ;; If it wasn't visible (it was just created, or it used
1063 ;; to be iconified) then uniconify, raise, etc.
1064 (make-frame-visible frame))
1067 (defun frames-of-buffer (&optional buffer visible-only)
1068 "Return list of frames that BUFFER is currently being displayed on.
1069 If the buffer is being displayed on the currently selected frame, that frame
1070 is first in the list. VISIBLE-ONLY will only list non-iconified frames."
1071 (let ((list (windows-of-buffer buffer))
1072 (cur-frame (selected-frame))
1073 next-frame frames save-frame)
1076 (if (memq (setq next-frame (window-frame (car list)))
1079 (if (eq cur-frame next-frame)
1080 (setq save-frame next-frame)
1082 (or (not visible-only)
1083 (frame-visible-p next-frame))
1084 (setq frames (append frames (list next-frame))))))
1085 (setq list (cdr list)))
1088 (append (list save-frame) frames)
1091 (defcustom temp-buffer-shrink-to-fit nil
1092 "*When non-nil resize temporary output buffers to minimize blank lines."
1096 (defcustom temp-buffer-max-height .5
1097 "*Proportion of frame to use for temp windows."
1101 (defun show-temp-buffer-in-current-frame (buffer)
1102 "For use as the value of `temp-buffer-show-function':
1103 always displays the buffer in the selected frame, regardless of the behavior
1104 that would otherwise be introduced by the `pre-display-buffer-function', which
1105 is normally set to `get-frame-for-buffer' (which see)."
1106 (let ((pre-display-buffer-function nil)) ; turn it off, whatever it is
1107 (let ((window (display-buffer buffer nil nil temp-buffer-shrink-to-fit)))
1108 (if (not (eq (last-nonminibuf-frame) (window-frame window)))
1109 ;; only the pre-display-buffer-function should ever do this.
1110 (error "display-buffer switched frames on its own!!"))
1111 (setq minibuffer-scroll-window window)
1112 (set-window-start window 1) ; obeys narrowing
1113 (set-window-point window 1)
1116 (setq pre-display-buffer-function 'get-frame-for-buffer)
1117 (setq temp-buffer-show-function 'show-temp-buffer-in-current-frame)
1120 ;; from Bob Weiner <bweiner@pts.mot.com>, modified by Ben Wing
1121 (defun delete-other-frames (&optional frame)
1122 "Delete all but FRAME (or the selected frame)."
1124 (mapc 'delete-frame (delq (or frame (selected-frame)) (frame-list))))
1126 ;; By adding primitives to directly access the window hierarchy,
1127 ;; we can move many functions into Lisp. We do it this way
1128 ;; because the implementations are simpler in Lisp, and because
1129 ;; new functions like this can be added without requiring C
1132 (defun frame-utmost-window-2 (window position left-right-p major-end-p
1134 ;; LEFT-RIGHT-P means we're looking for the leftmost or rightmost
1135 ;; window, instead of the highest or lowest. In this case, we
1136 ;; say that the "major axis" goes left-to-right instead of top-to-
1137 ;; bottom. The "minor axis" always goes perpendicularly.
1139 ;; If MAJOR-END-P is t, we're looking for a windows that abut the
1140 ;; end (i.e. right or bottom) of the major axis, instead of the
1143 ;; If MINOR-END-P is t, then we want to start counting from the
1144 ;; end of the minor axis instead of the beginning.
1146 ;; Here's the general idea: Imagine we're trying to count the number
1147 ;; of windows that abut the top; call this function foo(). So, we
1148 ;; start with the root window. If this is a vertical combination
1149 ;; window, then foo() applied to the root window is the same as
1150 ;; foo() applied to the first child. If the root is a horizontal
1151 ;; combination window, then foo() applied to the root is the
1152 ;; same as the sum of foo() applied to each of the children.
1153 ;; Otherwise, the root window is a leaf window, and foo() is 1.
1154 ;; Now it's clear that, each time foo() encounters a leaf window,
1155 ;; it's encountering a different window that abuts the top.
1156 ;; With a little examining, you can see that foo encounters the
1157 ;; top-abutting windows in order from left to right. We can
1158 ;; modify foo() to return the nth top-abutting window by simply
1159 ;; keeping a global variable that is decremented each time
1160 ;; foo() encounters a leaf window and would return 1. If the
1161 ;; global counter gets to zero, we've encountered the window
1162 ;; we were looking for, so we exit right away using a `throw'.
1163 ;; Otherwise, we make sure that all normal paths return nil.
1166 (cond ((setq child (if left-right-p
1167 (window-first-hchild window)
1168 (window-first-vchild window)))
1170 (while (window-next-child child)
1171 (setq child (window-next-child child))))
1172 (frame-utmost-window-2 child position left-right-p major-end-p
1174 ((setq child (if left-right-p
1175 (window-first-vchild window)
1176 (window-first-hchild window)))
1178 (while (window-next-child child)
1179 (setq child (window-next-child child))))
1181 (frame-utmost-window-2 child position left-right-p major-end-p
1183 (setq child (if minor-end-p
1184 (window-previous-child child)
1185 (window-next-child child))))
1188 (setcar position (1- (car position)))
1189 (if (= (car position) 0)
1190 (throw 'fhw-exit window)
1193 (defun frame-utmost-window-1 (frame position left-right-p major-end-p)
1195 (or frame (setq frame (selected-frame)))
1196 (or position (setq position 0))
1198 (setq position (1+ position))
1199 (setq minor-end-p t)
1200 (setq position (- position)))
1202 ;; we use a cons here as a simple form of call-by-reference.
1203 ;; scheme has "boxes" for the same purpose.
1204 (frame-utmost-window-2 (frame-root-window frame) (list position)
1205 left-right-p major-end-p minor-end-p))))
1208 (defun frame-highest-window (&optional frame position)
1209 "Return the highest window on FRAME which is at POSITION.
1210 If omitted, FRAME defaults to the currently selected frame.
1211 POSITION is used to distinguish between multiple windows that abut
1212 the top of the frame: 0 means the leftmost window abutting the
1213 top of the frame, 1 the next-leftmost, etc. POSITION can also
1214 be less than zero: -1 means the rightmost window abutting the
1215 top of the frame, -2 the next-rightmost, etc.
1216 If omitted, POSITION defaults to 0, i.e. the leftmost highest window.
1217 If there is no window at the given POSITION, return nil."
1218 (frame-utmost-window-1 frame position nil nil))
1220 (defun frame-lowest-window (&optional frame position)
1221 "Return the lowest window on FRAME which is at POSITION.
1222 If omitted, FRAME defaults to the currently selected frame.
1223 POSITION is used to distinguish between multiple windows that abut
1224 the bottom of the frame: 0 means the leftmost window abutting the
1225 bottom of the frame, 1 the next-leftmost, etc. POSITION can also
1226 be less than zero: -1 means the rightmost window abutting the
1227 bottom of the frame, -2 the next-rightmost, etc.
1228 If omitted, POSITION defaults to 0, i.e. the leftmost lowest window.
1229 If there is no window at the given POSITION, return nil."
1230 (frame-utmost-window-1 frame position nil t))
1232 (defun frame-leftmost-window (&optional frame position)
1233 "Return the leftmost window on FRAME which is at POSITION.
1234 If omitted, FRAME defaults to the currently selected frame.
1235 POSITION is used to distinguish between multiple windows that abut
1236 the left edge of the frame: 0 means the highest window abutting the
1237 left edge of the frame, 1 the next-highest, etc. POSITION can also
1238 be less than zero: -1 means the lowest window abutting the
1239 left edge of the frame, -2 the next-lowest, etc.
1240 If omitted, POSITION defaults to 0, i.e. the highest leftmost window.
1241 If there is no window at the given POSITION, return nil."
1242 (frame-utmost-window-1 frame position t nil))
1244 (defun frame-rightmost-window (&optional frame position)
1245 "Return the rightmost window on FRAME which is at POSITION.
1246 If omitted, FRAME defaults to the currently selected frame.
1247 POSITION is used to distinguish between multiple windows that abut
1248 the right edge of the frame: 0 means the highest window abutting the
1249 right edge of the frame, 1 the next-highest, etc. POSITION can also
1250 be less than zero: -1 means the lowest window abutting the
1251 right edge of the frame, -2 the next-lowest, etc.
1252 If omitted, POSITION defaults to 0, i.e. the highest rightmost window.
1253 If there is no window at the given POSITION, return nil."
1254 (frame-utmost-window-1 frame position t t))
1258 ;; frame properties.
1260 (defun set-frame-property (frame prop val)
1261 "Set property PROP of FRAME to VAL. See `set-frame-properties'."
1262 (set-frame-properties frame (list prop val)))
1264 (defun frame-height (&optional frame)
1265 "Return number of lines available for display on FRAME."
1266 (frame-property frame 'height))
1268 (defun frame-width (&optional frame)
1269 "Return number of columns available for display on FRAME."
1270 (frame-property frame 'width))
1272 (put 'cursor-color 'frame-property-alias [text-cursor background])
1273 (put 'modeline 'frame-property-alias 'has-modeline-p)
1278 ;;; frame.el ends here