XEmacs 21.2.5
[chise/xemacs-chise.git.1] / src / frame.c
1 /* Generic frame functions.
2    Copyright (C) 1989, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
3    Copyright (C) 1995, 1996 Ben Wing.
4    Copyright (C) 1995 Sun Microsystems, Inc.
5
6 This file is part of XEmacs.
7
8 XEmacs is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with XEmacs; see the file COPYING.  If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23 /* Synched up with: FSF 19.30. */
24
25 /* This file has been Mule-ized. */
26
27 #include <config.h>
28 #include "lisp.h"
29
30 #include "buffer.h"             /* for Vbuffer_alist */
31 #include "console.h"
32 #include "events.h"
33 #include "extents.h"
34 #include "faces.h"
35 #include "frame.h"
36 #include "glyphs.h"
37 #include "menubar.h"
38 #include "redisplay.h"
39 #include "scrollbar.h"
40 #include "window.h"
41
42 Lisp_Object Vselect_frame_hook, Qselect_frame_hook;
43 Lisp_Object Vdeselect_frame_hook, Qdeselect_frame_hook;
44 Lisp_Object Vcreate_frame_hook, Qcreate_frame_hook;
45 Lisp_Object Vdelete_frame_hook, Qdelete_frame_hook;
46 Lisp_Object Vmouse_enter_frame_hook, Qmouse_enter_frame_hook;
47 Lisp_Object Vmouse_leave_frame_hook, Qmouse_leave_frame_hook;
48 Lisp_Object Vmap_frame_hook, Qmap_frame_hook;
49 Lisp_Object Vunmap_frame_hook, Qunmap_frame_hook;
50 int  allow_deletion_of_last_visible_frame;
51 Lisp_Object Vadjust_frame_function;
52 Lisp_Object Vmouse_motion_handler;
53 Lisp_Object Vsynchronize_minibuffers;
54 Lisp_Object Qsynchronize_minibuffers;
55 Lisp_Object Qbuffer_predicate;
56 Lisp_Object Qmake_initial_minibuffer_frame;
57 Lisp_Object Qcustom_initialize_frame;
58
59 /* We declare all these frame properties here even though many of them
60    are currently only used in frame-x.c, because we should generalize
61    them. */
62
63 Lisp_Object Qminibuffer;
64 Lisp_Object Qunsplittable;
65 Lisp_Object Qinternal_border_width;
66 Lisp_Object Qtop_toolbar_shadow_color;
67 Lisp_Object Qbottom_toolbar_shadow_color;
68 Lisp_Object Qbackground_toolbar_color;
69 Lisp_Object Qtop_toolbar_shadow_pixmap;
70 Lisp_Object Qbottom_toolbar_shadow_pixmap;
71 Lisp_Object Qtoolbar_shadow_thickness;
72 Lisp_Object Qscrollbar_placement;
73 Lisp_Object Qinter_line_space;
74 Lisp_Object Qvisual_bell;
75 Lisp_Object Qbell_volume;
76 Lisp_Object Qpointer_background;
77 Lisp_Object Qpointer_color;
78 Lisp_Object Qtext_pointer;
79 Lisp_Object Qspace_pointer;
80 Lisp_Object Qmodeline_pointer;
81 Lisp_Object Qgc_pointer;
82 Lisp_Object Qinitially_unmapped;
83 Lisp_Object Quse_backing_store;
84 Lisp_Object Qborder_color;
85 Lisp_Object Qborder_width;
86
87 Lisp_Object Qframep, Qframe_live_p;
88 Lisp_Object Qframe_x_p, Qframe_tty_p;
89 Lisp_Object Qdelete_frame;
90
91 Lisp_Object Qframe_title_format, Vframe_title_format;
92 Lisp_Object Qframe_icon_title_format, Vframe_icon_title_format;
93
94 Lisp_Object Vdefault_frame_name;
95 Lisp_Object Vdefault_frame_plist;
96
97 Lisp_Object Vframe_icon_glyph;
98
99 Lisp_Object Qhidden;
100
101 Lisp_Object Qvisible, Qiconic, Qinvisible, Qvisible_iconic, Qinvisible_iconic;
102 Lisp_Object Qnomini, Qvisible_nomini, Qiconic_nomini, Qinvisible_nomini;
103 Lisp_Object Qvisible_iconic_nomini, Qinvisible_iconic_nomini;
104
105 Lisp_Object Qset_specifier, Qset_glyph_image, Qset_face_property;
106 Lisp_Object Qface_property_instance;
107
108 Lisp_Object Qframe_property_alias;
109
110 /* If this is non-nil, it is the frame that make-frame is currently
111    creating.  We can't set the current frame to this in case the
112    debugger goes off because it would try and display to it.  However,
113    there are some places which need to reference it which have no
114    other way of getting it if it isn't the selected frame. */
115 Lisp_Object Vframe_being_created;
116 Lisp_Object Qframe_being_created;
117
118 static void store_minibuf_frame_prop (struct frame *f, Lisp_Object val);
119
120 EXFUN (Fset_frame_properties, 2);
121
122 \f
123 static Lisp_Object
124 mark_frame (Lisp_Object obj, void (*markobj) (Lisp_Object))
125 {
126   struct frame *f = XFRAME (obj);
127
128 #define MARKED_SLOT(x) ((void) (markobj (f->x)));
129 #include "frameslots.h"
130
131   if (FRAME_LIVE_P (f)) /* device is nil for a dead frame */
132     MAYBE_FRAMEMETH (f, mark_frame, (f, markobj));
133
134   return Qnil;
135 }
136
137 static void
138 print_frame (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
139 {
140   struct frame *frm = XFRAME (obj);
141   char buf[200];
142
143   if (print_readably)
144     error ("printing unreadable object #<frame %s 0x%x>",
145            XSTRING_DATA (frm->name), frm->header.uid);
146
147   sprintf (buf, "#<%s-frame ", !FRAME_LIVE_P (frm) ? "dead" :
148            FRAME_TYPE_NAME (frm));
149   write_c_string (buf, printcharfun);
150   print_internal (frm->name, printcharfun, 1);
151   sprintf (buf, " 0x%x>", frm->header.uid);
152   write_c_string (buf, printcharfun);
153 }
154
155 DEFINE_LRECORD_IMPLEMENTATION ("frame", frame,
156                                mark_frame, print_frame, 0, 0, 0,
157                                struct frame);
158 \f
159 static void
160 nuke_all_frame_slots (struct frame *f)
161 {
162 #define MARKED_SLOT(x)  f->x = Qnil;
163 #include "frameslots.h"
164 }
165
166 /* Allocate a new frame object and set all its fields to reasonable
167    values.  The root window is created but the minibuffer will be done
168    later. */
169
170 static struct frame *
171 allocate_frame_core (Lisp_Object device)
172 {
173   /* This function can GC */
174   Lisp_Object frame;
175   Lisp_Object root_window;
176   struct frame *f = alloc_lcrecord_type (struct frame, lrecord_frame);
177
178   zero_lcrecord (f);
179   nuke_all_frame_slots (f);
180   XSETFRAME (frame, f);
181
182   f->device = device;
183   f->framemeths = XDEVICE (device)->devmeths;
184   f->buffer_alist = Fcopy_sequence (Vbuffer_alist);
185
186   root_window = allocate_window ();
187   XWINDOW (root_window)->frame = frame;
188
189   /* 10 is arbitrary,
190      Just so that there is "something there."
191      Correct size will be set up later with change_frame_size.  */
192
193   f->width  = 10;
194   f->height = 10;
195
196   XWINDOW (root_window)->pixel_width = 10;
197   XWINDOW (root_window)->pixel_height = 9;
198
199   /* The size of the minibuffer window is now set in x_create_frame
200      in xfns.c. */
201
202   f->root_window = root_window;
203   f->selected_window = root_window;
204   f->last_nonminibuf_window = root_window;
205
206   /* Choose a buffer for the frame's root window.  */
207   XWINDOW (root_window)->buffer = Qt;
208   {
209     Lisp_Object buf;
210
211     buf = Fcurrent_buffer ();
212     /* If buf is a 'hidden' buffer (i.e. one whose name starts with
213        a space), try to find another one.  */
214     if (string_char (XSTRING (Fbuffer_name (buf)), 0) == ' ')
215       buf = Fother_buffer (buf, Qnil, Qnil);
216     Fset_window_buffer (root_window, buf);
217   }
218
219   return f;
220 }
221
222 static void
223 setup_normal_frame (struct frame *f)
224 {
225   Lisp_Object mini_window;
226   Lisp_Object frame;
227
228   XSETFRAME (frame, f);
229
230   mini_window = allocate_window ();
231   XWINDOW (f->root_window)->next = mini_window;
232   XWINDOW (mini_window)->prev = f->root_window;
233   XWINDOW (mini_window)->mini_p = Qt;
234   XWINDOW (mini_window)->frame = frame;
235   f->minibuffer_window = mini_window;
236   f->has_minibuffer = 1;
237
238   XWINDOW (mini_window)->buffer = Qt;
239   Fset_window_buffer (mini_window, Vminibuffer_zero);
240 }
241
242 /* Make a frame using a separate minibuffer window on another frame.
243    MINI_WINDOW is the minibuffer window to use.  nil means use the
244    default-minibuffer-frame.  */
245
246 static void
247 setup_frame_without_minibuffer (struct frame *f, Lisp_Object mini_window)
248 {
249   /* This function can GC */
250   Lisp_Object device = f->device;
251
252   if (!NILP (mini_window))
253     CHECK_LIVE_WINDOW (mini_window);
254
255   if (!NILP (mini_window)
256       && !EQ (DEVICE_CONSOLE (XDEVICE (device)),
257               FRAME_CONSOLE (XFRAME (XWINDOW (mini_window)->frame))))
258     error ("frame and minibuffer must be on the same console");
259
260   if (NILP (mini_window))
261     {
262       struct console *con = XCONSOLE (FRAME_CONSOLE (f));
263       /* Use default-minibuffer-frame if possible.  */
264       if (!FRAMEP (con->default_minibuffer_frame)
265           || ! FRAME_LIVE_P (XFRAME (con->default_minibuffer_frame)))
266         {
267           /* If there's no minibuffer frame to use, create one.  */
268           con->default_minibuffer_frame
269             = call1 (Qmake_initial_minibuffer_frame, device);
270         }
271       mini_window = XFRAME (con->default_minibuffer_frame)->minibuffer_window;
272     }
273
274   /* Install the chosen minibuffer window, with proper buffer.  */
275   store_minibuf_frame_prop (f, mini_window);
276   Fset_window_buffer (mini_window, Vminibuffer_zero);
277 }
278
279 /* Make a frame containing only a minibuffer window.  */
280
281 static void
282 setup_minibuffer_frame (struct frame *f)
283 {
284   /* This function can GC */
285   /* First make a frame containing just a root window, no minibuffer.  */
286   Lisp_Object mini_window;
287   Lisp_Object frame;
288
289   XSETFRAME (frame, f);
290
291   f->no_split = 1;
292   f->has_minibuffer = 1;
293
294   /* Now label the root window as also being the minibuffer.
295      Avoid infinite looping on the window chain by marking next pointer
296      as nil. */
297
298   mini_window = f->minibuffer_window = f->root_window;
299   XWINDOW (mini_window)->mini_p = Qt;
300   XWINDOW (mini_window)->next   = Qnil;
301   XWINDOW (mini_window)->prev   = Qnil;
302   XWINDOW (mini_window)->frame  = frame;
303
304   /* Put the proper buffer in that window.  */
305
306   Fset_window_buffer (mini_window, Vminibuffer_zero);
307 }
308
309 static Lisp_Object
310 make_sure_its_a_fresh_plist (Lisp_Object foolist)
311 {
312   if (CONSP (Fcar (foolist)))
313     {
314       /* looks like an alist to me. */
315       foolist = Fcopy_alist (foolist);
316       foolist = Fdestructive_alist_to_plist (foolist);
317     }
318   else
319     foolist = Fcopy_sequence (foolist);
320
321   return foolist;
322 }
323
324 DEFUN ("make-frame", Fmake_frame, 0, 2, "", /*
325 Create and return a new frame, displaying the current buffer.
326 Runs the functions listed in `create-frame-hook' after frame creation.
327
328 Optional argument PROPS is a property list (a list of alternating
329 keyword-value specifications) of properties for the new frame.
330 \(An alist is accepted for backward compatibility but should not
331 be passed in.)
332
333 See `set-frame-properties', `default-x-frame-plist', and
334 `default-tty-frame-plist' for the specially-recognized properties.
335 */
336        (props, device))
337 {
338   struct frame *f;
339   struct device *d;
340   Lisp_Object frame = Qnil, name = Qnil, minibuf;
341   struct gcpro gcpro1, gcpro2, gcpro3;
342   int speccount = specpdl_depth ();
343   int first_frame_on_device = 0;
344   int first_frame_on_console = 0;
345
346   d = decode_device (device);
347   XSETDEVICE (device, d);
348
349   /* PROPS and NAME may be freshly-created, so make sure to GCPRO. */
350   GCPRO3 (frame, props, name);
351
352   props = make_sure_its_a_fresh_plist (props);
353   if (DEVICE_SPECIFIC_FRAME_PROPS (d))
354     /* Put the device-specific props before the more general ones so
355        that they override them. */
356     props = nconc2 (props,
357                     make_sure_its_a_fresh_plist
358                     (*DEVICE_SPECIFIC_FRAME_PROPS (d)));
359   props = nconc2 (props, make_sure_its_a_fresh_plist (Vdefault_frame_plist));
360   Fcanonicalize_lax_plist (props, Qnil);
361
362   name = Flax_plist_get (props, Qname, Qnil);
363   if (!NILP (name))
364     CHECK_STRING (name);
365   else if (STRINGP (Vdefault_frame_name))
366     name = Vdefault_frame_name;
367   else
368     name = build_string ("emacs");
369
370   if (!NILP (Fstring_match (make_string ((CONST Bufbyte *) "\\.", 2), name,
371                             Qnil, Qnil)))
372     signal_simple_error (". not allowed in frame names", name);
373
374   f = allocate_frame_core (device);
375   XSETFRAME (frame, f);
376
377   specbind (Qframe_being_created, name);
378   f->name = name;
379
380   FRAMEMETH (f, init_frame_1, (f, props));
381
382   minibuf = Flax_plist_get (props, Qminibuffer, Qunbound);
383   if (UNBOUNDP (minibuf))
384     {
385       /* If minibuf is unspecified, then look for a minibuffer X resource. */
386       /* #### Not implemented any more.  We need to fix things up so
387          that we search out all X resources and append them to the end of
388          props, above.  This is the only way in general to assure
389          coherent behavior for all frame properties/resources/etc. */
390     }
391   else
392     props = Flax_plist_remprop (props, Qminibuffer);
393
394   if (EQ (minibuf, Qnone) || NILP (minibuf))
395     setup_frame_without_minibuffer (f, Qnil);
396   else if (EQ (minibuf, Qonly))
397     setup_minibuffer_frame (f);
398   else if (WINDOWP (minibuf))
399     setup_frame_without_minibuffer (f, minibuf);
400   else if (EQ (minibuf, Qt) || UNBOUNDP (minibuf))
401     setup_normal_frame (f);
402   else
403     signal_simple_error ("Invalid value for `minibuffer'", minibuf);
404
405   update_frame_window_mirror (f);
406
407   if (initialized)
408     {
409       if (!NILP (f->minibuffer_window))
410         reset_face_cachels (XWINDOW (f->minibuffer_window));
411       reset_face_cachels (XWINDOW (f->root_window));
412     }
413
414   /* If no frames on this device formerly existed, say this is the
415      first frame.  It kind of assumes that frameless devices don't
416      exist, but it shouldn't be too harmful.  */
417   if (NILP (DEVICE_FRAME_LIST (d)))
418     first_frame_on_device = 1;
419
420   /* This *must* go before the init_*() methods.  Those functions
421      call Lisp code, and if any of them causes a warning to be displayed
422      and the *Warnings* buffer to be created, it won't get added to
423      the frame-specific version of the buffer-alist unless the frame
424      is accessible from the device. */
425
426 #if 0
427   DEVICE_FRAME_LIST (d) = nconc2 (DEVICE_FRAME_LIST (d), Fcons (frame, Qnil));
428 #endif
429   DEVICE_FRAME_LIST (d) = Fcons (frame, DEVICE_FRAME_LIST (d));
430   RESET_CHANGED_SET_FLAGS;
431
432   /* Now make sure that the initial cached values are set correctly.
433      Do this after the init_frame method is called because that may
434      do things (e.g. create widgets) that are necessary for the
435      specifier value-changed methods to work OK. */
436   recompute_all_cached_specifiers_in_frame (f);
437
438   if (!DEVICE_STREAM_P (d))
439     {
440       init_frame_faces (f);
441
442 #ifdef HAVE_SCROLLBARS
443       /* Finish up resourcing the scrollbars. */
444       init_frame_scrollbars (f);
445 #endif
446
447 #ifdef HAVE_TOOLBARS
448       /* Create the initial toolbars.  We have to do this after the frame
449          methods are called because it may potentially call some things itself
450          which depend on the normal frame methods having initialized
451          things. */
452       init_frame_toolbars (f);
453 #endif
454
455       reset_face_cachels (XWINDOW (FRAME_SELECTED_WINDOW (f)));
456       reset_glyph_cachels (XWINDOW (FRAME_SELECTED_WINDOW (f)));
457       change_frame_size (f, f->height, f->width, 0);
458     }
459
460   MAYBE_FRAMEMETH (f, init_frame_2, (f, props));
461   Fset_frame_properties (frame, props);
462   MAYBE_FRAMEMETH (f, init_frame_3, (f));
463
464   /* Hallelujah, praise the lord. */
465   f->init_finished = 1;
466
467   /* If this is the first frame on the device, make it the selected one. */
468   if (first_frame_on_device && NILP (DEVICE_SELECTED_FRAME (d)))
469     set_device_selected_frame (d, frame);
470
471   /* If at startup or if the current console is a stream console
472      (usually also at startup), make this console the selected one
473      so that messages show up on it. */
474   if (NILP (Fselected_console ()) ||
475       CONSOLE_STREAM_P (XCONSOLE (Fselected_console ())))
476     Fselect_console (DEVICE_CONSOLE (d));
477
478   first_frame_on_console =
479     (first_frame_on_device &&
480      XINT (Flength (CONSOLE_DEVICE_LIST (XCONSOLE (DEVICE_CONSOLE (d)))))
481      == 1);
482
483   /* #### all this calling of frame methods at various odd times
484      is somewhat of a mess.  It's necessary to do it this way due
485      to strange console-type-specific things that need to be done. */
486   MAYBE_FRAMEMETH (f, after_init_frame, (f, first_frame_on_device,
487                                          first_frame_on_console));
488
489   if (first_frame_on_device)
490     {
491       if (first_frame_on_console)
492         va_run_hook_with_args (Qcreate_console_hook, 1, DEVICE_CONSOLE (d));
493       va_run_hook_with_args (Qcreate_device_hook, 1, device);
494     }
495   va_run_hook_with_args (Qcreate_frame_hook, 1, frame);
496
497   /* Initialize custom-specific stuff. */
498   if (!UNBOUNDP (symbol_function (XSYMBOL (Qcustom_initialize_frame))))
499     call1 (Qcustom_initialize_frame, frame);
500
501   unbind_to (speccount, Qnil);
502
503   UNGCPRO;
504   return frame;
505 }
506
507 \f
508 /* this function should be used in most cases when a Lisp function is passed
509    a FRAME argument.  Use this unless you don't accept nil == current frame
510    (in which case, do a CHECK_LIVE_FRAME() and then an XFRAME()) or you
511    allow dead frames.  Note that very few functions should accept dead
512    frames.  It could be argued that functions should just do nothing when
513    given a dead frame, but the presence of a dead frame usually indicates
514    an oversight in the Lisp code that could potentially lead to strange
515    results and so it is better to catch the error early.
516
517    If you only accept X frames, use decode_x_frame(), which does what this
518    function does but also makes sure the frame is an X frame. */
519
520 struct frame *
521 decode_frame (Lisp_Object frame)
522 {
523   if (NILP (frame))
524     return selected_frame ();
525
526   CHECK_LIVE_FRAME (frame);
527   return XFRAME (frame);
528 }
529
530 struct frame *
531 decode_frame_or_selected (Lisp_Object cdf)
532 {
533   if (CONSOLEP (cdf))
534     cdf = CONSOLE_SELECTED_DEVICE (decode_console (cdf));
535   if (DEVICEP (cdf))
536     cdf = DEVICE_SELECTED_FRAME (decode_device (cdf));
537   return decode_frame (cdf);
538 }
539
540 Lisp_Object
541 make_frame (struct frame *f)
542 {
543   Lisp_Object frame;
544   XSETFRAME (frame, f);
545   return frame;
546 }
547
548 \f
549 /*
550  * window size changes are held up during critical regions.  Afterwards,
551  * we want to deal with any delayed changes.
552  */
553 void
554 hold_frame_size_changes (void)
555 {
556   in_display = 1;
557 }
558
559 void
560 unhold_one_frame_size_changes (struct frame *f)
561 {
562   in_display = 0;
563
564   if (f->size_change_pending)
565     change_frame_size (f, f->new_height, f->new_width, 0);
566 }
567
568 void
569 unhold_frame_size_changes (void)
570 {
571   Lisp_Object frmcons, devcons, concons;
572
573   FRAME_LOOP_NO_BREAK (frmcons, devcons, concons)
574     unhold_one_frame_size_changes (XFRAME (XCAR (frmcons)));
575 }
576
577 void
578 invalidate_vertical_divider_cache_in_frame (struct frame *f)
579 {
580   /* Invalidate cached value of needs_vertical_divider_p in
581      every and all windows */
582   map_windows (f, invalidate_vertical_divider_cache_in_window, 0);
583 }
584 \f
585 /*
586  * Frame size may change due to changes in scrollbars, toolbars,
587  * default font etc. These changes are applied early in redisplay
588  * frame.
589  */
590 void
591 adjust_frame_size (struct frame *f)
592 {
593   int keep_char_size = 0;
594   Lisp_Object frame;
595   XSETFRAME (frame, f);
596
597   if (!f->size_slipped)
598     return;
599
600   /* Don't adjust tty frames. #### May break when TTY have menubars.
601      Then, write an Vadjust_frame_function which will return t for TTY
602      frames. Another solution is frame_size_fixed_p method for TTYs,
603      which always returned yes it's fixed.
604   */
605   if (!FRAME_WIN_P (f))
606     {
607       CLEAR_FRAME_SIZE_SLIPPED (f);
608       return;
609     }
610
611   /* frame_size_fixed_p tells that frame size cannot currently
612      be changed change due to external conditions */
613   if (!FRAMEMETH_OR_GIVEN (f, frame_size_fixed_p, (f), 0))
614     {
615       if (NILP (Vadjust_frame_function))
616         keep_char_size = 1;
617       else if (EQ (Vadjust_frame_function, Qt))
618         keep_char_size = 0;
619       else
620         keep_char_size =
621           NILP (call1_trapping_errors ("Error in adjust-frame-function",
622                                        Vadjust_frame_function, frame));
623
624       if (keep_char_size)
625         Fset_frame_size (frame, make_int (FRAME_CHARWIDTH(f)),
626                          make_int (FRAME_CHARHEIGHT(f)), Qnil);
627     }
628
629   if (!keep_char_size)
630     {
631       int height, width;
632       pixel_to_char_size (f, FRAME_PIXWIDTH(f), FRAME_PIXHEIGHT(f),
633                           &width, &height);
634       change_frame_size (f, height, width, 0);
635       CLEAR_FRAME_SIZE_SLIPPED (f);
636     }
637 }
638
639 /*
640  * This is a "specifier changed in frame" handler for various specifiers
641  * changing which causes frame size adjustment
642  */
643 void
644 frame_size_slipped (Lisp_Object specifier, struct frame *f,
645                     Lisp_Object oldval)
646 {
647   MARK_FRAME_SIZE_SLIPPED(f);
648 }
649 \f
650 DEFUN ("framep", Fframep, 1, 1, 0, /*
651 Return non-nil if OBJECT is a frame.
652 Also see `frame-live-p'.
653 Note that FSF Emacs kludgily returns a value indicating what type of
654 frame this is.  Use the cleaner function `frame-type' for that.
655 */
656        (object))
657 {
658   return FRAMEP (object) ? Qt : Qnil;
659 }
660
661 DEFUN ("frame-live-p", Fframe_live_p, 1, 1, 0, /*
662 Return non-nil if OBJECT is a frame which has not been deleted.
663 */
664        (object))
665 {
666   return FRAMEP (object) && FRAME_LIVE_P (XFRAME (object)) ? Qt : Qnil;
667 }
668
669 \f
670 DEFUN ("focus-frame", Ffocus_frame, 1, 1, 0, /*
671 Select FRAME and give it the window system focus.
672 This function is not affected by the value of `focus-follows-mouse'.
673 */
674        (frame))
675 {
676   CHECK_LIVE_FRAME (frame);
677
678   MAYBE_DEVMETH (XDEVICE (FRAME_DEVICE (XFRAME (frame))), focus_on_frame,
679                  (XFRAME (frame)));
680   /* FRAME will be selected by the time we receive the next event.
681      However, it is better to select it explicitly now, in case the
682      Lisp code depends on frame being selected.  */
683   Fselect_frame (frame);
684   return Qnil;
685 }
686
687 /* Called from Fselect_window() */
688 void
689 select_frame_1 (Lisp_Object frame)
690 {
691   struct frame *f = XFRAME (frame);
692   Lisp_Object old_selected_frame = Fselected_frame (Qnil);
693
694   if (EQ (frame, old_selected_frame))
695     return;
696
697   /* now select the frame's device */
698   set_device_selected_frame (XDEVICE (FRAME_DEVICE (f)), frame);
699   select_device_1 (FRAME_DEVICE (f));
700
701   update_frame_window_mirror (f);
702 }
703
704 DEFUN ("select-frame", Fselect_frame, 1, 1, 0, /*
705 Select the frame FRAME.
706 Subsequent editing commands apply to its selected window.
707 The selection of FRAME lasts until the next time the user does
708 something to select a different frame, or until the next time this
709 function is called.
710
711 Note that this does not actually cause the window-system focus to be
712 set to this frame, or the `select-frame-hook' or `deselect-frame-hook'
713 to be run, until the next time that XEmacs is waiting for an event.
714
715 Also note that when focus-follows-mouse is non-nil, the frame
716 selection is temporary and is reverted when the current command
717 terminates, much like the buffer selected by `set-buffer'.  In order
718 to effect a permanent focus change, use `focus-frame'.
719 */
720        (frame))
721 {
722   CHECK_LIVE_FRAME (frame);
723
724   /* select the frame's selected window.  This will call
725      selected_frame_1(). */
726   Fselect_window (FRAME_SELECTED_WINDOW (XFRAME (frame)), Qnil);
727
728   /* Nothing should be depending on the return value of this function.
729      But, of course, there is stuff out there which is. */
730   return frame;
731 }
732
733 /* use this to retrieve the currently selected frame.  You should use
734    this in preference to Fselected_frame (Qnil) unless you are prepared
735    to handle the possibility of there being no selected frame (this
736    happens at some points during startup). */
737
738 struct frame *
739 selected_frame (void)
740 {
741   Lisp_Object device = Fselected_device (Qnil);
742   Lisp_Object frame = DEVICE_SELECTED_FRAME (XDEVICE (device));
743   if (NILP (frame))
744     signal_simple_error ("No frames exist on device", device);
745   return XFRAME (frame);
746 }
747
748 /* use this instead of XFRAME (DEVICE_SELECTED_FRAME (d)) to catch
749    the possibility of there being no frames on the device (just created).
750    There is no point doing this inside of redisplay because errors
751    cause an abort(), indicating a flaw in the logic, and error_check_frame()
752    will catch this just as well. */
753
754 struct frame *
755 device_selected_frame (struct device *d)
756 {
757   Lisp_Object frame = DEVICE_SELECTED_FRAME (d);
758   if (NILP (frame))
759     {
760       Lisp_Object device;
761       XSETDEVICE (device, d);
762       signal_simple_error ("No frames exist on device", device);
763     }
764   return XFRAME (frame);
765 }
766
767 #if 0 /* FSFmacs */
768
769 xxDEFUN ("handle-switch-frame", Fhandle_switch_frame, 1, 2, "e", /*
770 Handle a switch-frame event EVENT.
771 Switch-frame events are usually bound to this function.
772 A switch-frame event tells Emacs that the window manager has requested
773 that the user's events be directed to the frame mentioned in the event.
774 This function selects the selected window of the frame of EVENT.
775
776 If EVENT is frame object, handle it as if it were a switch-frame event
777 to that frame.
778 */
779          (frame, no_enter))
780 {
781   /* Preserve prefix arg that the command loop just cleared.  */
782   XCONSOLE (Vselected_console)->prefix_arg = Vcurrent_prefix_arg;
783 #if 0 /* unclean! */
784   run_hook (Qmouse_leave_buffer_hook);
785 #endif
786   return do_switch_frame (frame, no_enter, 0);
787 }
788
789 /* A load of garbage. */
790 xxDEFUN ("ignore-event", Fignore_event, 0, 0, "", /*
791 Do nothing, but preserve any prefix argument already specified.
792 This is a suitable binding for iconify-frame and make-frame-visible.
793 */
794          ())
795 {
796   struct console *c = XCONSOLE (Vselected_console);
797
798   c->prefix_arg = Vcurrent_prefix_arg;
799   return Qnil;
800 }
801
802 #endif /* 0 */
803
804 DEFUN ("selected-frame", Fselected_frame, 0, 1, 0, /*
805 Return the frame that is now selected on device DEVICE.
806 If DEVICE is not specified, the selected device will be used.
807 If no frames exist on the device, nil is returned.
808 */
809        (device))
810 {
811   if (NILP (device) && NILP (Fselected_device (Qnil)))
812     return Qnil; /* happens early in temacs */
813   return DEVICE_SELECTED_FRAME (decode_device (device));
814 }
815
816 Lisp_Object
817 frame_first_window (struct frame *f)
818 {
819   Lisp_Object w = f->root_window;
820
821   while (1)
822     {
823       if (! NILP (XWINDOW (w)->hchild))
824         w = XWINDOW (w)->hchild;
825       else if (! NILP (XWINDOW (w)->vchild))
826         w = XWINDOW (w)->vchild;
827       else
828         break;
829     }
830
831   return w;
832 }
833
834 DEFUN ("active-minibuffer-window", Factive_minibuffer_window, 0, 0, 0, /*
835 Return the currently active minibuffer window, or nil if none.
836 */
837        ())
838 {
839   return minibuf_level ? minibuf_window : Qnil;
840 }
841
842 DEFUN ("last-nonminibuf-frame", Flast_nonminibuf_frame, 0, 1, 0, /*
843 Return the most-recently-selected non-minibuffer-only frame on CONSOLE.
844 This will always be the same as (selected-frame device) unless the
845 selected frame is a minibuffer-only frame.
846 CONSOLE defaults to the selected console if omitted.
847 */
848        (console))
849 {
850   Lisp_Object result;
851
852   XSETCONSOLE (console, decode_console (console));
853   /* Just in case the machinations in delete_frame_internal() resulted
854      in the last-nonminibuf-frame getting out of sync, make sure and
855      return the selected frame if it's acceptable. */
856   result = Fselected_frame (CONSOLE_SELECTED_DEVICE (XCONSOLE (console)));
857   if (!NILP (result) && !FRAME_MINIBUF_ONLY_P (XFRAME (result)))
858     return result;
859   return CONSOLE_LAST_NONMINIBUF_FRAME (XCONSOLE (console));
860 }
861
862 DEFUN ("frame-root-window", Fframe_root_window, 0, 1, 0, /*
863 Return the root-window of FRAME.
864 If omitted, FRAME defaults to the currently selected frame.
865 */
866        (frame))
867 {
868   struct frame *f = decode_frame (frame);
869   return FRAME_ROOT_WINDOW (f);
870 }
871
872 DEFUN ("frame-selected-window", Fframe_selected_window, 0, 1, 0, /*
873 Return the selected window of frame object FRAME.
874 If omitted, FRAME defaults to the currently selected frame.
875 */
876        (frame))
877 {
878   struct frame *f = decode_frame (frame);
879   return FRAME_SELECTED_WINDOW (f);
880 }
881
882 void
883 set_frame_selected_window (struct frame *f, Lisp_Object window)
884 {
885   assert (XFRAME (WINDOW_FRAME (XWINDOW (window))) == f);
886   f->selected_window = window;
887   if (!MINI_WINDOW_P (XWINDOW (window)) || FRAME_MINIBUF_ONLY_P (f))
888     {
889 #ifdef HAVE_TOOLBARS
890       if (!EQ (f->last_nonminibuf_window, window))
891         MARK_TOOLBAR_CHANGED;
892 #endif
893       f->last_nonminibuf_window = window;
894     }
895 }
896
897 DEFUN ("set-frame-selected-window", Fset_frame_selected_window, 2, 2, 0, /*
898 Set the selected window of frame object FRAME to WINDOW.
899 If FRAME is nil, the selected frame is used.
900 If FRAME is the selected frame, this makes WINDOW the selected window.
901 */
902        (frame, window))
903 {
904   XSETFRAME (frame, decode_frame (frame));
905   CHECK_LIVE_WINDOW (window);
906
907   if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
908     error ("In `set-frame-selected-window', WINDOW is not on FRAME");
909
910   if (XFRAME (frame) == selected_frame ())
911     return Fselect_window (window, Qnil);
912
913   set_frame_selected_window (XFRAME (frame), window);
914   return window;
915 }
916
917 \f
918 DEFUN ("frame-device", Fframe_device, 0, 1, 0, /*
919 Return the device that FRAME is on.
920 If omitted, FRAME defaults to the currently selected frame.
921 */
922        (frame))
923 {
924   return FRAME_DEVICE (decode_frame (frame));
925 }
926
927 int
928 is_surrogate_for_selected_frame (struct frame *f)
929 {
930   struct device *d = XDEVICE (f->device);
931   struct frame *dsf = device_selected_frame (d);
932
933   /* Can't be a surrogate for ourselves. */
934   if (f == dsf)
935     return 0;
936
937   if (!FRAME_HAS_MINIBUF_P (dsf) &&
938       f == XFRAME (WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (dsf)))))
939     return 1;
940   else
941     return 0;
942 }
943
944 static int
945 frame_matches_frametype (Lisp_Object frame, Lisp_Object type)
946 {
947   struct frame *f = XFRAME (frame);
948
949   if (WINDOWP (type))
950     {
951       CHECK_LIVE_WINDOW (type);
952
953       if (EQ (FRAME_MINIBUF_WINDOW (f), type)
954           /* Check that F either is, or has forwarded
955              its focus to, TYPE's frame.  */
956           && (EQ (WINDOW_FRAME (XWINDOW (type)), frame)
957               || EQ (WINDOW_FRAME (XWINDOW (type)),
958                      FRAME_FOCUS_FRAME (f))))
959         return 1;
960       else
961         return 0;
962     }
963
964 #if 0 /* FSFmacs */
965   if (EQ (type, Qvisible) || EQ (type, Qiconic) || EQ (type, Qvisible_iconic)
966       || EQ (type, Qvisible_nomini) || EQ (type, Qiconic_nomini)
967       || EQ (type, Qvisible_iconic_nomini))
968     FRAME_SAMPLE_VISIBILITY (f);
969 #endif
970
971   if (NILP (type))
972     type = Qnomini;
973   if (ZEROP (type))
974     type = Qvisible_iconic;
975
976   if (EQ (type, Qvisible))
977     return FRAME_VISIBLE_P (f);
978   if (EQ (type, Qiconic))
979     return FRAME_ICONIFIED_P (f);
980   if (EQ (type, Qinvisible))
981     return !FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f);
982   if (EQ (type, Qvisible_iconic))
983     return FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f);
984   if (EQ (type, Qinvisible_iconic))
985     return !FRAME_VISIBLE_P (f);
986
987   if (EQ (type, Qnomini))
988     return !FRAME_MINIBUF_ONLY_P (f);
989   if (EQ (type, Qvisible_nomini))
990     return FRAME_VISIBLE_P (f) && !FRAME_MINIBUF_ONLY_P (f);
991   if (EQ (type, Qiconic_nomini))
992     return FRAME_ICONIFIED_P (f) && !FRAME_MINIBUF_ONLY_P (f);
993   if (EQ (type, Qinvisible_nomini))
994     return !FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f) &&
995       !FRAME_MINIBUF_ONLY_P (f);
996   if (EQ (type, Qvisible_iconic_nomini))
997     return ((FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
998             && !FRAME_MINIBUF_ONLY_P (f));
999   if (EQ (type, Qinvisible_iconic_nomini))
1000     return !FRAME_VISIBLE_P (f) && !FRAME_MINIBUF_ONLY_P (f);
1001
1002   return 1;
1003 }
1004
1005 int
1006 device_matches_console_spec (Lisp_Object frame, Lisp_Object device,
1007                              Lisp_Object console)
1008 {
1009   if (EQ (console, Qwindow_system))
1010     return DEVICE_WIN_P (XDEVICE (device));
1011   if (NILP (console))
1012     console = (DEVICE_CONSOLE (XDEVICE (FRAME_DEVICE (XFRAME (frame)))));
1013   if (DEVICEP (console))
1014     return EQ (device, console);
1015   if (CONSOLEP (console))
1016     return EQ (DEVICE_CONSOLE (XDEVICE (device)), console);
1017   if (valid_console_type_p (console))
1018     return EQ (DEVICE_TYPE (XDEVICE (device)), console);
1019   return 1;
1020 }
1021
1022 /* Return the next frame in the frame list after FRAME.
1023    FRAMETYPE and CONSOLE control which frames and devices
1024    are considered; see `next-frame'. */
1025
1026 static Lisp_Object
1027 next_frame_internal (Lisp_Object frame, Lisp_Object frametype,
1028                      Lisp_Object console, int called_from_delete_device)
1029 {
1030   int passed = 0;
1031   int started_over = 0;
1032
1033   /* If this frame is dead, it won't be in frame_list, and we'll loop
1034      forever.  Forestall that.  */
1035   CHECK_LIVE_FRAME (frame);
1036
1037   while (1)
1038     {
1039       Lisp_Object devcons, concons;
1040
1041       DEVICE_LOOP_NO_BREAK (devcons, concons)
1042         {
1043           Lisp_Object device = XCAR (devcons);
1044           Lisp_Object frmcons;
1045
1046           if (!device_matches_console_spec (frame, device, console))
1047             continue;
1048
1049           DEVICE_FRAME_LOOP (frmcons, XDEVICE (device))
1050             {
1051               Lisp_Object f = XCAR (frmcons);
1052               if (passed)
1053                 {
1054                   /* #### Doing this here is bad and is now
1055                      unnecessary.  The real bug was that f->iconified
1056                      was never, ever updated unless a user explicitly
1057                      called frame-iconified-p.  That has now been
1058                      fixed.  With this change removed all of the other
1059                      changes made to support this routine having the
1060                      called_from_delete_device arg could be removed.
1061                      But it is too close to release to do that now. */
1062 #if 0
1063                   /* Make sure the visibility and iconified flags are
1064                      up-to-date unless we're being deleted. */
1065                   if (!called_from_delete_device)
1066                     {
1067                       Fframe_iconified_p (f);
1068                       Fframe_visible_p (f);
1069                     }
1070 #endif
1071
1072                   /* Decide whether this frame is eligible to be returned.  */
1073
1074                   /* If we've looped all the way around without finding any
1075                      eligible frames, return the original frame.  */
1076                   if (EQ (f, frame))
1077                     return f;
1078
1079                   if (frame_matches_frametype (f, frametype))
1080                     return f;
1081                 }
1082
1083               if (EQ (frame, f))
1084                 passed++;
1085             }
1086         }
1087       /* We hit the end of the list, and need to start over again. */
1088       if (started_over)
1089         return Qnil;
1090       started_over++;
1091     }
1092 }
1093
1094 Lisp_Object
1095 next_frame (Lisp_Object frame, Lisp_Object frametype, Lisp_Object console)
1096 {
1097   return next_frame_internal (frame, frametype, console, 0);
1098 }
1099
1100 /* Return the previous frame in the frame list before FRAME.
1101    FRAMETYPE and CONSOLE control which frames and devices
1102    are considered; see `next-frame'. */
1103
1104 Lisp_Object
1105 prev_frame (Lisp_Object frame, Lisp_Object frametype, Lisp_Object console)
1106 {
1107   Lisp_Object devcons, concons;
1108   Lisp_Object prev;
1109
1110   /* If this frame is dead, it won't be in frame_list, and we'll loop
1111      forever.  Forestall that.  */
1112   CHECK_LIVE_FRAME (frame);
1113
1114   prev = Qnil;
1115   DEVICE_LOOP_NO_BREAK (devcons, concons)
1116     {
1117       Lisp_Object device = XCAR (devcons);
1118       Lisp_Object frmcons;
1119
1120       if (!device_matches_console_spec (frame, device, console))
1121         continue;
1122
1123       DEVICE_FRAME_LOOP (frmcons, XDEVICE (device))
1124         {
1125           Lisp_Object f = XCAR (frmcons);
1126
1127           if (EQ (frame, f) && !NILP (prev))
1128             return prev;
1129
1130           /* Decide whether this frame is eligible to be returned,
1131              according to frametype.  */
1132
1133           if (frame_matches_frametype (f, frametype))
1134             prev = f;
1135
1136         }
1137     }
1138
1139   /* We've scanned the entire list.  */
1140   if (NILP (prev))
1141     /* We went through the whole frame list without finding a single
1142        acceptable frame.  Return the original frame.  */
1143     return frame;
1144   else
1145     /* There were no acceptable frames in the list before FRAME; otherwise,
1146        we would have returned directly from the loop.  Since PREV is the last
1147        acceptable frame in the list, return it.  */
1148     return prev;
1149 }
1150
1151 DEFUN ("next-frame", Fnext_frame, 0, 3, 0, /*
1152 Return the next frame of the right type in the frame list after FRAME.
1153 FRAMETYPE controls which frames are eligible to be returned; all
1154 others will be skipped.  Note that if there is only one eligible
1155 frame, then `next-frame' called repeatedly will always return
1156 the same frame, and if there is no eligible frame, then FRAME is
1157 returned.
1158
1159 Possible values for FRAMETYPE are
1160
1161 'visible                 Consider only frames that are visible.
1162 'iconic                  Consider only frames that are iconic.
1163 'invisible               Consider only frames that are invisible
1164                          (this is different from iconic).
1165 'visible-iconic          Consider frames that are visible or iconic.
1166 'invisible-iconic        Consider frames that are invisible or iconic.
1167 'nomini                  Consider all frames except minibuffer-only ones.
1168 'visible-nomini          Like `visible' but omits minibuffer-only frames.
1169 'iconic-nomini           Like `iconic' but omits minibuffer-only frames.
1170 'invisible-nomini        Like `invisible' but omits minibuffer-only frames.
1171 'visible-iconic-nomini   Like `visible-iconic' but omits minibuffer-only
1172                          frames.
1173 'invisible-iconic-nomini Like `invisible-iconic' but omits minibuffer-only
1174                          frames.
1175 any other value          Consider all frames.
1176
1177 If FRAMETYPE is omitted, 'nomini is used.  A FRAMETYPE of 0 (a number)
1178 is treated like 'iconic, for backwards compatibility.
1179
1180 If FRAMETYPE is a window, include only its own frame and any frame now
1181 using that window as the minibuffer.
1182
1183 Optional third argument CONSOLE controls which consoles or devices the
1184 returned frame may be on.  If CONSOLE is a console, return frames only
1185 on that console.  If CONSOLE is a device, return frames only on that
1186 device.  If CONSOLE is a console type, return frames only on consoles
1187 of that type.  If CONSOLE is 'window-system, return any frames on any
1188 window-system consoles.  If CONSOLE is nil or omitted, return frames only
1189 on the FRAME's console.  Otherwise, all frames are considered.
1190 */
1191        (frame, frametype, console))
1192 {
1193   XSETFRAME (frame, decode_frame (frame));
1194
1195   return next_frame (frame, frametype, console);
1196 }
1197
1198 DEFUN ("previous-frame", Fprevious_frame, 0, 3, 0, /*
1199 Return the next frame of the right type in the frame list after FRAME.
1200 FRAMETYPE controls which frames are eligible to be returned; all
1201 others will be skipped.  Note that if there is only one eligible
1202 frame, then `previous-frame' called repeatedly will always return
1203 the same frame, and if there is no eligible frame, then FRAME is
1204 returned.
1205
1206 See `next-frame' for an explanation of the FRAMETYPE and CONSOLE
1207 arguments.
1208 */
1209        (frame, frametype, console))
1210 {
1211   XSETFRAME (frame, decode_frame (frame));
1212
1213   return prev_frame (frame, frametype, console);
1214 }
1215
1216 /* Return any frame for which PREDICATE is non-zero, or return Qnil
1217    if there aren't any. */
1218
1219 Lisp_Object
1220 find_some_frame (int (*predicate) (Lisp_Object, void *),
1221                  void *closure)
1222 {
1223   Lisp_Object framecons, devcons, concons;
1224
1225   FRAME_LOOP_NO_BREAK (framecons, devcons, concons)
1226     {
1227       Lisp_Object frame = XCAR (framecons);
1228
1229       if ((predicate) (frame, closure))
1230         return frame;
1231     }
1232
1233   return Qnil;
1234 }
1235
1236 \f
1237
1238 /* extern void free_line_insertion_deletion_costs (struct frame *f); */
1239
1240 /* Return 1 if it is ok to delete frame F;
1241    0 if all frames aside from F are invisible.
1242    (Exception: if F is a stream frame, it's OK to delete if
1243    any other frames exist.) */
1244
1245 static int
1246 other_visible_frames_internal (struct frame *f, int called_from_delete_device)
1247 {
1248   Lisp_Object frame;
1249
1250   XSETFRAME (frame, f);
1251   if (FRAME_STREAM_P (f))
1252     return !EQ (frame, next_frame_internal (frame, Qt, Qt,
1253                                             called_from_delete_device));
1254   return !EQ (frame, next_frame_internal (frame, Qvisible_iconic_nomini, Qt,
1255                                           called_from_delete_device));
1256 }
1257
1258 int
1259 other_visible_frames (struct frame *f)
1260 {
1261   return other_visible_frames_internal (f, 0);
1262 }
1263
1264 /* Delete frame F.
1265
1266    If FORCE is non-zero, allow deletion of the only frame.
1267
1268    If CALLED_FROM_DELETE_DEVICE is non-zero, then, if
1269    deleting the last frame on a device, just delete it,
1270    instead of calling `delete-device'.
1271
1272    If FROM_IO_ERROR is non-zero, then the frame is gone due
1273    to an I/O error.  This affects what happens if we exit
1274    (we do an emergency exit instead of `save-buffers-kill-emacs'.)
1275 */
1276
1277 void
1278 delete_frame_internal (struct frame *f, int force,
1279                        int called_from_delete_device,
1280                        int from_io_error)
1281 {
1282   /* This function can GC */
1283   int minibuffer_selected;
1284   struct device *d;
1285   struct console *con;
1286   Lisp_Object frame;
1287   Lisp_Object device;
1288   Lisp_Object console;
1289   struct gcpro gcpro1;
1290
1291   /* OK to delete an already deleted frame. */
1292   if (! FRAME_LIVE_P (f))
1293     return;
1294
1295   XSETFRAME (frame, f);
1296   GCPRO1 (frame);
1297
1298   device = FRAME_DEVICE (f);
1299   d = XDEVICE (device);
1300   console = DEVICE_CONSOLE (d);
1301   con = XCONSOLE (console);
1302
1303   if (!called_from_delete_device)
1304     {
1305       /* If we're deleting the only non-minibuffer frame on the
1306          device, delete the device. */
1307       if (EQ (frame, next_frame (frame, Qnomini, FRAME_DEVICE (f))))
1308         {
1309           delete_device_internal (d, force, 0, from_io_error);
1310           UNGCPRO;
1311           return;
1312         }
1313     }
1314
1315   /* In FSF, delete-frame will not normally allow you to delete the
1316      last visible frame.  This was too annoying, so we changed it to the
1317      only frame.  However, this would let people shoot themselves by
1318      deleting all frames which were either visible or iconified and thus
1319      losing any way of communicating with the still running XEmacs process.
1320      So we put it back.  */
1321   if (!force && !allow_deletion_of_last_visible_frame &&
1322       !other_visible_frames_internal (f, called_from_delete_device))
1323     error ("Attempt to delete the sole visible or iconified frame");
1324
1325   /* Does this frame have a minibuffer, and is it the surrogate
1326      minibuffer for any other frame?  */
1327   if (FRAME_HAS_MINIBUF_P (f))
1328     {
1329       Lisp_Object frmcons, devcons, concons;
1330
1331       FRAME_LOOP_NO_BREAK (frmcons, devcons, concons)
1332         {
1333           Lisp_Object this = XCAR (frmcons);
1334
1335           if (! EQ (this, frame)
1336               && EQ (frame, (WINDOW_FRAME
1337                              (XWINDOW
1338                               (FRAME_MINIBUF_WINDOW (XFRAME (this)))))))
1339             {
1340               /* We've found another frame whose minibuffer is on
1341                  this frame. */
1342               signal_simple_error
1343                 ("Attempt to delete a surrogate minibuffer frame", frame);
1344             }
1345         }
1346     }
1347
1348   /* Test for popup frames hanging around. */
1349   /* Deletion of a parent frame with popups is deadly. */
1350   {
1351     Lisp_Object frmcons, devcons, concons;
1352
1353     FRAME_LOOP_NO_BREAK (frmcons, devcons, concons)
1354       {
1355         Lisp_Object this = XCAR (frmcons);
1356
1357
1358         if (! EQ (this, frame))
1359           {
1360             struct device *devcons_d = XDEVICE (XCAR (devcons));
1361             if (EQ (frame, DEVMETH_OR_GIVEN (devcons_d, get_frame_parent,
1362                                              (XFRAME (this)),
1363                                              Qnil)))
1364               /* We've found a popup frame whose parent is this frame. */
1365               signal_simple_error
1366                 ("Attempt to delete a frame with live popups", frame);
1367           }
1368       }
1369   }
1370
1371   /* Before here, we haven't made any dangerous changes (just checked for
1372      error conditions).  Now run the delete-frame-hook.  Remember that
1373      user code there could do any number of dangerous things, including
1374      signalling an error. */
1375
1376   va_run_hook_with_args (Qdelete_frame_hook, 1, frame);
1377
1378   if (!FRAME_LIVE_P (f)) /* Make sure the delete-frame-hook didn't */
1379     {                    /* go ahead and delete anything. */
1380       UNGCPRO;
1381       return;
1382     }
1383
1384   /* Call the delete-device-hook and delete-console-hook now if
1385      appropriate, before we do any dangerous things -- they too could
1386      signal an error. */
1387   if (XINT (Flength (DEVICE_FRAME_LIST (d))) == 1)
1388     {
1389       va_run_hook_with_args (Qdelete_device_hook, 1, device);
1390       if (!FRAME_LIVE_P (f)) /* Make sure the delete-device-hook didn't */
1391         {                    /* go ahead and delete anything. */
1392           UNGCPRO;
1393           return;
1394         }
1395
1396       if (XINT (Flength (CONSOLE_DEVICE_LIST (con))) == 1)
1397         {
1398           va_run_hook_with_args (Qdelete_console_hook, 1, console);
1399           if (!FRAME_LIVE_P (f)) /* Make sure the delete-console-hook didn't */
1400             {                    /* go ahead and delete anything. */
1401               UNGCPRO;
1402               return;
1403             }
1404         }
1405     }
1406
1407   minibuffer_selected = EQ (minibuf_window, Fselected_window (Qnil));
1408
1409   /* If we were focused on this frame, then we're not any more.
1410      Assume that we lost the focus; that way, the call to
1411      Fselect_frame() below won't end up making us explicitly
1412      focus on another frame, which is generally undesirable in
1413      a point-to-type world.  If our mouse ends up sitting over
1414      another frame, we will receive a FocusIn event and end up
1415      making that frame the selected frame.
1416
1417      #### This may not be an ideal solution in a click-to-type
1418      world (in that case, we might want to explicitly choose
1419      another frame to have the focus, rather than relying on
1420      the WM, which might focus on a frame in a different app
1421      or focus on nothing at all).  But there's no easy way
1422      to detect which focus model we're running on, and the
1423      alternative is more heinous. */
1424
1425   if (EQ (frame, DEVICE_FRAME_WITH_FOCUS_REAL (d)))
1426     DEVICE_FRAME_WITH_FOCUS_REAL (d) = Qnil;
1427   if (EQ (frame, DEVICE_FRAME_WITH_FOCUS_FOR_HOOKS (d)))
1428     DEVICE_FRAME_WITH_FOCUS_FOR_HOOKS (d) = Qnil;
1429   if (EQ (frame, DEVICE_FRAME_THAT_OUGHT_TO_HAVE_FOCUS (d)))
1430     DEVICE_FRAME_THAT_OUGHT_TO_HAVE_FOCUS (d) = Qnil;
1431
1432   /* Don't allow the deleted frame to remain selected.
1433      Note that in the former scheme of things, this would
1434      have caused us to regain the focus.  This no longer
1435      applies (see above); I think the new behavior is more
1436      logical.  If someone disagrees, it can always be
1437      changed (or a new user variable can be introduced, ugh.) */
1438   if (EQ (frame, DEVICE_SELECTED_FRAME (d)))
1439     {
1440       Lisp_Object next;
1441
1442       /* If this is a popup frame, select its parent if possible.
1443          Otherwise, find another visible frame; if none, just take any frame.
1444          First try the same device, then the same console. */
1445
1446       next = DEVMETH_OR_GIVEN (d, get_frame_parent, (f), Qnil);
1447       if (NILP (next) || EQ (next, frame) || ! FRAME_LIVE_P (XFRAME (next)))
1448         next = next_frame_internal (frame, Qvisible, device,
1449                                     called_from_delete_device);
1450       if (NILP (next) || EQ (next, frame))
1451         next = next_frame_internal (frame, Qvisible, console,
1452                                     called_from_delete_device);
1453       if (NILP (next) || EQ (next, frame))
1454         next = next_frame_internal (frame, Qvisible, Qt,
1455                                     called_from_delete_device);
1456       if (NILP (next) || EQ (next, frame))
1457         next = next_frame_internal (frame, Qt, device,
1458                                     called_from_delete_device);
1459       if (NILP (next) || EQ (next, frame))
1460         next = next_frame_internal (frame, Qt, console,
1461                                     called_from_delete_device);
1462       if (NILP (next) || EQ (next, frame))
1463         next = next_frame_internal (frame, Qt, Qt, called_from_delete_device);
1464
1465       /* if we haven't found another frame at this point
1466          then there aren't any. */
1467       if (NILP (next) || EQ (next, frame))
1468         ;
1469       else
1470         {
1471           int did_select = 0;
1472           /* if this is the global selected frame, select another one. */
1473           if (EQ (frame, Fselected_frame (Qnil)))
1474             {
1475                 Fselect_frame (next);
1476                 did_select = 1;
1477             }
1478           /*
1479            * If the new frame we just selected is on a different
1480            * device then we still need to change DEVICE_SELECTED_FRAME(d)
1481            * to a live frame, if there are any left on this device.
1482            */
1483           if (!EQ (device, FRAME_DEVICE(XFRAME(next))))
1484             {
1485                 Lisp_Object next_f =
1486                     next_frame_internal (frame, Qt, device,
1487                                          called_from_delete_device);
1488                 if (NILP (next_f) || EQ (next_f, frame))
1489                   ;
1490                 else
1491                   set_device_selected_frame (d, next_f);
1492             }
1493           else if (! did_select)
1494             set_device_selected_frame (d, next);
1495
1496         }
1497     }
1498
1499   /* Don't allow minibuf_window to remain on a deleted frame.  */
1500   if (EQ (f->minibuffer_window, minibuf_window))
1501     {
1502       struct frame *sel_frame = selected_frame ();
1503       Fset_window_buffer (sel_frame->minibuffer_window,
1504                           XWINDOW (minibuf_window)->buffer);
1505       minibuf_window = sel_frame->minibuffer_window;
1506
1507       /* If the dying minibuffer window was selected,
1508          select the new one.  */
1509       if (minibuffer_selected)
1510         Fselect_window (minibuf_window, Qnil);
1511     }
1512
1513   /* After this point, no errors must be allowed to occur. */
1514
1515 #ifdef HAVE_MENUBARS
1516   free_frame_menubars (f);
1517 #endif
1518 #ifdef HAVE_SCROLLBARS
1519   free_frame_scrollbars (f);
1520 #endif
1521 #ifdef HAVE_TOOLBARS
1522   free_frame_toolbars (f);
1523 #endif
1524
1525   /* This must be done before the window and window_mirror structures
1526      are freed.  The scrollbar information is attached to them. */
1527   MAYBE_FRAMEMETH (f, delete_frame, (f));
1528
1529   /* Mark all the windows that used to be on FRAME as deleted, and then
1530      remove the reference to them.  */
1531   delete_all_subwindows (XWINDOW (f->root_window));
1532   f->root_window = Qnil;
1533
1534   /* Remove the frame now from the list.  This way, any events generated
1535      on this frame by the maneuvers below will disperse themselves. */
1536
1537   /* This used to be Fdelq(), but that will cause a seg fault if the
1538      QUIT checker happens to get invoked, because the frame list is in
1539      an inconsistent state. */
1540   d->frame_list = delq_no_quit (frame, d->frame_list);
1541   RESET_CHANGED_SET_FLAGS;
1542
1543   f->dead = 1;
1544   f->visible = 0;
1545
1546   free_window_mirror (f->root_mirror);
1547 /*  free_line_insertion_deletion_costs (f); */
1548
1549   /* If we've deleted the last non-minibuf frame, then try to find
1550      another one.  */
1551   if (EQ (frame, CONSOLE_LAST_NONMINIBUF_FRAME (con)))
1552     {
1553       Lisp_Object frmcons, devcons;
1554
1555       set_console_last_nonminibuf_frame (con, Qnil);
1556
1557       CONSOLE_FRAME_LOOP_NO_BREAK (frmcons, devcons, con)
1558         {
1559           Lisp_Object ecran = XCAR (frmcons);
1560           if (!FRAME_MINIBUF_ONLY_P (XFRAME (ecran)))
1561             {
1562               set_console_last_nonminibuf_frame (con, ecran);
1563               goto double_break_1;
1564             }
1565         }
1566     }
1567  double_break_1:
1568
1569 #if 0
1570   /* The following test is degenerate FALSE */
1571   if (called_from_delete_device < 0)
1572     /* then we're being called from delete-console, and we shouldn't
1573        try to find another default-minibuffer frame for the console.
1574        */
1575     con->default_minibuffer_frame = Qnil;
1576 #endif
1577
1578   /* If we've deleted this console's default_minibuffer_frame, try to
1579      find another one.  Prefer minibuffer-only frames, but also notice
1580      frames with other windows.  */
1581   if (EQ (frame, con->default_minibuffer_frame))
1582     {
1583       Lisp_Object frmcons, devcons;
1584       /* The last frame we saw with a minibuffer, minibuffer-only or not.  */
1585       Lisp_Object frame_with_minibuf;
1586       /* Some frame we found on the same console, or nil if there are none. */
1587       Lisp_Object frame_on_same_console;
1588
1589       frame_on_same_console = Qnil;
1590       frame_with_minibuf = Qnil;
1591
1592       set_console_last_nonminibuf_frame (con, Qnil);
1593
1594       CONSOLE_FRAME_LOOP_NO_BREAK (frmcons, devcons, con)
1595         {
1596           Lisp_Object this;
1597           struct frame *f1;
1598
1599           this = XCAR (frmcons);
1600           f1 = XFRAME (this);
1601
1602           /* Consider only frames on the same console
1603              and only those with minibuffers.  */
1604           if (FRAME_HAS_MINIBUF_P (f1))
1605             {
1606               frame_with_minibuf = this;
1607               if (FRAME_MINIBUF_ONLY_P (f1))
1608                 goto double_break_2;
1609             }
1610
1611           frame_on_same_console = this;
1612         }
1613     double_break_2:
1614
1615       if (!NILP (frame_on_same_console))
1616         {
1617           /* We know that there must be some frame with a minibuffer out
1618              there.  If this were not true, all of the frames present
1619              would have to be minibuffer-less, which implies that at some
1620              point their minibuffer frames must have been deleted, but
1621              that is prohibited at the top; you can't delete surrogate
1622              minibuffer frames.  */
1623           if (NILP (frame_with_minibuf))
1624             abort ();
1625
1626           con->default_minibuffer_frame = frame_with_minibuf;
1627         }
1628       else
1629         /* No frames left on this console--say no minibuffer either.  */
1630         con->default_minibuffer_frame = Qnil;
1631     }
1632
1633   nuke_all_frame_slots (f); /* nobody should be accessing the device
1634                                or anything else any more, and making
1635                                them Qnil allows for better GC'ing
1636                                in case a pointer to the dead frame
1637                                continues to hang around. */
1638   f->framemeths = dead_console_methods;
1639   UNGCPRO;
1640 }
1641
1642 void
1643 io_error_delete_frame (Lisp_Object frame)
1644 {
1645   delete_frame_internal (XFRAME (frame), 1, 0, 1);
1646 }
1647
1648 DEFUN ("delete-frame", Fdelete_frame, 0, 2, "", /*
1649 Delete FRAME, permanently eliminating it from use.
1650 If omitted, FRAME defaults to the selected frame.
1651 A frame may not be deleted if its minibuffer is used by other frames.
1652 Normally, you cannot delete the last non-minibuffer-only frame (you must
1653 use `save-buffers-kill-emacs' or `kill-emacs').  However, if optional
1654 second argument FORCE is non-nil, you can delete the last frame. (This
1655 will automatically call `save-buffers-kill-emacs'.)
1656 */
1657        (frame, force))
1658 {
1659   /* This function can GC */
1660   struct frame *f;
1661
1662   if (NILP (frame))
1663     {
1664       f = selected_frame ();
1665       XSETFRAME (frame, f);
1666     }
1667   else
1668     {
1669       CHECK_FRAME (frame);
1670       f = XFRAME (frame);
1671     }
1672
1673   delete_frame_internal (f, !NILP (force), 0, 0);
1674   return Qnil;
1675 }
1676
1677 \f
1678 /* Return mouse position in character cell units.  */
1679
1680 static int
1681 mouse_pixel_position_1 (struct device *d, Lisp_Object *frame,
1682                         int *x, int *y)
1683 {
1684   switch (DEVMETH_OR_GIVEN (d, get_mouse_position, (d, frame, x, y), -1))
1685     {
1686     case 1:
1687       return 1;
1688
1689     case 0:
1690       *frame = Qnil;
1691       break;
1692
1693     case -1:
1694       *frame = DEVICE_SELECTED_FRAME (d);
1695       break;
1696
1697     default:
1698       abort (); /* method is incorrectly written */
1699     }
1700
1701   return 0;
1702 }
1703
1704 DEFUN ("mouse-pixel-position", Fmouse_pixel_position, 0, 1, 0, /*
1705 Return a list (WINDOW X . Y) giving the current mouse window and position.
1706 The position is given in pixel units, where (0, 0) is the upper-left corner.
1707
1708 When the cursor is not over a window, the return value is a list (nil nil).
1709
1710 DEVICE specifies the device on which to read the mouse position, and
1711 defaults to the selected device.  If the device is a mouseless terminal
1712 or Emacs hasn't been programmed to read its mouse position, it returns
1713 the device's selected window for WINDOW and nil for X and Y.
1714 */
1715        (device))
1716 {
1717   struct device *d = decode_device (device);
1718   Lisp_Object frame;
1719   Lisp_Object window = Qnil;
1720   Lisp_Object x = Qnil;
1721   Lisp_Object y = Qnil;
1722   int intx, inty;
1723
1724   if (mouse_pixel_position_1 (d, &frame, &intx, &inty) > 0)
1725     {
1726       struct window *w =
1727         find_window_by_pixel_pos (intx, inty, XFRAME (frame)->root_window);
1728       if (w)
1729         {
1730           XSETWINDOW (window, w);
1731
1732           /* Adjust the position to be relative to the window. */
1733           intx -= w->pixel_left;
1734           inty -= w->pixel_top;
1735           XSETINT (x, intx);
1736           XSETINT (y, inty);
1737         }
1738     }
1739   else if (FRAMEP (frame))
1740     window = FRAME_SELECTED_WINDOW (XFRAME (frame));
1741
1742   return Fcons (window, Fcons (x, y));
1743 }
1744
1745 DEFUN ("mouse-position", Fmouse_position, 0, 1, 0, /*
1746 Return a list (WINDOW X . Y) giving the current mouse window and position.
1747 The position is of a character under cursor, where (0, 0) is the upper-left
1748 corner of the window.
1749
1750 When the cursor is not over a character, or not over a window, the return
1751 value is a list (nil nil).
1752
1753 DEVICE specifies the device on which to read the mouse position, and
1754 defaults to the selected device.  If the device is a mouseless terminal
1755 or Emacs hasn't been programmed to read its mouse position, it returns
1756 the device's selected window for WINDOW and nil for X and Y.
1757 */
1758        (device))
1759 {
1760   struct device *d = decode_device (device);
1761   struct window *w;
1762   Lisp_Object frame, window = Qnil, lisp_x = Qnil, lisp_y = Qnil;
1763   int x, y, obj_x, obj_y;
1764   Bufpos bufpos, closest;
1765   Charcount modeline_closest;
1766   Lisp_Object obj1, obj2;
1767
1768   if (mouse_pixel_position_1 (d, &frame, &x, &y) > 0)
1769     {
1770       int res = pixel_to_glyph_translation (XFRAME (frame), x, y, &x, &y,
1771                                             &obj_x, &obj_y, &w, &bufpos,
1772                                             &closest, &modeline_closest,
1773                                             &obj1, &obj2);
1774       if (res == OVER_TEXT)
1775         {
1776           lisp_x = make_int (x);
1777           lisp_y = make_int (y);
1778           XSETWINDOW (window, w);
1779         }
1780     }
1781   else if (FRAMEP (frame))
1782     window = FRAME_SELECTED_WINDOW (XFRAME (frame));
1783
1784   return Fcons (window, Fcons (lisp_x, lisp_y));
1785 }
1786
1787 DEFUN ("mouse-position-as-motion-event", Fmouse_position_as_motion_event, 0, 1, 0, /*
1788 Return the current mouse position as a motion event.
1789 This allows you to call the standard event functions such as
1790 `event-over-toolbar-p' to determine where the mouse is.
1791
1792 DEVICE specifies the device on which to read the mouse position, and
1793 defaults to the selected device.  If the mouse position can't be determined
1794 \(e.g. DEVICE is a TTY device), nil is returned instead of an event.
1795 */
1796        (device))
1797 {
1798   struct device *d = decode_device (device);
1799   Lisp_Object frame;
1800   int intx, inty;
1801
1802   if (mouse_pixel_position_1 (d, &frame, &intx, &inty))
1803     {
1804       Lisp_Object event = Fmake_event (Qnil, Qnil);
1805       XEVENT (event)->event_type = pointer_motion_event;
1806       XEVENT (event)->channel = frame;
1807       XEVENT (event)->event.motion.x = intx;
1808       XEVENT (event)->event.motion.y = inty;
1809       return event;
1810     }
1811   else
1812     return Qnil;
1813 }
1814
1815 DEFUN ("set-mouse-position", Fset_mouse_position, 3, 3, 0, /*
1816 Move the mouse pointer to the center of character cell (X,Y) in WINDOW.
1817 Note, this is a no-op for an X frame that is not visible.
1818 If you have just created a frame, you must wait for it to become visible
1819 before calling this function on it, like this.
1820   (while (not (frame-visible-p frame)) (sleep-for .5))
1821 Note also: Warping the mouse is contrary to the ICCCM, so be very sure
1822  that the behavior won't end up being obnoxious!
1823 */
1824        (window, x, y))
1825 {
1826   struct window *w;
1827   int pix_x, pix_y;
1828
1829   CHECK_WINDOW (window);
1830   CHECK_INT (x);
1831   CHECK_INT (y);
1832
1833   /* Warping the mouse will cause EnterNotify and Focus events under X. */
1834   w = XWINDOW (window);
1835   glyph_to_pixel_translation (w, XINT (x), XINT (y), &pix_x, &pix_y);
1836
1837   MAYBE_FRAMEMETH (XFRAME (w->frame), set_mouse_position, (w, pix_x, pix_y));
1838
1839   return Qnil;
1840 }
1841
1842 DEFUN ("set-mouse-pixel-position", Fset_mouse_pixel_position, 3, 3, 0, /*
1843 Move the mouse pointer to pixel position (X,Y) in WINDOW.
1844 Note, this is a no-op for an X frame that is not visible.
1845 If you have just created a frame, you must wait for it to become visible
1846 before calling this function on it, like this.
1847   (while (not (frame-visible-p frame)) (sleep-for .5))
1848 */
1849        (window, x, y))
1850 {
1851   struct window *w;
1852
1853   CHECK_WINDOW (window);
1854   CHECK_INT (x);
1855   CHECK_INT (y);
1856
1857   /* Warping the mouse will cause EnterNotify and Focus events under X. */
1858   w = XWINDOW (window);
1859   FRAMEMETH (XFRAME (w->frame), set_mouse_position, (w, XINT (x), XINT (y)));
1860
1861   return Qnil;
1862 }
1863 \f
1864 DEFUN ("make-frame-visible", Fmake_frame_visible, 0, 1, 0, /*
1865 Make the frame FRAME visible (assuming it is an X-window).
1866 If omitted, FRAME defaults to the currently selected frame.
1867 Also raises the frame so that nothing obscures it.
1868 */
1869        (frame))
1870 {
1871   struct frame *f = decode_frame (frame);
1872
1873   MAYBE_FRAMEMETH (f, make_frame_visible, (f));
1874   return frame;
1875 }
1876
1877 DEFUN ("make-frame-invisible", Fmake_frame_invisible, 0, 2, 0, /*
1878 Unconditionally removes frame from the display (assuming it is an X-window).
1879 If omitted, FRAME defaults to the currently selected frame.
1880 If what you want to do is iconify the frame (if the window manager uses
1881 icons) then you should call `iconify-frame' instead.
1882 Normally you may not make FRAME invisible if all other frames are invisible
1883 and uniconified, but if the second optional argument FORCE is non-nil,
1884 you may do so.
1885 */
1886        (frame, force))
1887 {
1888   struct frame *f, *sel_frame;
1889   struct device *d;
1890
1891   f = decode_frame (frame);
1892   d = XDEVICE (FRAME_DEVICE (f));
1893   sel_frame = XFRAME (DEVICE_SELECTED_FRAME (d));
1894
1895   if (NILP (force) && !other_visible_frames (f))
1896     error ("Attempt to make invisible the sole visible or iconified frame");
1897
1898   /* Don't allow minibuf_window to remain on a deleted frame.  */
1899   if (EQ (f->minibuffer_window, minibuf_window))
1900     {
1901       Fset_window_buffer (sel_frame->minibuffer_window,
1902                           XWINDOW (minibuf_window)->buffer);
1903       minibuf_window = sel_frame->minibuffer_window;
1904     }
1905
1906   MAYBE_FRAMEMETH (f, make_frame_invisible, (f));
1907
1908   return Qnil;
1909 }
1910
1911 DEFUN ("iconify-frame", Ficonify_frame, 0, 1, "", /*
1912 Make the frame FRAME into an icon, if the window manager supports icons.
1913 If omitted, FRAME defaults to the currently selected frame.
1914 */
1915        (frame))
1916 {
1917   struct frame *f, *sel_frame;
1918   struct device *d;
1919
1920   f = decode_frame (frame);
1921   d = XDEVICE (FRAME_DEVICE (f));
1922   sel_frame = XFRAME (DEVICE_SELECTED_FRAME (d));
1923
1924   /* Don't allow minibuf_window to remain on a deleted frame.  */
1925   if (EQ (f->minibuffer_window, minibuf_window))
1926     {
1927       Fset_window_buffer (sel_frame->minibuffer_window,
1928                           XWINDOW (minibuf_window)->buffer);
1929       minibuf_window = sel_frame->minibuffer_window;
1930     }
1931
1932   MAYBE_FRAMEMETH (f, iconify_frame, (f));
1933
1934   return Qnil;
1935 }
1936
1937 DEFUN ("deiconify-frame", Fdeiconify_frame, 0, 1, 0, /*
1938 Open (de-iconify) the iconified frame FRAME.
1939 Under X, this is currently the same as `make-frame-visible'.
1940 If omitted, FRAME defaults to the currently selected frame.
1941 Also raises the frame so that nothing obscures it.
1942 */
1943        (frame))
1944 {
1945   return Fmake_frame_visible (frame);
1946 }
1947
1948 /* FSF returns 'icon for iconized frames.  What a crock! */
1949
1950 DEFUN ("frame-visible-p", Fframe_visible_p, 0, 1, 0, /*
1951 Return non NIL if FRAME is now "visible" (actually in use for display).
1952 A frame that is not visible is not updated, and, if it works through a
1953 window system, may not show at all.
1954 N.B. Under X "visible" means Mapped. It the window is mapped but not
1955 actually visible on screen then frame_visible returns 'hidden.
1956 */
1957        (frame))
1958 {
1959   struct frame *f = decode_frame (frame);
1960   int visible = FRAMEMETH_OR_GIVEN (f, frame_visible_p, (f), f->visible);
1961   return visible ? ( visible > 0 ? Qt : Qhidden ) : Qnil;
1962 }
1963
1964 DEFUN ("frame-totally-visible-p", Fframe_totally_visible_p, 0, 1, 0, /*
1965 Return t if frame is not obscured by any other window system windows.
1966 Always returns t for tty frames.
1967 */
1968        (frame))
1969 {
1970   struct frame *f = decode_frame (frame);
1971   return (FRAMEMETH_OR_GIVEN (f, frame_totally_visible_p, (f), f->visible)
1972           ? Qt : Qnil);
1973 }
1974
1975 DEFUN ("frame-iconified-p", Fframe_iconified_p, 0, 1, 0, /*
1976 Return t if FRAME is iconified.
1977 Not all window managers use icons; some merely unmap the window, so this
1978 function is not the inverse of `frame-visible-p'.  It is possible for a
1979 frame to not be visible and not be iconified either.  However, if the
1980 frame is iconified, it will not be visible.
1981 */
1982        (frame))
1983 {
1984   struct frame *f = decode_frame (frame);
1985   if (f->visible)
1986     return Qnil;
1987   f->iconified = FRAMEMETH_OR_GIVEN (f, frame_iconified_p, (f), 0);
1988   return f->iconified ? Qt : Qnil;
1989 }
1990
1991 DEFUN ("visible-frame-list", Fvisible_frame_list, 0, 1, 0, /*
1992 Return a list of all frames now "visible" (being updated).
1993 If DEVICE is specified only frames on that device will be returned.
1994 Note that under virtual window managers not all these frame are necessarily
1995 really updated.
1996 */
1997        (device))
1998 {
1999   Lisp_Object devcons, concons;
2000   struct frame *f;
2001   Lisp_Object value;
2002
2003   value = Qnil;
2004
2005   DEVICE_LOOP_NO_BREAK (devcons, concons)
2006     {
2007       assert (DEVICEP (XCAR (devcons)));
2008
2009       if (NILP (device) || EQ (device, XCAR (devcons)))
2010         {
2011           Lisp_Object frmcons;
2012
2013           DEVICE_FRAME_LOOP (frmcons, XDEVICE (XCAR (devcons)))
2014             {
2015               Lisp_Object frame = XCAR (frmcons);
2016               f = XFRAME (frame);
2017               if (FRAME_VISIBLE_P(f))
2018                 value = Fcons (frame, value);
2019             }
2020         }
2021     }
2022
2023   return value;
2024 }
2025
2026 \f
2027 DEFUN ("raise-frame", Fraise_frame, 0, 1, "", /*
2028 Bring FRAME to the front, so it occludes any frames it overlaps.
2029 If omitted, FRAME defaults to the currently selected frame.
2030 If FRAME is invisible, make it visible.
2031 If Emacs is displaying on an ordinary terminal or some other device which
2032 doesn't support multiple overlapping frames, this function does nothing.
2033 */
2034        (frame))
2035 {
2036   struct frame *f = decode_frame (frame);
2037
2038   /* Do like the documentation says. */
2039   Fmake_frame_visible (frame);
2040   MAYBE_FRAMEMETH (f, raise_frame, (f));
2041   return Qnil;
2042 }
2043
2044 DEFUN ("lower-frame", Flower_frame, 0, 1, "", /*
2045 Send FRAME to the back, so it is occluded by any frames that overlap it.
2046 If omitted, FRAME defaults to the currently selected frame.
2047 If Emacs is displaying on an ordinary terminal or some other device which
2048 doesn't support multiple overlapping frames, this function does nothing.
2049 */
2050        (frame))
2051 {
2052   struct frame *f = decode_frame (frame);
2053
2054   MAYBE_FRAMEMETH (f, lower_frame, (f));
2055   return Qnil;
2056 }
2057
2058 /* Ben thinks there is no need for `redirect-frame-focus' or `frame-focus',
2059    crockish FSFmacs functions.  See summary on focus in event-stream.c. */
2060
2061 \f
2062 /***************************************************************************/
2063 /*                           frame properties                              */
2064 /***************************************************************************/
2065
2066 static void internal_set_frame_size (struct frame *f, int cols, int rows,
2067                                      int pretend);
2068
2069 static void
2070 store_minibuf_frame_prop (struct frame *f, Lisp_Object val)
2071 {
2072   Lisp_Object frame;
2073   XSETFRAME (frame, f);
2074
2075   if (WINDOWP (val))
2076     {
2077       if (! MINI_WINDOW_P (XWINDOW (val)))
2078         signal_simple_error
2079           ("Surrogate minibuffer windows must be minibuffer windows",
2080            val);
2081
2082       if (FRAME_HAS_MINIBUF_P (f) || FRAME_MINIBUF_ONLY_P (f))
2083         signal_simple_error
2084           ("Can't change the surrogate minibuffer of a frame with its own minibuffer", frame);
2085
2086       /* Install the chosen minibuffer window, with proper buffer.  */
2087       f->minibuffer_window = val;
2088     }
2089   else if (EQ (val, Qt))
2090     {
2091       if (FRAME_HAS_MINIBUF_P (f) || FRAME_MINIBUF_ONLY_P (f))
2092         signal_simple_error
2093           ("Frame already has its own minibuffer", frame);
2094       else
2095         {
2096           setup_normal_frame (f);
2097           f->mirror_dirty = 1;
2098
2099           update_frame_window_mirror (f);
2100           internal_set_frame_size (f, f->width, f->height, 1);
2101         }
2102     }
2103 }
2104
2105 #if 0
2106
2107 /* possible code if you want to have symbols such as `default-background'
2108    map to setting the background of `default', etc. */
2109
2110 static int
2111 dissect_as_face_setting (Lisp_Object sym, Lisp_Object *face_out,
2112                          Lisp_Object *face_prop_out)
2113 {
2114   Lisp_Object list = Vbuilt_in_face_specifiers;
2115   struct Lisp_String *s;
2116
2117   if (!SYMBOLP (sym))
2118     return 0;
2119
2120   s = symbol_name (XSYMBOL (sym));
2121
2122   while (!NILP (list))
2123     {
2124       Lisp_Object prop = Fcar (list);
2125       struct Lisp_String *prop_name;
2126
2127       if (!SYMBOLP (prop))
2128         continue;
2129       prop_name = symbol_name (XSYMBOL (prop));
2130       if (string_length (s) > string_length (prop_name) + 1
2131           && !memcmp (string_data (prop_name),
2132                       string_data (s) + string_length (s)
2133                       - string_length (prop_name),
2134                       string_length (prop_name))
2135           && string_data (s)[string_length (s) - string_length (prop_name)
2136                              - 1] == '-')
2137         {
2138           Lisp_Object face =
2139             Ffind_face (make_string (string_data (s),
2140                                      string_length (s)
2141                                      - string_length (prop_name)
2142                                      - 1));
2143           if (!NILP (face))
2144             {
2145               *face_out = face;
2146               *face_prop_out = prop;
2147               return 1;
2148             }
2149         }
2150
2151       list = Fcdr (list);
2152     }
2153
2154   return 0;
2155 }
2156
2157 #endif /* 0 */
2158
2159 static Lisp_Object
2160 get_property_alias (Lisp_Object prop)
2161 {
2162   while (1)
2163     {
2164       Lisp_Object alias = Qnil;
2165
2166       if (SYMBOLP (prop))
2167         alias = Fget (prop, Qframe_property_alias, Qnil);
2168       if (NILP (alias))
2169         break;
2170       prop = alias;
2171       QUIT;
2172     }
2173
2174   return prop;
2175 }
2176
2177 /* #### Using this to modify the internal border width has no effect
2178    because the change isn't propagated to the windows.  Are there
2179    other properties which this claims to handle, but doesn't?
2180
2181    But of course.  This stuff needs more work, but it's a lot closer
2182    to sanity now than before with the horrible frame-params stuff. */
2183
2184 DEFUN ("set-frame-properties", Fset_frame_properties, 2, 2, 0, /*
2185 Change some properties of a frame.
2186 PLIST is a property list.
2187 You can also change frame properties individually using `set-frame-property',
2188 but it may be more efficient to change many properties at once.
2189
2190 Frame properties can be retrieved using `frame-property' or `frame-properties'.
2191
2192 The following symbols etc. have predefined meanings:
2193
2194  name           Name of the frame.  Used with X resources.
2195                 Unchangeable after creation.
2196
2197  height         Height of the frame, in lines.
2198
2199  width          Width of the frame, in characters.
2200
2201  minibuffer     Gives the minibuffer behavior for this frame.  Either
2202                 t (frame has its own minibuffer), `only' (frame is
2203                 a minibuffer-only frame), or a window (frame uses that
2204                 window, which is on another frame, as the minibuffer).
2205
2206  unsplittable   If non-nil, frame cannot be split by `display-buffer'.
2207
2208  current-display-table, menubar-visible-p, left-margin-width,
2209  right-margin-width, minimum-line-ascent, minimum-line-descent,
2210  use-left-overflow, use-right-overflow, scrollbar-width, scrollbar-height,
2211  default-toolbar, top-toolbar, bottom-toolbar, left-toolbar, right-toolbar,
2212  default-toolbar-height, default-toolbar-width, top-toolbar-height,
2213  bottom-toolbar-height, left-toolbar-width, right-toolbar-width,
2214  default-toolbar-visible-p, top-toolbar-visible-p, bottom-toolbar-visible-p,
2215  left-toolbar-visible-p, right-toolbar-visible-p, toolbar-buttons-captioned-p,
2216  top-toolbar-border-width, bottom-toolbar-border-width,
2217  left-toolbar-border-width, right-toolbar-border-width,
2218  modeline-shadow-thickness, has-modeline-p
2219                 [Giving the name of any built-in specifier variable is
2220                 equivalent to calling `set-specifier' on the specifier,
2221                 with a locale of FRAME.  Giving the name to `frame-property'
2222                 calls `specifier-instance' on the specifier.]
2223
2224  text-pointer-glyph, nontext-pointer-glyph, modeline-pointer-glyph,
2225  selection-pointer-glyph, busy-pointer-glyph, toolbar-pointer-glyph,
2226  menubar-pointer-glyph, scrollbar-pointer-glyph, gc-pointer-glyph,
2227  octal-escape-glyph, control-arrow-glyph, invisible-text-glyph,
2228  hscroll-glyph, truncation-glyph, continuation-glyph
2229                 [Giving the name of any glyph variable is equivalent to
2230                 calling `set-glyph-image' on the glyph, with a locale
2231                 of FRAME.  Giving the name to `frame-property' calls
2232                 `glyph-image-instance' on the glyph.]
2233
2234  [default foreground], [default background], [default font],
2235  [modeline foreground], [modeline background], [modeline font],
2236  etc.
2237                 [Giving a vector of a face and a property is equivalent
2238                 to calling `set-face-property' on the face and property,
2239                 with a locale of FRAME.  Giving the vector to
2240                 `frame-property' calls `face-property-instance' on the
2241                 face and property.]
2242
2243 Finally, if a frame property symbol has the property `frame-property-alias'
2244 on it, then the value will be used in place of that symbol when looking
2245 up and setting frame property values.  This allows you to alias one
2246 frame property name to another.
2247
2248 See the variables `default-x-frame-plist', `default-tty-frame-plist'
2249 and `default-mswindows-frame-plist' for a description of the properties
2250 recognized for particular types of frames.
2251 */
2252        (frame, plist))
2253 {
2254   struct frame *f = decode_frame (frame);
2255   Lisp_Object tail;
2256   Lisp_Object *tailp;
2257   struct gcpro gcpro1, gcpro2;
2258
2259   XSETFRAME (frame, f);
2260   GCPRO2 (frame, plist);
2261   Fcheck_valid_plist (plist);
2262   plist = Fcopy_sequence (plist);
2263   Fcanonicalize_lax_plist (plist, Qnil);
2264   for (tail = plist; !NILP (tail); tail = Fcdr (Fcdr (tail)))
2265     {
2266       Lisp_Object prop = Fcar (tail);
2267       Lisp_Object val = Fcar (Fcdr (tail));
2268
2269       prop = get_property_alias (prop);
2270
2271 #if 0
2272       /* mly wants this, but it's not reasonable to change the name of a
2273          frame after it has been created, because the old name was used
2274          for resource lookup. */
2275       if (EQ (prop, Qname))
2276         {
2277           CHECK_STRING (val);
2278           f->name = val;
2279         }
2280 #endif /* 0 */
2281       if (EQ (prop, Qminibuffer))
2282         store_minibuf_frame_prop (f, val);
2283       if (EQ (prop, Qunsplittable))
2284         f->no_split = !NILP (val);
2285       if (EQ (prop, Qbuffer_predicate))
2286         f->buffer_predicate = val;
2287       if (SYMBOLP (prop) && EQ (Fbuilt_in_variable_type (prop),
2288                                 Qconst_specifier))
2289         call3 (Qset_specifier, Fsymbol_value (prop), val, frame);
2290       if (SYMBOLP (prop) && !NILP (Fget (prop, Qconst_glyph_variable, Qnil)))
2291         call3 (Qset_glyph_image, Fsymbol_value (prop), val, frame);
2292       if (VECTORP (prop) && XVECTOR_LENGTH (prop) == 2)
2293         {
2294           Lisp_Object face_prop = XVECTOR_DATA (prop)[1];
2295           CHECK_SYMBOL (face_prop);
2296           call4 (Qset_face_property,
2297                  Fget_face (XVECTOR_DATA (prop)[0]),
2298                  face_prop, val, frame);
2299         }
2300     }
2301
2302   MAYBE_FRAMEMETH (f, set_frame_properties, (f, plist));
2303   for (tailp = &plist; !NILP (*tailp);)
2304     {
2305       Lisp_Object *next_tailp;
2306       Lisp_Object next;
2307       Lisp_Object prop;
2308
2309       next = Fcdr (*tailp);
2310       CHECK_CONS (next);
2311       next_tailp = &XCDR (next);
2312       prop = Fcar (*tailp);
2313
2314       prop = get_property_alias (prop);
2315
2316       if (EQ (prop, Qminibuffer)
2317           || EQ (prop, Qunsplittable)
2318           || EQ (prop, Qbuffer_predicate)
2319           || EQ (prop, Qheight)
2320           || EQ (prop, Qwidth)
2321           || (SYMBOLP (prop) && EQ (Fbuilt_in_variable_type (prop),
2322                                     Qconst_specifier))
2323           || (SYMBOLP (prop) && !NILP (Fget (prop, Qconst_glyph_variable,
2324                                              Qnil)))
2325           || (VECTORP (prop) && XVECTOR_LENGTH (prop) == 2)
2326           || FRAMEMETH_OR_GIVEN (f, internal_frame_property_p, (f, prop), 0))
2327         *tailp = *next_tailp;
2328       tailp = next_tailp;
2329     }
2330
2331   f->plist = nconc2 (plist, f->plist);
2332   Fcanonicalize_lax_plist (f->plist, Qnil);
2333   UNGCPRO;
2334   return Qnil;
2335 }
2336
2337 DEFUN ("frame-property", Fframe_property, 2, 3, 0, /*
2338 Return FRAME's value for property PROPERTY.
2339 See `set-frame-properties' for the built-in property names.
2340 */
2341        (frame, property, default_))
2342 {
2343   struct frame *f = decode_frame (frame);
2344   Lisp_Object value;
2345
2346   XSETFRAME (frame, f);
2347
2348   property = get_property_alias (property);
2349
2350   if (EQ (Qname, property)) return f->name;
2351
2352   if (EQ (Qheight, property) || EQ (Qwidth, property))
2353     {
2354       if (window_system_pixelated_geometry (frame))
2355         {
2356           int width, height;
2357           pixel_to_real_char_size (f, FRAME_PIXWIDTH (f), FRAME_PIXHEIGHT (f),
2358                                    &width, &height);
2359           return make_int (EQ (Qheight, property) ? height: width);
2360         }
2361       else
2362         return make_int (EQ (Qheight, property) ?
2363                          FRAME_HEIGHT (f) :
2364                          FRAME_WIDTH  (f));
2365     }
2366
2367   /* NOTE: FSF returns Qnil instead of Qt for FRAME_HAS_MINIBUF_P.
2368      This is over-the-top bogosity, because it's inconsistent with
2369      the semantics of `minibuffer' when passed to `make-frame'.
2370      Returning Qt makes things consistent. */
2371   if (EQ (Qminibuffer, property))
2372     return (FRAME_MINIBUF_ONLY_P (f) ? Qonly :
2373             FRAME_HAS_MINIBUF_P  (f) ? Qt    :
2374             FRAME_MINIBUF_WINDOW (f));
2375   if (EQ (Qunsplittable, property))
2376     return FRAME_NO_SPLIT_P (f) ? Qt : Qnil;
2377   if (EQ (Qbuffer_predicate, property))
2378     return f->buffer_predicate;
2379
2380   if (SYMBOLP (property))
2381     {
2382       if (EQ (Fbuilt_in_variable_type (property), Qconst_specifier))
2383         return Fspecifier_instance (Fsymbol_value (property),
2384                                     frame, default_, Qnil);
2385       if (!NILP (Fget (property, Qconst_glyph_variable, Qnil)))
2386         {
2387           Lisp_Object glyph = Fsymbol_value (property);
2388           CHECK_GLYPH (glyph);
2389           return Fspecifier_instance (XGLYPH_IMAGE (glyph),
2390                                       frame, default_, Qnil);
2391         }
2392     }
2393
2394   if (VECTORP (property) && XVECTOR_LENGTH (property) == 2)
2395     {
2396       Lisp_Object face_prop = XVECTOR_DATA (property)[1];
2397       CHECK_SYMBOL (face_prop);
2398       return call3 (Qface_property_instance,
2399                     Fget_face (XVECTOR_DATA (property)[0]),
2400                     face_prop, frame);
2401     }
2402
2403   if (HAS_FRAMEMETH_P (f, frame_property))
2404     if (!UNBOUNDP (value = FRAMEMETH (f, frame_property, (f, property))))
2405       return value;
2406
2407   if (!UNBOUNDP (value = external_plist_get (&f->plist, property, 1, ERROR_ME)))
2408     return value;
2409
2410   return default_;
2411 }
2412
2413 DEFUN ("frame-properties", Fframe_properties, 0, 1, 0, /*
2414 Return a property list of the properties of FRAME.
2415 Do not modify this list; use `set-frame-property' instead.
2416 */
2417        (frame))
2418 {
2419   struct frame *f = decode_frame (frame);
2420   Lisp_Object result = Qnil;
2421   struct gcpro gcpro1;
2422
2423   GCPRO1 (result);
2424
2425   XSETFRAME (frame, f);
2426
2427   /* #### for the moment (since old code uses `frame-parameters'),
2428      we call `copy-sequence' on f->plist.  That allows frame-parameters
2429      to destructively convert the plist into an alist, which is more
2430      efficient than doing it non-destructively.  At some point we
2431      should remove the call to copy-sequence. */
2432   result = Fcopy_sequence (f->plist);
2433
2434   /* #### should we be adding all the specifiers and glyphs?
2435      That would entail having a list of them all. */
2436   if (HAS_FRAMEMETH_P (f, frame_properties))
2437     result = nconc2 (FRAMEMETH (f, frame_properties, (f)), result);
2438
2439   if (!NILP (f->buffer_predicate))
2440     result = cons3 (Qbuffer_predicate, f->buffer_predicate, result);
2441
2442   if (FRAME_NO_SPLIT_P (f))
2443     result = cons3 (Qunsplittable, Qt, result);
2444
2445   /* NOTE: FSF returns Qnil instead of Qt for FRAME_HAS_MINIBUF_P.
2446      This is over-the-top bogosity, because it's inconsistent with
2447      the semantics of `minibuffer' when passed to `make-frame'.
2448      Returning Qt makes things consistent. */
2449   result = cons3 (Qminibuffer,
2450                   (FRAME_MINIBUF_ONLY_P (f) ? Qonly :
2451                    FRAME_HAS_MINIBUF_P  (f) ? Qt    :
2452                    FRAME_MINIBUF_WINDOW (f)),
2453                   result);
2454   {
2455     int width, height;
2456
2457     if (window_system_pixelated_geometry (frame))
2458       {
2459         pixel_to_real_char_size (f, FRAME_PIXWIDTH (f), FRAME_PIXHEIGHT (f),
2460                                  &width, &height);
2461       }
2462     else
2463       {
2464         height = FRAME_HEIGHT (f);
2465         width = FRAME_WIDTH (f);
2466       }
2467     result = cons3 (Qwidth , make_int (width),  result);
2468     result = cons3 (Qheight, make_int (height), result);
2469   }
2470
2471   result = cons3 (Qname, f->name, result);
2472
2473   UNGCPRO;
2474   return result;
2475 }
2476
2477 \f
2478 DEFUN ("frame-pixel-height", Fframe_pixel_height, 0, 1, 0, /*
2479 Return the height in pixels of FRAME.
2480 */
2481        (frame))
2482 {
2483   return make_int (decode_frame (frame)->pixheight);
2484 }
2485
2486 DEFUN ("frame-pixel-width", Fframe_pixel_width, 0, 1, 0, /*
2487 Return the width in pixels of FRAME.
2488 */
2489        (frame))
2490 {
2491   return make_int (decode_frame (frame)->pixwidth);
2492 }
2493
2494 DEFUN ("frame-name", Fframe_name, 0, 1, 0, /*
2495 Return the name of FRAME (defaulting to the selected frame).
2496 This is not the same as the `title' of the frame.
2497 */
2498        (frame))
2499 {
2500   return decode_frame (frame)->name;
2501 }
2502
2503 DEFUN ("frame-modified-tick", Fframe_modified_tick, 0, 1, 0, /*
2504 Return FRAME's tick counter, incremented for each change to the frame.
2505 Each frame has a tick counter which is incremented each time the frame
2506 is resized, a window is resized, added, or deleted, a face is changed,
2507 `set-window-buffer' or `select-window' is called on a window in the
2508 frame, the window-start of a window in the frame has changed, or
2509 anything else interesting has happened.  It wraps around occasionally.
2510 No argument or nil as argument means use selected frame as FRAME.
2511 */
2512        (frame))
2513 {
2514   return make_int (decode_frame (frame)->modiff);
2515 }
2516
2517 static void
2518 internal_set_frame_size (struct frame *f, int cols, int rows, int pretend)
2519 {
2520   /* An explicit size change cancels any pending frame size adjustment */
2521   CLEAR_FRAME_SIZE_SLIPPED(f);
2522
2523   if (pretend || !HAS_FRAMEMETH_P (f, set_frame_size))
2524     change_frame_size (f, rows, cols, 0);
2525   else
2526     FRAMEMETH (f, set_frame_size, (f, cols, rows));
2527 }
2528
2529 DEFUN ("set-frame-height", Fset_frame_height, 2, 3, 0, /*
2530 Specify that the frame FRAME has LINES lines.
2531 Optional third arg non-nil means that redisplay should use LINES lines
2532 but that the idea of the actual height of the frame should not be changed.
2533 */
2534        (frame, rows, pretend))
2535 {
2536   struct frame *f = decode_frame (frame);
2537   int height, width;
2538   XSETFRAME (frame, f);
2539   CHECK_INT (rows);
2540
2541   if (window_system_pixelated_geometry (frame))
2542     {
2543       char_to_real_pixel_size (f, 0, XINT (rows), 0, &height);
2544       width = FRAME_PIXWIDTH (f);
2545     }
2546   else
2547     {
2548       height = XINT (rows);
2549       width = FRAME_WIDTH (f);
2550     }
2551
2552   internal_set_frame_size (f, width, height, !NILP (pretend));
2553   return frame;
2554 }
2555
2556 DEFUN ("set-frame-width", Fset_frame_width, 2, 3, 0, /*
2557 Specify that the frame FRAME has COLS columns.
2558 Optional third arg non-nil means that redisplay should use COLS columns
2559 but that the idea of the actual width of the frame should not be changed.
2560 */
2561        (frame, cols, pretend))
2562 {
2563   struct frame *f = decode_frame (frame);
2564   int width, height;
2565   XSETFRAME (frame, f);
2566   CHECK_INT (cols);
2567
2568   if (window_system_pixelated_geometry (frame))
2569     {
2570       char_to_real_pixel_size (f, XINT (cols), 0, &width, 0);
2571       height = FRAME_PIXHEIGHT (f);
2572     }
2573   else
2574     {
2575       width = XINT (cols);
2576       height = FRAME_HEIGHT (f);
2577     }
2578
2579   internal_set_frame_size (f, width, height, !NILP (pretend));
2580   return frame;
2581 }
2582
2583 DEFUN ("set-frame-size", Fset_frame_size, 3, 4, 0, /*
2584 Set the size of FRAME to COLS by ROWS.
2585 Optional fourth arg non-nil means that redisplay should use COLS by ROWS
2586 but that the idea of the actual size of the frame should not be changed.
2587 */
2588        (frame, cols, rows, pretend))
2589 {
2590   struct frame *f = decode_frame (frame);
2591   int height, width;
2592   XSETFRAME (frame, f);
2593   CHECK_INT (cols);
2594   CHECK_INT (rows);
2595
2596   if (window_system_pixelated_geometry (frame))
2597     char_to_real_pixel_size (f, XINT (cols), XINT (rows), &width, &height);
2598   else
2599     {
2600       height = XINT (rows);
2601       width = XINT (cols);
2602     }
2603
2604   internal_set_frame_size (f, width, height, !NILP (pretend));
2605   return frame;
2606 }
2607
2608 DEFUN ("set-frame-position", Fset_frame_position, 3, 3, 0, /*
2609 Set position of FRAME in pixels to XOFFSET by YOFFSET.
2610 This is actually the position of the upper left corner of the frame.
2611 Negative values for XOFFSET or YOFFSET are interpreted relative to
2612 the rightmost or bottommost possible position (that stays within the screen).
2613 */
2614        (frame, xoffset, yoffset))
2615 {
2616   struct frame *f = decode_frame (frame);
2617   CHECK_INT (xoffset);
2618   CHECK_INT (yoffset);
2619
2620   MAYBE_FRAMEMETH (f, set_frame_position, (f, XINT (xoffset), XINT (yoffset)));
2621
2622   return Qt;
2623 }
2624
2625 \f
2626
2627 /* Frame size conversion functions moved here from EmacsFrame.c
2628    because they're generic and really don't belong in that file.
2629    Function get_default_char_pixel_size() removed because it's
2630    exactly the same as default_face_height_and_width(). */
2631 static void
2632 frame_conversion_internal (struct frame *f, int pixel_to_char,
2633                            int *pixel_width, int *pixel_height,
2634                            int *char_width, int *char_height,
2635                            int real_face)
2636 {
2637   int cpw;
2638   int cph;
2639   int egw;
2640   int obw, obh, bdr;
2641   Lisp_Object frame, window;
2642
2643   XSETFRAME (frame, f);
2644   if (real_face)
2645     default_face_height_and_width (frame, &cph, &cpw);
2646   else
2647     default_face_height_and_width_1 (frame, &cph, &cpw);
2648
2649   window = FRAME_SELECTED_WINDOW (f);
2650
2651   egw = max (glyph_width (Vcontinuation_glyph, Vdefault_face, 0, window),
2652              glyph_width (Vtruncation_glyph, Vdefault_face, 0, window));
2653   egw = max (egw, cpw);
2654   bdr = 2 * f->internal_border_width;
2655   obw = FRAME_SCROLLBAR_WIDTH (f) + FRAME_THEORETICAL_LEFT_TOOLBAR_WIDTH (f) +
2656     FRAME_THEORETICAL_RIGHT_TOOLBAR_WIDTH (f) +
2657     2 * FRAME_THEORETICAL_LEFT_TOOLBAR_BORDER_WIDTH (f) +
2658     2 * FRAME_THEORETICAL_RIGHT_TOOLBAR_BORDER_WIDTH (f);
2659   obh = FRAME_SCROLLBAR_HEIGHT (f) + FRAME_THEORETICAL_TOP_TOOLBAR_HEIGHT (f) +
2660     FRAME_THEORETICAL_BOTTOM_TOOLBAR_HEIGHT (f) +
2661     2 * FRAME_THEORETICAL_TOP_TOOLBAR_BORDER_WIDTH (f) +
2662     2 * FRAME_THEORETICAL_BOTTOM_TOOLBAR_BORDER_WIDTH (f);
2663
2664   if (pixel_to_char)
2665     {
2666       if (char_width)
2667         *char_width = 1 + ((*pixel_width - egw) - bdr - obw) / cpw;
2668       if (char_height)
2669         *char_height = (*pixel_height - bdr - obh) / cph;
2670     }
2671   else
2672     {
2673       if (pixel_width)
2674         *pixel_width = (*char_width - 1) * cpw + egw + bdr + obw;
2675       if (pixel_height)
2676         *pixel_height = *char_height * cph + bdr + obh;
2677     }
2678 }
2679
2680 /* This takes the size in pixels of the text area, and returns the number
2681    of characters that will fit there, taking into account the internal
2682    border width, and the pixel width of the line terminator glyphs (which
2683    always count as one "character" wide, even if they are not the same size
2684    as the default character size of the default font).  The frame scrollbar
2685    width and left and right toolbar widths are also subtracted out of the
2686    available width.  The frame scrollbar height and top and bottom toolbar
2687    heights are subtracted out of the available height.
2688
2689    Therefore the result is not necessarily a multiple of anything in
2690    particular.  */
2691 void
2692 pixel_to_char_size (struct frame *f, int pixel_width, int pixel_height,
2693                     int *char_width, int *char_height)
2694 {
2695   frame_conversion_internal (f, 1, &pixel_width, &pixel_height, char_width,
2696                              char_height, 0);
2697 }
2698
2699 /* Given a character size, this returns the minimum number of pixels
2700    necessary to display that many characters, taking into account the
2701    internal border width, scrollbar height and width, toolbar heights and
2702    widths and the size of the line terminator glyphs (assuming the line
2703    terminators take up exactly one character position).
2704
2705    Therefore the result is not necessarily a multiple of anything in
2706    particular.  */
2707 void
2708 char_to_pixel_size (struct frame *f, int char_width, int char_height,
2709                     int *pixel_width, int *pixel_height)
2710 {
2711   frame_conversion_internal (f, 0, pixel_width, pixel_height, &char_width,
2712                              &char_height, 0);
2713 }
2714
2715 /* Given a pixel size, rounds DOWN to the smallest size in pixels necessary
2716    to display the same number of characters as are displayable now.
2717  */
2718 void
2719 round_size_to_char (struct frame *f, int in_width, int in_height,
2720                     int *out_width, int *out_height)
2721 {
2722   int char_width;
2723   int char_height;
2724   pixel_to_char_size (f, in_width, in_height, &char_width, &char_height);
2725   char_to_pixel_size (f, char_width, char_height, out_width, out_height);
2726 }
2727
2728 /* Versions of the above which always account for real font metrics.
2729  */
2730 void
2731 pixel_to_real_char_size (struct frame *f, int pixel_width, int pixel_height,
2732                          int *char_width, int *char_height)
2733 {
2734   frame_conversion_internal (f, 1, &pixel_width, &pixel_height, char_width,
2735                              char_height, 1);
2736 }
2737
2738 void
2739 char_to_real_pixel_size (struct frame *f, int char_width, int char_height,
2740                          int *pixel_width, int *pixel_height)
2741 {
2742   frame_conversion_internal (f, 0, pixel_width, pixel_height, &char_width,
2743                              &char_height, 1);
2744 }
2745
2746 void
2747 round_size_to_real_char (struct frame *f, int in_width, int in_height,
2748                          int *out_width, int *out_height)
2749 {
2750   int char_width;
2751   int char_height;
2752   pixel_to_real_char_size (f, in_width, in_height, &char_width, &char_height);
2753   char_to_real_pixel_size (f, char_width, char_height, out_width, out_height);
2754 }
2755
2756 /* Change the frame height and/or width.  Values may be given as zero to
2757    indicate no change is to take place. */
2758 static void
2759 change_frame_size_1 (struct frame *f, int newheight, int newwidth)
2760 {
2761   Lisp_Object frame;
2762   int new_pixheight, new_pixwidth;
2763   int font_height, real_font_height, font_width;
2764
2765   /* #### Chuck -- shouldn't we be checking to see if the frame
2766      is being "changed" to its existing size, and do nothing if so? */
2767   /* No, because it would hose toolbar updates.  The toolbar
2768      update code relies on this function to cause window `top' and
2769      `left' coordinates to be recomputed even though no frame size
2770      change occurs. --kyle */
2771   if (in_display)
2772     abort ();
2773
2774   XSETFRAME (frame, f);
2775
2776   default_face_height_and_width (frame, &real_font_height, 0);
2777   default_face_height_and_width_1 (frame, &font_height, &font_width);
2778
2779   /* This size-change overrides any pending one for this frame.  */
2780   FRAME_NEW_HEIGHT (f) = 0;
2781   FRAME_NEW_WIDTH (f) = 0;
2782
2783   new_pixheight = newheight * font_height;
2784   new_pixwidth = (newwidth - 1) * font_width;
2785
2786   /* #### dependency on FRAME_WIN_P should be removed. */
2787   if (FRAME_WIN_P (f))
2788     {
2789       new_pixheight += FRAME_SCROLLBAR_HEIGHT (f);
2790       new_pixwidth += FRAME_SCROLLBAR_WIDTH (f);
2791     }
2792
2793   /* when frame_conversion_internal() calculated the number of rows/cols
2794      in the frame, the theoretical toolbar sizes were subtracted out.
2795      The calculations below adjust for real toolbar height/width in
2796      frame, which may be different from frame spec, taking the above
2797      fact into account */
2798   new_pixheight +=
2799     + FRAME_THEORETICAL_TOP_TOOLBAR_HEIGHT (f)
2800     + 2 * FRAME_THEORETICAL_TOP_TOOLBAR_BORDER_WIDTH (f)
2801     - FRAME_REAL_TOP_TOOLBAR_HEIGHT (f)
2802     - 2 * FRAME_REAL_TOP_TOOLBAR_BORDER_WIDTH (f);
2803
2804   new_pixheight +=
2805     + FRAME_THEORETICAL_BOTTOM_TOOLBAR_HEIGHT (f)
2806     + 2 * FRAME_THEORETICAL_BOTTOM_TOOLBAR_BORDER_WIDTH (f)
2807     - FRAME_REAL_BOTTOM_TOOLBAR_HEIGHT (f)
2808     - 2 * FRAME_REAL_BOTTOM_TOOLBAR_BORDER_WIDTH (f);
2809
2810   new_pixwidth +=
2811     + FRAME_THEORETICAL_LEFT_TOOLBAR_WIDTH (f)
2812     + 2 * FRAME_THEORETICAL_LEFT_TOOLBAR_BORDER_WIDTH (f)
2813     - FRAME_REAL_LEFT_TOOLBAR_WIDTH (f)
2814     - 2 * FRAME_REAL_LEFT_TOOLBAR_BORDER_WIDTH (f);
2815
2816   new_pixwidth +=
2817     + FRAME_THEORETICAL_RIGHT_TOOLBAR_WIDTH (f)
2818     + 2 * FRAME_THEORETICAL_RIGHT_TOOLBAR_BORDER_WIDTH (f)
2819     - FRAME_REAL_RIGHT_TOOLBAR_WIDTH (f)
2820     - 2 * FRAME_REAL_RIGHT_TOOLBAR_BORDER_WIDTH (f);
2821
2822   /* Adjust the width for the end glyph which may be a different width
2823      than the default character width. */
2824   {
2825     int adjustment, trunc_width, cont_width;
2826
2827     trunc_width = glyph_width (Vtruncation_glyph, Vdefault_face, 0,
2828                                FRAME_SELECTED_WINDOW (f));
2829     cont_width = glyph_width (Vcontinuation_glyph, Vdefault_face, 0,
2830                               FRAME_SELECTED_WINDOW (f));
2831     adjustment = max (trunc_width, cont_width);
2832     adjustment = max (adjustment, font_width);
2833
2834     new_pixwidth += adjustment;
2835   }
2836
2837   /* If we don't have valid values, exit. */
2838   if (!new_pixheight && !new_pixwidth)
2839     return;
2840
2841   if (new_pixheight)
2842     {
2843       XWINDOW (FRAME_ROOT_WINDOW (f))->pixel_top = FRAME_TOP_BORDER_END (f);
2844
2845       if (FRAME_HAS_MINIBUF_P (f)
2846           && ! FRAME_MINIBUF_ONLY_P (f))
2847         /* Frame has both root and minibuffer.  */
2848         {
2849           /*
2850            * Leave the minibuffer height the same if the frame has
2851            * been initialized, and the minibuffer height is tall
2852            * enough to display at least one line of text in the default
2853            * font, and the old minibuffer height is a multiple of the
2854            * default font height.  This should cause the minibuffer
2855            * height to be recomputed on font changes but not for
2856            * other frame size changes, which seems reasonable.
2857            */
2858           int old_minibuf_height =
2859             XWINDOW(FRAME_MINIBUF_WINDOW(f))->pixel_height;
2860           int minibuf_height =
2861             f->init_finished && (old_minibuf_height % real_font_height) == 0 ?
2862             max(old_minibuf_height, real_font_height) :
2863             real_font_height;
2864           set_window_pixheight (FRAME_ROOT_WINDOW (f),
2865                                 /* - font_height for minibuffer */
2866                                 new_pixheight - minibuf_height, 0);
2867
2868           XWINDOW (FRAME_MINIBUF_WINDOW (f))->pixel_top =
2869             new_pixheight - minibuf_height + FRAME_TOP_BORDER_END (f);
2870
2871           set_window_pixheight (FRAME_MINIBUF_WINDOW (f), minibuf_height, 0);
2872         }
2873       else
2874         /* Frame has just one top-level window.  */
2875         set_window_pixheight (FRAME_ROOT_WINDOW (f), new_pixheight, 0);
2876
2877       FRAME_HEIGHT (f) = newheight;
2878       if (FRAME_TTY_P (f))
2879         f->pixheight = newheight;
2880     }
2881
2882   if (new_pixwidth)
2883     {
2884       XWINDOW (FRAME_ROOT_WINDOW (f))->pixel_left = FRAME_LEFT_BORDER_END (f);
2885       set_window_pixwidth (FRAME_ROOT_WINDOW (f), new_pixwidth, 0);
2886
2887       if (FRAME_HAS_MINIBUF_P (f))
2888         {
2889           XWINDOW (FRAME_MINIBUF_WINDOW (f))->pixel_left =
2890             FRAME_LEFT_BORDER_END (f);
2891           set_window_pixwidth (FRAME_MINIBUF_WINDOW (f), new_pixwidth, 0);
2892         }
2893
2894       FRAME_WIDTH (f) = newwidth;
2895       if (FRAME_TTY_P (f))
2896         f->pixwidth = newwidth;
2897     }
2898
2899   if (window_system_pixelated_geometry (frame))
2900     pixel_to_real_char_size (f, FRAME_PIXWIDTH (f), FRAME_PIXHEIGHT (f),
2901                              &FRAME_CHARWIDTH (f), &FRAME_CHARHEIGHT (f));
2902   else
2903     {
2904       FRAME_CHARWIDTH (f) = FRAME_WIDTH (f);
2905       FRAME_CHARHEIGHT (f) = FRAME_HEIGHT (f);
2906     }
2907
2908   MARK_FRAME_TOOLBARS_CHANGED (f);
2909   MARK_FRAME_CHANGED (f);
2910   f->echo_area_garbaged = 1;
2911 }
2912
2913 void
2914 change_frame_size (struct frame *f, int newheight, int newwidth, int delay)
2915 {
2916   /* sometimes we get passed a size that's too small (esp. when a
2917      client widget gets resized, since we have no control over this).
2918      So deal. */
2919   check_frame_size (f, &newheight, &newwidth);
2920
2921   if (delay || in_display || gc_in_progress)
2922     {
2923       MARK_FRAME_SIZE_CHANGED (f);
2924       f->new_width = newwidth;
2925       f->new_height = newheight;
2926       return;
2927     }
2928
2929   f->size_change_pending = 0;
2930   /* For TTY frames, it's like one, like all ...
2931      Can't have two TTY frames of different sizes on the same device. */
2932   if (FRAME_TTY_P (f))
2933     {
2934       Lisp_Object frmcons;
2935
2936       DEVICE_FRAME_LOOP (frmcons, XDEVICE (FRAME_DEVICE (f)))
2937         change_frame_size_1 (XFRAME (XCAR (frmcons)), newheight, newwidth);
2938     }
2939   else
2940     change_frame_size_1 (f, newheight, newwidth);
2941 }
2942
2943 \f
2944 void
2945 update_frame_title (struct frame *f)
2946 {
2947   struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
2948   Lisp_Object title_format;
2949   Lisp_Object icon_format;
2950   Bufbyte *title;
2951
2952   /* We don't change the title for the minibuffer unless the frame
2953      only has a minibuffer. */
2954   if (MINI_WINDOW_P (w) && !FRAME_MINIBUF_ONLY_P (f))
2955     return;
2956
2957   /* And we don't want dead buffers to blow up on us. */
2958   if (!BUFFER_LIVE_P (XBUFFER (w->buffer)))
2959     return;
2960
2961   title = NULL;
2962   title_format = symbol_value_in_buffer (Qframe_title_format,      w->buffer);
2963   icon_format  = symbol_value_in_buffer (Qframe_icon_title_format, w->buffer);
2964
2965   if (HAS_FRAMEMETH_P (f, set_title_from_bufbyte))
2966     {
2967       title = generate_formatted_string (w, title_format, Qnil,
2968                                          DEFAULT_INDEX, CURRENT_DISP);
2969       FRAMEMETH (f, set_title_from_bufbyte, (f, title));
2970     }
2971
2972   if (HAS_FRAMEMETH_P (f, set_icon_name_from_bufbyte))
2973     {
2974       if (!EQ (icon_format, title_format) || !title)
2975         {
2976           if (title)
2977             xfree (title);
2978
2979           title = generate_formatted_string (w, icon_format, Qnil,
2980                                              DEFAULT_INDEX, CURRENT_DISP);
2981         }
2982       FRAMEMETH (f, set_icon_name_from_bufbyte, (f, title));
2983     }
2984
2985   if (title)
2986     xfree (title);
2987 }
2988
2989 \f
2990 DEFUN ("set-frame-pointer", Fset_frame_pointer, 2, 2, 0, /*
2991 Set the mouse pointer of FRAME to the given pointer image instance.
2992 You should not call this function directly.  Instead, set one of
2993 the variables `text-pointer-glyph', `nontext-pointer-glyph',
2994 `modeline-pointer-glyph', `selection-pointer-glyph',
2995 `busy-pointer-glyph', or `toolbar-pointer-glyph'.
2996 */
2997        (frame, image_instance))
2998 {
2999   struct frame *f = decode_frame (frame);
3000   CHECK_POINTER_IMAGE_INSTANCE (image_instance);
3001   if (!EQ (f->pointer, image_instance))
3002     {
3003       f->pointer = image_instance;
3004       MAYBE_FRAMEMETH (f, set_frame_pointer, (f));
3005     }
3006   return Qnil;
3007 }
3008
3009 \f
3010 void
3011 update_frame_icon (struct frame *f)
3012 {
3013   if (f->icon_changed || f->windows_changed)
3014     {
3015       Lisp_Object frame;
3016       Lisp_Object new_icon;
3017
3018       XSETFRAME (frame, f);
3019       new_icon = glyph_image_instance (Vframe_icon_glyph, frame,
3020                                        ERROR_ME_WARN, 0);
3021       if (!EQ (new_icon, f->icon))
3022         {
3023           f->icon = new_icon;
3024           MAYBE_FRAMEMETH (f, set_frame_icon, (f));
3025         }
3026     }
3027
3028   f->icon_changed = 0;
3029 }
3030
3031 static void
3032 icon_glyph_changed (Lisp_Object glyph, Lisp_Object property,
3033                     Lisp_Object locale)
3034 {
3035   MARK_ICON_CHANGED;
3036 }
3037
3038 \f
3039 void
3040 syms_of_frame (void)
3041 {
3042   defsymbol (&Qdelete_frame_hook, "delete-frame-hook");
3043   defsymbol (&Qselect_frame_hook, "select-frame-hook");
3044   defsymbol (&Qdeselect_frame_hook, "deselect-frame-hook");
3045   defsymbol (&Qcreate_frame_hook, "create-frame-hook");
3046   defsymbol (&Qcustom_initialize_frame, "custom-initialize-frame");
3047   defsymbol (&Qmouse_enter_frame_hook, "mouse-enter-frame-hook");
3048   defsymbol (&Qmouse_leave_frame_hook, "mouse-leave-frame-hook");
3049   defsymbol (&Qmap_frame_hook, "map-frame-hook");
3050   defsymbol (&Qunmap_frame_hook, "unmap-frame-hook");
3051
3052   defsymbol (&Qframep, "framep");
3053   defsymbol (&Qframe_live_p, "frame-live-p");
3054   defsymbol (&Qframe_x_p, "frame-x-p");
3055   defsymbol (&Qframe_tty_p, "frame-tty-p");
3056   defsymbol (&Qdelete_frame, "delete-frame");
3057   defsymbol (&Qsynchronize_minibuffers, "synchronize-minibuffers");
3058   defsymbol (&Qbuffer_predicate, "buffer-predicate");
3059   defsymbol (&Qframe_being_created, "frame-being-created");
3060   defsymbol (&Qmake_initial_minibuffer_frame, "make-initial-minibuffer-frame");
3061
3062   defsymbol (&Qframe_title_format, "frame-title-format");
3063   defsymbol (&Qframe_icon_title_format, "frame-icon-title-format");
3064
3065   defsymbol (&Qhidden, "hidden");
3066   defsymbol (&Qvisible, "visible");
3067   defsymbol (&Qiconic, "iconic");
3068   defsymbol (&Qinvisible, "invisible");
3069   defsymbol (&Qvisible_iconic, "visible-iconic");
3070   defsymbol (&Qinvisible_iconic, "invisible-iconic");
3071   defsymbol (&Qnomini, "nomini");
3072   defsymbol (&Qvisible_nomini, "visible-nomini");
3073   defsymbol (&Qiconic_nomini, "iconic-nomini");
3074   defsymbol (&Qinvisible_nomini, "invisible-nomini");
3075   defsymbol (&Qvisible_iconic_nomini, "visible-iconic-nomini");
3076   defsymbol (&Qinvisible_iconic_nomini, "invisible-iconic-nomini");
3077
3078   defsymbol (&Qminibuffer, "minibuffer");
3079   defsymbol (&Qunsplittable, "unsplittable");
3080   defsymbol (&Qinternal_border_width, "internal-border-width");
3081   defsymbol (&Qtop_toolbar_shadow_color, "top-toolbar-shadow-color");
3082   defsymbol (&Qbottom_toolbar_shadow_color, "bottom-toolbar-shadow-color");
3083   defsymbol (&Qbackground_toolbar_color, "background-toolbar-color");
3084   defsymbol (&Qtop_toolbar_shadow_pixmap, "top-toolbar-shadow-pixmap");
3085   defsymbol (&Qbottom_toolbar_shadow_pixmap, "bottom-toolbar-shadow-pixmap");
3086   defsymbol (&Qtoolbar_shadow_thickness, "toolbar-shadow-thickness");
3087   defsymbol (&Qscrollbar_placement, "scrollbar-placement");
3088   defsymbol (&Qinter_line_space, "inter-line-space");
3089   /* Qiconic already in this function. */
3090   defsymbol (&Qvisual_bell, "visual-bell");
3091   defsymbol (&Qbell_volume, "bell-volume");
3092   defsymbol (&Qpointer_background, "pointer-background");
3093   defsymbol (&Qpointer_color, "pointer-color");
3094   defsymbol (&Qtext_pointer, "text-pointer");
3095   defsymbol (&Qspace_pointer, "space-pointer");
3096   defsymbol (&Qmodeline_pointer, "modeline-pointer");
3097   defsymbol (&Qgc_pointer, "gc-pointer");
3098   defsymbol (&Qinitially_unmapped, "initially-unmapped");
3099   defsymbol (&Quse_backing_store, "use-backing-store");
3100   defsymbol (&Qborder_color, "border-color");
3101   defsymbol (&Qborder_width, "border-width");
3102   /* Qwidth, Qheight, Qleft, Qtop in general.c */
3103   defsymbol (&Qset_specifier, "set-specifier");
3104   defsymbol (&Qset_glyph_image, "set-glyph-image");
3105   defsymbol (&Qset_face_property, "set-face-property");
3106   defsymbol (&Qface_property_instance, "face-property-instance");
3107   defsymbol (&Qframe_property_alias, "frame-property-alias");
3108
3109   DEFSUBR (Fmake_frame);
3110   DEFSUBR (Fframep);
3111   DEFSUBR (Fframe_live_p);
3112 #if 0 /* FSFmacs */
3113   DEFSUBR (Fignore_event);
3114 #endif
3115   DEFSUBR (Ffocus_frame);
3116   DEFSUBR (Fselect_frame);
3117   DEFSUBR (Fselected_frame);
3118   DEFSUBR (Factive_minibuffer_window);
3119   DEFSUBR (Flast_nonminibuf_frame);
3120   DEFSUBR (Fframe_root_window);
3121   DEFSUBR (Fframe_selected_window);
3122   DEFSUBR (Fset_frame_selected_window);
3123   DEFSUBR (Fframe_device);
3124   DEFSUBR (Fnext_frame);
3125   DEFSUBR (Fprevious_frame);
3126   DEFSUBR (Fdelete_frame);
3127   DEFSUBR (Fmouse_position);
3128   DEFSUBR (Fmouse_pixel_position);
3129   DEFSUBR (Fmouse_position_as_motion_event);
3130   DEFSUBR (Fset_mouse_position);
3131   DEFSUBR (Fset_mouse_pixel_position);
3132   DEFSUBR (Fmake_frame_visible);
3133   DEFSUBR (Fmake_frame_invisible);
3134   DEFSUBR (Ficonify_frame);
3135   DEFSUBR (Fdeiconify_frame);
3136   DEFSUBR (Fframe_visible_p);
3137   DEFSUBR (Fframe_totally_visible_p);
3138   DEFSUBR (Fframe_iconified_p);
3139   DEFSUBR (Fvisible_frame_list);
3140   DEFSUBR (Fraise_frame);
3141   DEFSUBR (Flower_frame);
3142   DEFSUBR (Fframe_property);
3143   DEFSUBR (Fframe_properties);
3144   DEFSUBR (Fset_frame_properties);
3145   DEFSUBR (Fframe_pixel_height);
3146   DEFSUBR (Fframe_pixel_width);
3147   DEFSUBR (Fframe_name);
3148   DEFSUBR (Fframe_modified_tick);
3149   DEFSUBR (Fset_frame_height);
3150   DEFSUBR (Fset_frame_width);
3151   DEFSUBR (Fset_frame_size);
3152   DEFSUBR (Fset_frame_position);
3153   DEFSUBR (Fset_frame_pointer);
3154 }
3155
3156 void
3157 vars_of_frame (void)
3158 {
3159   /* */
3160   Vframe_being_created = Qnil;
3161   staticpro (&Vframe_being_created);
3162
3163 #ifdef HAVE_CDE
3164   Fprovide (intern ("cde"));
3165 #endif
3166
3167 #ifdef HAVE_OFFIX_DND
3168   Fprovide (intern ("offix"));
3169 #endif
3170
3171 #if 0 /* FSFmacs stupidity */
3172   xxDEFVAR_LISP ("emacs-iconified", &Vemacs_iconified /*
3173 Non-nil if all of emacs is iconified and frame updates are not needed.
3174 */ );
3175   Vemacs_iconified = Qnil;
3176 #endif
3177
3178   DEFVAR_LISP ("select-frame-hook", &Vselect_frame_hook /*
3179 Function or functions to run just after a new frame is given the focus.
3180 Note that calling `select-frame' does not necessarily set the focus:
3181 The actual window-system focus will not be changed until the next time
3182 that XEmacs is waiting for an event, and even then, the window manager
3183 may refuse the focus-change request.
3184 */ );
3185   Vselect_frame_hook = Qnil;
3186
3187   DEFVAR_LISP ("deselect-frame-hook", &Vdeselect_frame_hook /*
3188 Function or functions to run just before a frame loses the focus.
3189 See `select-frame-hook'.
3190 */ );
3191   Vdeselect_frame_hook = Qnil;
3192
3193   DEFVAR_LISP ("delete-frame-hook", &Vdelete_frame_hook /*
3194 Function or functions to call when a frame is deleted.
3195 One argument, the about-to-be-deleted frame.
3196 */ );
3197   Vdelete_frame_hook = Qnil;
3198
3199   DEFVAR_LISP ("create-frame-hook", &Vcreate_frame_hook /*
3200 Function or functions to call when a frame is created.
3201 One argument, the newly-created frame.
3202 */ );
3203   Vcreate_frame_hook = Qnil;
3204
3205   DEFVAR_LISP ("mouse-enter-frame-hook", &Vmouse_enter_frame_hook /*
3206 Function or functions to call when the mouse enters a frame.
3207 One argument, the frame.
3208 Be careful not to make assumptions about the window manager's focus model.
3209 In most cases, the `deselect-frame-hook' is more appropriate.
3210 */ );
3211   Vmouse_enter_frame_hook = Qnil;
3212
3213   DEFVAR_LISP ("mouse-leave-frame-hook", &Vmouse_leave_frame_hook /*
3214 Function or functions to call when the mouse leaves a frame.
3215 One argument, the frame.
3216 Be careful not to make assumptions about the window manager's focus model.
3217 In most cases, the `select-frame-hook' is more appropriate.
3218 */ );
3219   Vmouse_leave_frame_hook = Qnil;
3220
3221   DEFVAR_LISP ("map-frame-hook", &Vmap_frame_hook /*
3222 Function or functions to call when a frame is mapped.
3223 One argument, the frame.
3224 */ );
3225   Vmap_frame_hook = Qnil;
3226
3227   DEFVAR_LISP ("unmap-frame-hook", &Vunmap_frame_hook /*
3228 Function or functions to call when a frame is unmapped.
3229 One argument, the frame.
3230 */ );
3231   Vunmap_frame_hook = Qnil;
3232
3233   DEFVAR_BOOL ("allow-deletion-of-last-visible-frame",
3234                &allow_deletion_of_last_visible_frame /*
3235 *Non-nil means to assume the force option to delete-frame.
3236 */ );
3237   allow_deletion_of_last_visible_frame = 0;
3238
3239   DEFVAR_LISP ("adjust-frame-function", &Vadjust_frame_function /*
3240 Function or constant controlling adjustment of frame.
3241 When scrollbars, toolbars, default font etc. change in frame, the frame
3242 needs to be adjusted. The adjustment is controlled by this variable.
3243 Legal values are:
3244   nil to keep character frame size unchanged when possible (resize)
3245   t   to keep pixel size unchanged (never resize)
3246   function symbol or lambda form. This function must return boolean
3247       value which is treated as above. Function is passed one parameter,
3248       the frame being adjusted. It function should not modify or delete
3249       the frame.
3250 */ );
3251   Vadjust_frame_function = Qnil;
3252
3253   DEFVAR_LISP ("mouse-motion-handler", &Vmouse_motion_handler /*
3254 Handler for motion events.  One arg, the event.
3255 For most applications, you should use `mode-motion-hook' instead of this.
3256 */ );
3257   Vmouse_motion_handler = Qnil;
3258
3259   DEFVAR_LISP ("synchronize-minibuffers",&Vsynchronize_minibuffers /*
3260 Set to t if all minibuffer windows are to be synchronized.
3261 This will cause echo area messages to appear in the minibuffers of all
3262 visible frames.
3263 */ );
3264   Vsynchronize_minibuffers = Qnil;
3265
3266   DEFVAR_LISP ("frame-title-format", &Vframe_title_format /*
3267 Controls the title of the X window corresponding to the selected frame.
3268 This is the same format as `modeline-format' with the exception that
3269 %- is ignored.
3270 */ );
3271   Vframe_title_format = Fpurecopy (build_string ("%S: %b"));
3272
3273   DEFVAR_LISP ("frame-icon-title-format", &Vframe_icon_title_format /*
3274 Controls the title of the icon corresponding to the selected frame.
3275 See also the variable `frame-title-format'.
3276 */ );
3277   Vframe_icon_title_format = Fpurecopy (build_string ("%b"));
3278
3279   DEFVAR_LISP ("default-frame-name", &Vdefault_frame_name /*
3280 The default name to assign to newly-created frames.
3281 This can be overridden by arguments to `make-frame'.
3282 This must be a string.
3283 */ );
3284 #ifndef INFODOCK
3285   Vdefault_frame_name = Fpurecopy (build_string ("emacs"));
3286 #else
3287   Vdefault_frame_name = Fpurecopy (build_string ("InfoDock"));
3288 #endif
3289
3290   DEFVAR_LISP ("default-frame-plist", &Vdefault_frame_plist /*
3291 Plist of default values for frame creation, other than the first one.
3292 These may be set in your init file, like this:
3293
3294   \(setq default-frame-plist '(width 80 height 55))
3295
3296 The properties may be in alist format for backward compatibility
3297 but you should not rely on this behavior.
3298
3299 These override values given in window system configuration data,
3300  including X Windows' defaults database.
3301
3302 Since the first X frame is created before loading your .emacs file,
3303 you must use the X resource database for that.
3304
3305 For values specific to the first Emacs frame, see `initial-frame-plist'.
3306 For values specific to the separate minibuffer frame, see
3307  `minibuffer-frame-plist'.
3308
3309 See also the variables `default-x-frame-plist' and
3310 `default-tty-frame-plist', which are like `default-frame-plist'
3311 except that they apply only to X or tty frames, respectively
3312 \(whereas `default-frame-plist' applies to all types of frames).
3313 */ );
3314   Vdefault_frame_plist = Qnil;
3315
3316   DEFVAR_LISP ("frame-icon-glyph", &Vframe_icon_glyph /*
3317 Icon glyph used to iconify a frame.
3318 */ );
3319 }
3320
3321 void
3322 complex_vars_of_frame (void)
3323 {
3324   Vframe_icon_glyph = allocate_glyph (GLYPH_ICON, icon_glyph_changed);
3325 }