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