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