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