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