2 Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
3 Copyright (C) 1996 Ben Wing.
5 This file is part of SXEmacs
7 SXEmacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 SXEmacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* Synched up with: Not in FSF. */
23 /* Written by Ben Wing. */
29 #include "TTY/console-tty.h" /* for Fconsole_tty_controlling process ins
30 suspend-console. Needs refactoring...*/
31 #include "events/events.h"
33 #include "redisplay.h"
37 Lisp_Object Vconsole_list, Vselected_console;
39 Lisp_Object Vcreate_console_hook, Vdelete_console_hook;
41 Lisp_Object Qconsolep, Qconsole_live_p;
42 Lisp_Object Qcreate_console_hook;
43 Lisp_Object Qdelete_console_hook;
45 Lisp_Object Qsuspend_hook;
46 Lisp_Object Qsuspend_resume_hook;
48 /* This structure holds the default values of the console-local
49 variables defined with DEFVAR_CONSOLE_LOCAL, that have special
50 slots in each console. The default value occupies the same slot
51 in this structure as an individual console's value occupies in
52 that console. Setting the default value also goes through the
53 list of consoles and stores into each console that does not say
54 it has a local value. */
55 Lisp_Object Vconsole_defaults;
56 static void *console_defaults_saved_slots;
58 /* This structure marks which slots in a console have corresponding
59 default values in console_defaults.
60 Each such slot has a nonzero value in this structure.
61 The value has only one nonzero bit.
63 When a console has its own local value for a slot,
64 the bit for that slot (found in the same slot in this structure)
65 is turned on in the console's local_var_flags slot.
67 If a slot in this structure is 0, then there is a DEFVAR_CONSOLE_LOCAL
68 for the slot, but there is no default value for it; the corresponding
69 slot in console_defaults is not used except to initialize newly-created
72 If a slot is -1, then there is a DEFVAR_CONSOLE_LOCAL for it
73 as well as a default value which is used to initialize newly-created
74 consoles and as a reset-value when local-vars are killed.
76 If a slot is -2, there is no DEFVAR_CONSOLE_LOCAL for it.
77 (The slot is always local, but there's no lisp variable for it.)
78 The default value is only used to initialize newly-creation consoles.
80 If a slot is -3, then there is no DEFVAR_CONSOLE_LOCAL for it but
81 there is a default which is used to initialize newly-creation
82 consoles and as a reset-value when local-vars are killed.
85 struct console console_local_flags;
87 /* This structure holds the names of symbols whose values may be
88 console-local. It is indexed and accessed in the same way as the above. */
89 static Lisp_Object Vconsole_local_symbols;
90 static void *console_local_symbols_saved_slots;
92 DEFINE_CONSOLE_TYPE(dead);
94 Lisp_Object Vconsole_type_list;
96 console_type_entry_dynarr *the_console_type_entry_dynarr;
98 static Lisp_Object mark_console(Lisp_Object obj)
100 struct console *con = XCONSOLE(obj);
102 #define MARKED_SLOT(x) mark_object (con->x)
103 #include "conslots.h"
106 /* Can be zero for Vconsole_defaults, Vconsole_local_symbols */
108 mark_object(con->conmeths->symbol);
109 MAYBE_CONMETH(con, mark_console, (con));
116 print_console(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
118 struct console *con = XCONSOLE(obj);
121 error("printing unreadable object #<console %s 0x%x>",
122 XSTRING_DATA(con->name), con->header.uid);
124 write_fmt_string(printcharfun, "#<%s-console",
125 (!CONSOLE_LIVE_P(con) ? "dead" : CONSOLE_TYPE_NAME(con)));
126 if (CONSOLE_LIVE_P(con) && !NILP(CONSOLE_CONNECTION(con))) {
127 write_c_string(" on ", printcharfun);
128 print_internal(CONSOLE_CONNECTION(con), printcharfun, 1);
130 write_fmt_str(printcharfun, " 0x%x>", con->header.uid);
133 DEFINE_LRECORD_IMPLEMENTATION("console", console,
134 mark_console, print_console, 0, 0, 0, 0,
137 static struct console *allocate_console(void)
140 struct console *con =
141 alloc_lcrecord_type(struct console, &lrecord_console);
144 copy_lcrecord(con, XCONSOLE(Vconsole_defaults));
146 XSETCONSOLE(console, con);
149 con->quit_char = 7; /* C-g */
150 con->command_builder = allocate_command_builder(console);
151 con->function_key_map = Fmake_sparse_keymap(Qnil);
157 struct console *decode_console(Lisp_Object console)
160 console = Fselected_console();
161 /* quietly accept devices and frames for the console arg */
162 if (DEVICEP(console) || FRAMEP(console))
163 console = DEVICE_CONSOLE(decode_device(console));
164 CHECK_LIVE_CONSOLE(console);
165 return XCONSOLE(console);
168 struct console_methods *decode_console_type(Lisp_Object type,
173 for (i = 0; i < Dynarr_length(the_console_type_entry_dynarr); i++)
175 (type, Dynarr_at(the_console_type_entry_dynarr, i).symbol))
176 return Dynarr_at(the_console_type_entry_dynarr,
179 maybe_signal_simple_error("Invalid console type", type, Qconsole, errb);
184 int valid_console_type_p(Lisp_Object type)
186 return decode_console_type(type, ERROR_ME_NOT) != 0;
189 DEFUN("valid-console-type-p", Fvalid_console_type_p, 1, 1, 0, /*
190 Return t if CONSOLE-TYPE is a valid console type.
191 Valid types are 'x, 'tty, and 'stream.
195 return valid_console_type_p(console_type) ? Qt : Qnil;
198 DEFUN("console-type-list", Fconsole_type_list, 0, 0, 0, /*
199 Return a list of valid console types.
203 return Fcopy_sequence(Vconsole_type_list);
206 DEFUN("cdfw-console", Fcdfw_console, 1, 1, 0, /*
207 Given a console, device, frame, or window, return the associated console.
208 Return nil otherwise.
212 return CDFW_CONSOLE(object);
215 DEFUN("selected-console", Fselected_console, 0, 0, 0, /*
216 Return the console which is currently active.
220 return Vselected_console;
223 /* Called from selected_device_1(), called from selected_frame_1(),
224 called from Fselect_window() */
225 void select_console_1(Lisp_Object console)
227 /* perhaps this should do something more complicated */
228 Vselected_console = console;
230 /* #### Schedule this to be removed in 19.14 */
231 #ifdef HAVE_X_WINDOWS
232 if (CONSOLE_X_P(XCONSOLE(console)))
237 if (CONSOLE_GTK_P(XCONSOLE(console)))
238 Vwindow_system = Qgtk;
241 Vwindow_system = Qnil;
244 DEFUN("select-console", Fselect_console, 1, 1, 0, /*
245 Select the console CONSOLE.
246 Subsequent editing commands apply to its selected device, selected frame,
247 and selected window. The selection of CONSOLE lasts until the next time
248 the user does something to select a different console, or until the next
249 time this function is called.
255 CHECK_LIVE_CONSOLE(console);
257 device = CONSOLE_SELECTED_DEVICE(XCONSOLE(console));
259 struct device *d = XDEVICE(device);
260 Lisp_Object frame = DEVICE_SELECTED_FRAME(d);
262 struct frame *f = XFRAME(frame);
263 Fselect_window(FRAME_SELECTED_WINDOW(f), Qnil);
265 error("Can't select console with no frames.");
267 error("Can't select a console with no devices");
271 void set_console_last_nonminibuf_frame(struct console *con, Lisp_Object frame)
273 con->last_nonminibuf_frame = frame;
276 DEFUN("consolep", Fconsolep, 1, 1, 0, /*
277 Return non-nil if OBJECT is a console.
281 return CONSOLEP(object) ? Qt : Qnil;
284 DEFUN("console-live-p", Fconsole_live_p, 1, 1, 0, /*
285 Return non-nil if OBJECT is a console that has not been deleted.
289 return CONSOLEP(object) && CONSOLE_LIVE_P(XCONSOLE(object)) ? Qt : Qnil;
292 DEFUN("console-type", Fconsole_type, 0, 1, 0, /*
293 Return the console type (e.g. `x' or `tty') of CONSOLE.
294 Value is `tty' for a tty console (a character-only terminal),
295 `x' for a console that is an X display,
296 `mswindows' for a console that is a Windows NT/95/97 connection,
297 `pc' for a console that is a direct-write MS-DOS connection (not yet
299 `stream' for a stream console (which acts like a stdio stream), and
300 `dead' for a deleted console.
304 /* don't call decode_console() because we want to allow for dead
307 console = Fselected_console();
308 CHECK_CONSOLE(console);
309 return CONSOLE_TYPE(XCONSOLE(console));
312 DEFUN("console-name", Fconsole_name, 0, 1, 0, /*
313 Return the name of CONSOLE.
317 return CONSOLE_NAME(decode_console(console));
320 DEFUN("console-connection", Fconsole_connection, 0, 1, 0, /*
321 Return the connection of the specified console.
322 CONSOLE defaults to the selected console if omitted.
326 return CONSOLE_CONNECTION(decode_console(console));
329 Lisp_Object make_console(struct console * con)
332 XSETCONSOLE(console, con);
337 semi_canonicalize_console_connection(struct console_methods *meths,
338 Lisp_Object name, Error_behavior errb)
340 if (HAS_CONTYPE_METH_P(meths, semi_canonicalize_console_connection))
341 return CONTYPE_METH(meths, semi_canonicalize_console_connection,
344 return CONTYPE_METH_OR_GIVEN(meths,
345 canonicalize_console_connection,
350 canonicalize_console_connection(struct console_methods *meths,
351 Lisp_Object name, Error_behavior errb)
353 if (HAS_CONTYPE_METH_P(meths, canonicalize_console_connection))
354 return CONTYPE_METH(meths, canonicalize_console_connection,
357 return CONTYPE_METH_OR_GIVEN(meths,
358 semi_canonicalize_console_connection,
363 find_console_of_type(struct console_methods *meths, Lisp_Object canon)
367 CONSOLE_LOOP(concons) {
368 Lisp_Object console = XCAR(concons);
370 if (EQ(CONMETH_TYPE(meths), CONSOLE_TYPE(XCONSOLE(console)))
372 internal_equal(CONSOLE_CANON_CONNECTION(XCONSOLE(console)),
380 DEFUN("find-console", Ffind_console, 1, 2, 0, /*
381 Look for an existing console attached to connection CONNECTION.
382 Return the console if found; otherwise, return nil.
384 If TYPE is specified, only return consoles of that type; otherwise,
385 return consoles of any type. (It is possible, although unlikely,
386 that two consoles of different types could have the same connection
387 name; in such a case, the first console found is returned.)
391 Lisp_Object canon = Qnil;
397 struct console_methods *conmeths =
398 decode_console_type(type, ERROR_ME);
400 canonicalize_console_connection(conmeths, connection,
403 RETURN_UNGCPRO(Qnil);
405 RETURN_UNGCPRO(find_console_of_type(conmeths, canon));
409 for (i = 0; i < Dynarr_length(the_console_type_entry_dynarr);
411 struct console_methods *conmeths =
412 Dynarr_at(the_console_type_entry_dynarr, i).meths;
414 canonicalize_console_connection(conmeths,
417 if (!UNBOUNDP(canon)) {
418 Lisp_Object console =
419 find_console_of_type(conmeths, canon);
421 RETURN_UNGCPRO(console);
425 RETURN_UNGCPRO(Qnil);
429 DEFUN("get-console", Fget_console, 1, 2, 0, /*
430 Look for an existing console attached to connection CONNECTION.
431 Return the console if found; otherwise, signal an error.
433 If TYPE is specified, only return consoles of that type; otherwise,
434 return consoles of any type. (It is possible, although unlikely,
435 that two consoles of different types could have the same connection
436 name; in such a case, the first console found is returned.)
440 Lisp_Object console = Ffind_console(connection, type);
443 signal_simple_error("No such console", connection);
445 signal_simple_error_2("No such console", type,
452 create_console(Lisp_Object name, Lisp_Object type, Lisp_Object connection,
455 /* This function can GC */
460 console = Ffind_console(connection, type);
464 con = allocate_console();
465 XSETCONSOLE(console, con);
469 con->conmeths = decode_console_type(type, ERROR_ME);
471 CONSOLE_NAME(con) = name;
472 CONSOLE_CONNECTION(con) =
473 semi_canonicalize_console_connection(con->conmeths, connection,
475 CONSOLE_CANON_CONNECTION(con) =
476 canonicalize_console_connection(con->conmeths, connection,
479 MAYBE_CONMETH(con, init_console, (con, props));
481 /* Do it this way so that the console list is in order of creation */
482 Vconsole_list = nconc2(Vconsole_list, Fcons(console, Qnil));
484 if (CONMETH_OR_GIVEN(con, initially_selected_for_input, (con), 0))
485 event_stream_select_console(con);
492 add_entry_to_console_type_list(Lisp_Object symbol,
493 struct console_methods *meths)
495 struct console_type_entry entry;
497 entry.symbol = symbol;
499 Dynarr_add(the_console_type_entry_dynarr, entry);
500 Vconsole_type_list = Fcons(symbol, Vconsole_type_list);
503 /* find a console other than the selected one. Prefer non-stream
504 consoles over stream consoles. */
506 static Lisp_Object find_other_console(Lisp_Object console)
510 /* look for a non-stream console */
511 CONSOLE_LOOP(concons) {
512 Lisp_Object con = XCAR(concons);
513 if (!CONSOLE_STREAM_P(XCONSOLE(con))
515 && !NILP(CONSOLE_SELECTED_DEVICE(XCONSOLE(con)))
516 && !NILP(DEVICE_SELECTED_FRAME
517 (XDEVICE(CONSOLE_SELECTED_DEVICE(XCONSOLE(con))))))
521 return XCAR(concons);
523 /* OK, now look for a stream console */
524 CONSOLE_LOOP(concons) {
525 Lisp_Object con = XCAR(concons);
526 if (!EQ(con, console)
527 && !NILP(CONSOLE_SELECTED_DEVICE(XCONSOLE(con)))
528 && !NILP(DEVICE_SELECTED_FRAME
529 (XDEVICE(CONSOLE_SELECTED_DEVICE(XCONSOLE(con))))))
533 return XCAR(concons);
535 /* Sorry, there ain't none */
540 find_nonminibuffer_frame_not_on_console_predicate(Lisp_Object frame,
545 VOID_TO_LISP(console, closure);
546 if (FRAME_MINIBUF_ONLY_P(XFRAME(frame)))
548 if (EQ(console, FRAME_CONSOLE(XFRAME(frame))))
553 static Lisp_Object find_nonminibuffer_frame_not_on_console(Lisp_Object console)
556 find_some_frame(find_nonminibuffer_frame_not_on_console_predicate,
557 LISP_TO_VOID(console));
560 /* Delete console CON.
562 If FORCE is non-zero, allow deletion of the only frame.
564 If CALLED_FROM_KILL_EMACS is non-zero, then, if
565 deleting the last console, just delete it,
566 instead of calling `save-buffers-kill-emacs'.
568 If FROM_IO_ERROR is non-zero, then the console is gone due
569 to an I/O error. This affects what happens if we exit
570 (we do an emergency exit instead of `save-buffers-kill-emacs'.)
574 delete_console_internal(struct console *con, int force,
575 int called_from_kill_emacs, int from_io_error)
577 /* This function can GC */
581 /* OK to delete an already-deleted console. */
582 if (!CONSOLE_LIVE_P(con))
585 XSETCONSOLE(console, con);
588 if (!called_from_kill_emacs) {
591 if ((XINT(Flength(Vconsole_list)) == 1)
592 /* if we just created the console, it might not be listed,
594 && !NILP(memq_no_quit(console, Vconsole_list)))
596 /* If there aren't any nonminibuffer frames that would
597 be left, then exit. */
598 else if (NILP(find_nonminibuffer_frame_not_on_console(console)))
603 error("Attempt to delete the only frame");
604 else if (from_io_error) {
605 /* Mayday mayday! We're going down! */
606 stderr_out(" Autosaving and exiting...\n");
607 Vwindow_system = Qnil; /* let it lie! */
608 preparing_for_armageddon = 1;
609 Fkill_emacs(make_int(70));
611 call0(Qsave_buffers_kill_emacs);
613 /* If we get here, the user said they didn't want
614 to exit, so don't. */
620 /* Breathe a sigh of relief. We're still alive. */
623 Lisp_Object frmcons, devcons;
625 /* First delete all frames without their own minibuffers,
626 to avoid errors coming from attempting to delete a frame
627 that is a surrogate for another frame.
629 We don't set "called_from_delete_console" because we want the
630 device to go ahead and get deleted if we delete the last frame
631 on a device. We won't run into trouble here because for any
632 frame without a minibuffer, there has to be another one on
633 the same console with a minibuffer, and we're not deleting that,
634 so delete_console_internal() won't get recursively called.
636 WRONG! With surrogate minibuffers this isn't true. Frames
637 with only a minibuffer are not enough to prevent
638 delete_frame_internal from triggering a device deletion. */
639 CONSOLE_FRAME_LOOP_NO_BREAK(frmcons, devcons, con) {
640 struct frame *f = XFRAME(XCAR(frmcons));
641 /* delete_frame_internal() might do anything such as run hooks,
643 if (FRAME_LIVE_P(f) && !FRAME_HAS_MINIBUF_P(f))
644 delete_frame_internal(f, 1, 1, from_io_error);
646 if (!CONSOLE_LIVE_P(con)) { /* make sure the delete-*-hook didn't
647 go ahead and delete anything */
653 CONSOLE_DEVICE_LOOP(devcons, con) {
654 struct device *d = XDEVICE(XCAR(devcons));
655 /* delete_device_internal() might do anything such as run hooks,
657 if (DEVICE_LIVE_P(d))
658 delete_device_internal(d, 1, 1, from_io_error);
659 if (!CONSOLE_LIVE_P(con)) { /* make sure the delete-*-hook didn't
660 go ahead and delete anything */
667 CONSOLE_SELECTED_DEVICE(con) = Qnil;
669 /* try to select another console */
671 if (EQ(console, Fselected_console())) {
672 Lisp_Object other_dev = find_other_console(console);
673 if (!NILP(other_dev))
674 Fselect_console(other_dev);
677 Vselected_console = Qnil;
678 Vwindow_system = Qnil;
682 if (con->input_enabled)
683 event_stream_unselect_console(con);
685 MAYBE_CONMETH(con, delete_console, (con));
687 Vconsole_list = delq_no_quit(console, Vconsole_list);
688 RESET_CHANGED_SET_FLAGS;
689 con->conmeths = dead_console_methods;
694 void io_error_delete_console(Lisp_Object console)
696 delete_console_internal(XCONSOLE(console), 1, 0, 1);
699 DEFUN("delete-console", Fdelete_console, 1, 2, 0, /*
700 Delete CONSOLE, permanently eliminating it from use.
701 Normally, you cannot delete the last non-minibuffer-only frame (you must
702 use `save-buffers-kill-emacs' or `kill-emacs'). However, if optional
703 second argument FORCE is non-nil, you can delete the last frame. (This
704 will automatically call `save-buffers-kill-emacs'.)
708 CHECK_CONSOLE(console);
709 delete_console_internal(XCONSOLE(console), !NILP(force), 0, 0);
713 DEFUN("console-list", Fconsole_list, 0, 0, 0, /*
714 Return a list of all consoles.
718 return Fcopy_sequence(Vconsole_list);
721 DEFUN("console-device-list", Fconsole_device_list, 0, 1, 0, /*
722 Return a list of all devices on CONSOLE.
723 If CONSOLE is nil, the selected console is used.
727 return Fcopy_sequence(CONSOLE_DEVICE_LIST(decode_console(console)));
730 DEFUN("console-enable-input", Fconsole_enable_input, 1, 1, 0, /*
731 Enable input on console CONSOLE.
735 struct console *con = decode_console(console);
736 if (!con->input_enabled)
737 event_stream_select_console(con);
741 DEFUN("console-disable-input", Fconsole_disable_input, 1, 1, 0, /*
742 Disable input on console CONSOLE.
746 struct console *con = decode_console(console);