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