1 /* Keyboard input; editor command loop.
2 Copyright (C) 1985, 1986, 1987, 1988, 1990 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /*** For version 19, can simplify this by making interrupt_input 1 on VMS. */
22 /* This must precede sys/signal.h on certain machines. */
23 #include <sys/types.h>
24 /* Allow config.h to undefine symbols found here. */
32 #include "termhooks.h"
43 /* Get FIONREAD, if it is available. */
49 #include <sys/ioctl.h>
53 #include "emacssignal.h"
55 /* Allow m- file to inhibit use of FIONREAD. */
56 #ifdef BROKEN_FIONREAD
60 /* Make all keyboard buffers much bigger when using X windows. */
62 #define BUFFER_SIZE_FACTOR 16
64 #define BUFFER_SIZE_FACTOR 1
67 /* Following definition copied from eval.c */
71 struct backtrace
*next
;
72 Lisp_Object
*function
;
73 Lisp_Object
*args
; /* Points to vector of args. */
74 int nargs
; /* length of vector */
75 /* if nargs is UNEVALLED, args points to slot holding list of unevalled args */
79 /* Non-nil disable property on a command means
80 do not execute it; call disabled-command-hook's value instead. */
81 Lisp_Object Qdisabled
, Vdisabled_command_hook
;
83 int recent_keys_index
; /* Index for storing next element into recent_keys */
84 int total_keys
; /* Total number of elements stored into recent_keys */
85 char recent_keys
[100]; /* Holds last 100 keystrokes */
87 /* Buffer holding the key that invoked the current command. */
88 unsigned char *this_command_keys
;
89 int this_command_key_count
; /* Size in use. */
90 int this_command_keys_size
; /* Size allocated. */
92 extern struct backtrace
*backtrace_list
;
94 static jmp_buf getcjmp
; /* for longjmp to where kbd input is being done. */
96 int waiting_for_input
; /* True while doing kbd input */
98 /* True while displaying for echoing. Delays C-g throwing. */
101 int immediate_quit
; /* Nonzero means C-G should cause immediate error-signal. */
103 int help_char
; /* Character to recognize as the help char. */
105 Lisp_Object Vhelp_form
; /* Form to execute when help char is typed. */
107 /* Character that causes a quit. Normally C-g. */
111 extern Lisp_Object global_map
;
113 /* Current depth in recursive edits. */
115 int command_loop_level
;
117 /* Last input character read as a command. */
119 int last_command_char
;
121 /* Last input character read for any purpose. */
125 /* If not -1, a character to be read as the next command input */
127 int unread_command_char
;
129 /* Char to use as prefix when a meta character is typed in.
130 This is bound on entry to minibuffer in case Esc is changed there. */
132 int meta_prefix_char
;
134 /* Total number of times read_command_char has returned. */
138 /* Auto-save automatically when this many characters have been typed
139 since the last time. */
141 static int auto_save_interval
;
143 /* Value of num_input_chars as of last auto save. */
147 /* Last command executed by the editor command loop, not counting
148 commands that set the prefix argument. */
150 Lisp_Object last_command
;
152 /* The command being executed by the command loop.
153 Commands may set this, and the value set will be copied into last_command
154 instead of the actual command. */
155 Lisp_Object this_command
;
157 Lisp_Object Qself_insert_command
;
158 Lisp_Object Qforward_char
;
159 Lisp_Object Qbackward_char
;
161 /* read_key_sequence stores here the command definition of the
162 key sequence that it reads. */
163 Lisp_Object read_key_sequence_cmd
;
165 /* Form to evaluate (if non-nil) when Emacs is started */
166 Lisp_Object Vtop_level
;
168 /* User-supplied string to translate input characters through */
169 Lisp_Object Vkeyboard_translate_table
;
171 FILE *dribble
; /* File in which we write all commands we read */
173 /* Nonzero if input is available */
176 /* Nonzero if should obey 0200 bit in input chars as "Meta" */
179 extern char *pending_malloc_warning
;
181 /* Buffer for pre-read keyboard input */
182 unsigned char kbd_buffer
[256 * BUFFER_SIZE_FACTOR
];
184 /* Number of characters available in kbd_buffer. */
187 /* Pointer to next available character in kbd_buffer. */
188 unsigned char *kbd_ptr
;
190 /* Address (if not 0) of word to zero out
191 if a SIGIO interrupt happens */
192 long *input_available_clear_word
;
194 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
195 Default is 1 if INTERRUPT_INPUT is defined. */
199 /* Nonzero while interrupts are temporarily deferred during redisplay. */
201 int interrupts_deferred
;
203 /* nonzero means use ^S/^Q for flow control. */
208 #define sigfree() sigsetmask (SIGEMPTYMASK)
209 #define sigholdx(sig) sigsetmask (sigmask (sig))
210 #define sigblockx(sig) sigblock (sigmask (sig))
211 #define sigunblockx(sig) sigblock (SIGEMPTYMASK)
212 #define sigpausex(sig) sigpause (0)
213 #endif /* not BSD4_1 */
216 #define SIGIO SIGTINT
217 /* sigfree and sigholdx are in sysdep.c */
218 #define sigblockx(sig) sighold (sig)
219 #define sigunblockx(sig) sigrelse (sig)
220 #define sigpausex(sig) sigpause (sig)
223 /* We are unable to use interrupts if FIONREAD is not available,
224 so flush SIGIO so we won't try. */
231 /* If we support X Windows, and won't get an interrupt when input
232 arrives from the server, poll periodically so we can detect C-g. */
233 #ifdef HAVE_X_WINDOWS
235 #define POLL_FOR_INPUT
239 /* Function for init_keyboard to call with no args (if nonzero). */
240 void (*keyboard_init_hook
) ();
242 static void read_avail_input ();
243 static void get_input_pending ();
245 /* Non-zero tells input_available_signal to call read_socket_hook
246 even if FIONREAD returns zero. */
247 static int force_input
;
249 static int echo_keystrokes
; /* > 0 if we are to echo keystrokes */
251 /* Nonzero means echo each character as typed. */
252 static int immediate_echo
;
254 #define min(a,b) ((a)<(b)?(a):(b))
255 #define max(a,b) ((a)>(b)?(a):(b))
257 static char echobuf
[100];
258 static char *echoptr
;
260 /* Install the string STR as the beginning of the string of echoing,
261 so that it serves as a prompt for the next character.
262 Also start echoing. */
267 int len
= strlen (str
);
268 if (len
> sizeof echobuf
- 4)
269 len
= sizeof echobuf
- 4;
270 bcopy (str
, echobuf
, len
+ 1);
271 echoptr
= echobuf
+ len
;
276 /* Add the character C to the echo string,
277 if echoing is going on. */
282 extern char *push_key_description ();
288 if (ptr
- echobuf
> sizeof echobuf
- 6)
291 if (echoptr
!= echobuf
)
294 ptr
= push_key_description (c
, ptr
);
295 if (echoptr
== echobuf
&& c
== help_char
)
297 strcpy (ptr
, " (Type ? for further options)");
308 /* Temporarily add a dash to the end of the echo string,
309 so that it serves as a mini-prompt for the very next character. */
313 if (!immediate_echo
&& echoptr
== echobuf
)
316 /* Put a dash at the end of the buffer temporarily,
317 but make it go away when the next character is added. */
324 /* Display the current echo string, and begin echoing if not already
334 for (i
= 0; i
< this_command_key_count
; i
++)
335 echo_char (this_command_keys
[i
]);
343 if (waiting_for_input
&& !NULL (Vquit_flag
))
344 quit_throw_to_read_command_char ();
347 /* Turn off echoing, for the start of a new command. */
355 /* When an auto-save happens, record the "time", and don't do again soon. */
358 last_auto_save
= num_input_chars
;
361 Lisp_Object
recursive_edit_unwind (), command_loop ();
363 DEFUN ("recursive-edit", Frecursive_edit
, Srecursive_edit
, 0, 0, "",
364 "Invoke the editor command loop recursively.\n\
365 Do (throw 'exit nil) within the command loop to make this function return,\n\
366 or (throw 'exit t) to make this function signal an error.\n\
367 This function is called by the editor initialization\n\
371 int count
= specpdl_ptr
- specpdl
;
373 command_loop_level
++;
374 update_mode_lines
= 1;
376 record_unwind_protect (recursive_edit_unwind
,
377 (current_buffer
!= XBUFFER (XWINDOW (selected_window
)->buffer
)
390 int count
= specpdl_ptr
- specpdl
;
393 if (command_loop_level
> 0)
395 specbind (Qstandard_output
, Qt
);
396 specbind (Qstandard_input
, Qt
);
399 val
= command_loop ();
401 Fsignal (Qquit
, Qnil
);
408 recursive_edit_unwind (buffer
)
412 Fset_buffer (buffer
);
413 command_loop_level
--;
414 update_mode_lines
= 1;
422 Lisp_Object errmsg
, tail
, errname
, file_error
;
428 Vstandard_output
= Qt
;
429 Vstandard_input
= Qt
;
430 Vexecuting_macro
= Qnil
;
431 echo_area_contents
= 0;
436 errname
= Fcar (data
);
438 if (EQ (errname
, Qerror
))
441 if (!CONSP (data
)) data
= Qnil
;
442 errmsg
= Fcar (data
);
447 errmsg
= Fget (errname
, Qerror_message
);
448 file_error
= Fmemq (Qfile_error
,
449 Fget (errname
, Qerror_conditions
));
452 /* Print an error message including the data items.
453 This is done by printing it into a scratch buffer
454 and then making a copy of the text in the buffer. */
456 if (!CONSP (data
)) data
= Qnil
;
460 /* For file-error, make error message by concatenating
461 all the data items. They are all strings. */
462 if (!NULL (file_error
))
463 errmsg
= XCONS (tail
)->car
, tail
= XCONS (tail
)->cdr
;
465 if (XTYPE (errmsg
) == Lisp_String
)
468 write_string_1 ("peculiar error", -1, Qt
);
470 for (i
= 0; CONSP (tail
); tail
= Fcdr (tail
), i
++)
472 write_string_1 (i
? ", " : ": ", 2, Qt
);
473 if (!NULL (file_error
))
474 Fprinc (Fcar (tail
), Qt
);
476 Fprin1 (Fcar (tail
), Qt
);
480 /* In -batch mode, force out the error message and newlines after it
485 Fkill_emacs (make_number (-1));
490 Vinhibit_quit
= Qnil
;
491 return make_number (0);
494 Lisp_Object
command_loop_1 ();
495 Lisp_Object
command_loop_2 ();
496 Lisp_Object
cmd_error ();
497 Lisp_Object
top_level_1 ();
499 /* Entry to editor-command-loop.
500 This level has the catches for exiting/returning to editor command loop.
501 It returns nil to exit recursive edit, t to abort it. */
506 if (command_loop_level
> 0 || minibuf_level
> 0)
508 return internal_catch (Qexit
, command_loop_2
, Qnil
);
513 internal_catch (Qtop_level
, top_level_1
, Qnil
);
514 internal_catch (Qtop_level
, command_loop_2
, Qnil
);
515 /* End of file in -batch run causes exit here. */
521 /* Here we catch errors in execution of commands within the
522 editing loop, and reenter the editing loop.
523 When there is an error, cmd_error runs and returns a non-nil
524 value to us. A value of nil means that cmd_loop_1 itself
525 returned due to end of file (or end of kbd macro). */
530 register Lisp_Object val
;
532 val
= internal_condition_case (command_loop_1
, Qerror
, cmd_error
);
540 return Feval (Vtop_level
);
546 /* On entry to the outer level, run the startup file */
547 if (!NULL (Vtop_level
))
548 internal_condition_case (top_level_2
, Qerror
, cmd_error
);
549 else if (!NULL (Vpurify_flag
))
550 message ("Bare impure Emacs (standard Lisp code not loaded)");
552 message ("Bare Emacs (standard Lisp code not loaded)");
556 DEFUN ("top-level", Ftop_level
, Stop_level
, 0, 0, "",
557 "Exit all recursive editing levels.")
560 Fthrow (Qtop_level
, Qnil
);
563 DEFUN ("exit-recursive-edit", Fexit_recursive_edit
, Sexit_recursive_edit
, 0, 0, "",
564 "Exit from the innermost recursive edit or minibuffer.")
567 if (command_loop_level
> 0 || minibuf_level
> 0)
568 Fthrow (Qexit
, Qnil
);
569 error ("No recursive edit is in progress");
572 DEFUN ("abort-recursive-edit", Fabort_recursive_edit
, Sabort_recursive_edit
, 0, 0, "",
573 "Abort the command that requested this recursive edit or minibuffer input.")
576 if (command_loop_level
> 0 || minibuf_level
> 0)
578 error ("No recursive edit is in progress");
581 /* This is the actual command reading loop,
582 sans error-handling encapsulation */
584 Lisp_Object
Fcommand_execute ();
598 waiting_for_input
= 0;
601 /* Don't clear out last_command at the beginning of a macro. */
602 if (NULL (Vexecuting_macro
)
603 || XTYPE (Vexecuting_macro
) != Lisp_String
)
607 this_command_key_count
= 0;
611 /* Install chars successfully executed in kbd macro */
612 if (defining_kbd_macro
&& NULL (Vprefix_arg
))
613 finalize_kbd_macro_chars ();
615 /* Make sure current window's buffer is selected. */
617 if (XBUFFER (XWINDOW (selected_window
)->buffer
) != current_buffer
)
618 set_buffer_internal (XBUFFER (XWINDOW (selected_window
)->buffer
));
620 /* Display any malloc warning that just came out.
621 Use while because displaying one warning can cause another. */
622 while (pending_malloc_warning
)
623 display_malloc_warning ();
627 /* If minibuffer on and echo area in use,
628 wait 2 sec and redraw minibufer. */
630 if (minibuf_level
&& echo_area_contents
)
632 int count
= specpdl_ptr
- specpdl
;
633 specbind (Qinhibit_quit
, Qt
);
634 Fsit_for (make_number (2), Qnil
);
637 echo_area_contents
= 0;
639 if (!NULL (Vquit_flag
))
642 unread_command_char
= quit_char
;
648 /* If prev. command was directly displayed, we don't need
649 redisplay. Try shortcut for reading single-char key sequence. */
651 i
= fast_read_one_key (keybuf
);
653 /* Shortcut not applicable or found a prefix key.
654 Take full precautions and read key sequence the hard way. */
658 alloca (0); /* Cause a garbage collection now */
659 /* Since we can free the most stuff here. */
660 #endif /* C_ALLOCA */
662 /* Read next key sequence; i gets its length. */
664 i
= read_key_sequence (keybuf
, sizeof keybuf
, 0,
665 no_redisplay
&& buffer_shared
<= 1);
668 /* Now we have read a key sequence of length I,
669 or else I is 0 and we found end of file. */
671 if (i
== 0) /* End of file -- happens only in */
672 return Qnil
; /* a kbd macro, at the end */
674 last_command_char
= keybuf
[i
- 1];
676 cmd
= read_key_sequence_cmd
;
677 if (!NULL (Vexecuting_macro
))
679 if (!NULL (Vquit_flag
))
681 Vexecuting_macro
= Qt
;
682 QUIT
; /* Make some noise. */
683 /* Will return since macro now empty. */
687 /* Do redisplay processing after this command except in special
688 cases identified below that set no_redisplay to 1. */
691 /* Execute the command. */
695 /* nil means key is undefined. */
697 defining_kbd_macro
= 0;
704 if (NULL (Vprefix_arg
) && ! no_direct
)
706 if (EQ (cmd
, Qforward_char
) && point
< ZV
)
708 lose
= FETCH_CHAR (point
);
710 if (lose
>= ' ' && lose
< 0177
711 && (XFASTINT (XWINDOW (selected_window
)->last_modified
)
713 && (XFASTINT (XWINDOW (selected_window
)->last_point
)
715 && !windows_or_buffers_changed
716 && EQ (current_buffer
->selective_display
, Qnil
)
717 && !detect_input_pending ()
718 && NULL (Vexecuting_macro
))
719 no_redisplay
= direct_output_forward_char (1);
722 else if (EQ (cmd
, Qbackward_char
) && point
> BEGV
)
725 lose
= FETCH_CHAR (point
);
726 if (lose
>= ' ' && lose
< 0177
727 && (XFASTINT (XWINDOW (selected_window
)->last_modified
)
729 && (XFASTINT (XWINDOW (selected_window
)->last_point
)
731 && !windows_or_buffers_changed
732 && EQ (current_buffer
->selective_display
, Qnil
)
733 && !detect_input_pending ()
734 && NULL (Vexecuting_macro
))
735 no_redisplay
= direct_output_forward_char (-1);
738 else if (EQ (cmd
, Qself_insert_command
))
740 if (NULL (Vexecuting_macro
) &&
741 !EQ (minibuf_window
, selected_window
))
743 if (!nonundocount
|| nonundocount
>= 20)
750 lose
= (XFASTINT (XWINDOW (selected_window
)->last_modified
)
752 || (XFASTINT (XWINDOW (selected_window
)->last_point
)
754 || MODIFF
<= current_buffer
->save_modified
755 || windows_or_buffers_changed
756 || !EQ (current_buffer
->selective_display
, Qnil
)
757 || detect_input_pending ()
758 || !NULL (Vexecuting_macro
);
759 if (self_insert_internal (last_command_char
, 0))
765 && (point
== ZV
|| FETCH_CHAR (point
) == '\n')
766 && last_command_char
>= ' '
767 && last_command_char
< 0177)
769 = direct_output_for_insert (last_command_char
);
774 /* Here for a command that isn't executed directly */
777 if (NULL (Vprefix_arg
))
779 Fcommand_execute (cmd
, Qnil
);
781 /* This label logically belongs inside the above group,
782 but moving it is said to avoid a compiler bug on SCO V.3.2v2. */
785 if (NULL (Vprefix_arg
))
787 last_command
= this_command
;
788 this_command_key_count
= 0;
794 /* Input of single characters from keyboard */
796 Lisp_Object
print_help ();
801 /* Alarm interrupt calls this and requests echoing at earliest safe time. */
804 int old_errno
= errno
;
806 /* Note: no need to reestablish handler on USG systems
807 because it is established, if approriate, each time an alarm is requested. */
810 extern int select_alarmed
;
811 if (select_alarmed
== 0)
836 /* Nonzero means polling for input is temporarily suppresed. */
837 int poll_suppress_count
;
839 /* Number of seconds between polling for input. */
842 #ifdef POLL_FOR_INPUT
843 int polling_for_input
;
845 /* Handle an alarm once each second and read pending input
846 so as to handle a C-g if it comces in. */
852 if (!waiting_for_input
)
853 read_avail_input (&junk
);
854 signal (SIGALRM
, input_poll_signal
);
855 alarm (polling_period
);
860 /* Begin signals to poll for input, if they are appropriate.
861 This function is called unconditionally from various places. */
865 #ifdef POLL_FOR_INPUT
866 if (read_socket_hook
)
868 poll_suppress_count
--;
869 if (poll_suppress_count
== 0)
871 signal (SIGALRM
, input_poll_signal
);
872 polling_for_input
= 1;
873 alarm (polling_period
);
879 /* Turn off polling. */
883 #ifdef POLL_FOR_INPUT
884 if (read_socket_hook
)
886 if (poll_suppress_count
== 0)
888 polling_for_input
= 0;
891 poll_suppress_count
++;
896 /* read a character from the keyboard; call the redisplay if needed */
897 /* commandflag 0 means do not do auto-saving, but do do redisplay.
898 -1 means do not do redisplay, but do do autosaving.
901 read_command_char (commandflag
)
909 extern request_echo ();
911 if (unread_command_char
>= 0)
913 c
= unread_command_char
;
914 unread_command_char
= -1;
915 if (this_command_key_count
== 0)
920 if (!NULL (Vexecuting_macro
))
922 if (XTYPE (Vexecuting_macro
) != Lisp_String
923 || XSTRING (Vexecuting_macro
)->size
<= executing_macro_index
)
926 c
= XSTRING (Vexecuting_macro
)->data
[executing_macro_index
++];
930 /* Save outer setjmp data, in case called recursively. */
931 bcopy (getcjmp
, save_jump
, sizeof getcjmp
);
935 if (commandflag
>= 0 && !detect_input_pending ())
939 && auto_save_interval
> 0
940 && num_input_chars
- last_auto_save
> max (auto_save_interval
, 20)
941 && !detect_input_pending ())
942 Fdo_auto_save (Qnil
);
944 if (_setjmp (getcjmp
))
947 waiting_for_input
= 0;
948 input_available_clear_word
= 0;
953 /* Message turns off echoing unless more keystrokes turn it on again. */
954 if (echo_area_contents
&& *echo_area_contents
&& echo_area_contents
!= echobuf
)
957 /* If already echoing, continue, and prompt. */
960 /* If in middle of key sequence and minibuffer not active,
961 start echoing if enough time elapses. */
962 if (minibuf_level
== 0 && !immediate_echo
&& this_command_key_count
> 0
963 && echo_keystrokes
> 0
964 && (echo_area_contents
== 0 || *echo_area_contents
== 0))
966 /* Else start echoing if user waits more than `alarmtime' seconds. */
967 /* This interrupt either calls echo right away
968 or sets echo_flag, which causes echo to be called
969 by set_waiting_for_input's next invocation. */
970 signal (SIGALRM
, request_echo
);
973 alarmtime
= echo_keystrokes
;
974 alarm ((unsigned) alarmtime
);
977 c
= kbd_buffer_read_command_char ();
979 /* Terminate Emacs in batch mode if at eof. */
980 if (noninteractive
&& c
< 0)
981 Fkill_emacs (make_number (1));
985 bcopy (save_jump
, getcjmp
, sizeof getcjmp
);
987 /* Cancel alarm if it was set and has not already gone off. */
988 if (alarmtime
> 0) alarm (0);
990 echo_area_contents
= 0;
992 if (c
< 0) return -1;
994 c
&= meta_key
? 0377 : 0177;
996 if (XTYPE (Vkeyboard_translate_table
) == Lisp_String
997 && XSTRING (Vkeyboard_translate_table
)->size
> c
)
998 c
= XSTRING (Vkeyboard_translate_table
)->data
[c
];
1001 recent_keys
[recent_keys_index
] = c
;
1002 recent_keys_index
= (recent_keys_index
+ 1) % sizeof recent_keys
;
1010 store_kbd_macro_char (c
);
1015 reread_first
: /* Rereading a char and it is the first in a command. */
1019 /* Record this character as part of the current key. */
1020 if (this_command_key_count
== this_command_keys_size
)
1022 this_command_keys_size
*= 2;
1024 = (unsigned char *) xrealloc (this_command_keys
,
1025 this_command_keys_size
);
1027 this_command_keys
[this_command_key_count
++] = c
;
1029 /* Rereading in the middle of a command. */
1032 last_input_char
= c
;
1036 /* Process the help character specially if enabled */
1037 if (c
== help_char
&& !NULL (Vhelp_form
))
1039 count
= specpdl_ptr
- specpdl
;
1041 record_unwind_protect (Fset_window_configuration
,
1042 Fcurrent_window_configuration ());
1044 tem
= Feval (Vhelp_form
);
1045 if (XTYPE (tem
) == Lisp_String
)
1046 internal_with_output_to_temp_buffer ("*Help*", print_help
, tem
);
1049 c
= read_command_char (0);
1050 /* Remove the help from the screen */
1056 c
= read_command_char (0);
1067 Fprinc (object
, Qnil
);
1071 /* Low level keyboard input.
1072 Read characters into kbd_buffer
1073 from which they are obtained by kbd_buffer_read_command_char. */
1075 /* Set this for debugging, to have a way to get out */
1078 /* Store a character obtained at interrupt level into kbd_buffer, fifo */
1079 kbd_buffer_store_char (c
)
1085 || ((c
== (0200 | quit_char
)) && !meta_key
))
1087 interrupt_signal ();
1091 if (c
&& c
== stop_character
)
1097 if (kbd_ptr
!= kbd_buffer
)
1099 bcopy (kbd_ptr
, kbd_buffer
, kbd_count
);
1100 kbd_ptr
= kbd_buffer
;
1103 if (kbd_count
< sizeof kbd_buffer
)
1105 kbd_buffer
[kbd_count
++] = c
;
1109 kbd_buffer_read_command_char ()
1119 /* Either ordinary input buffer or C-g buffered means we can return. */
1122 if (!NULL (Vquit_flag
))
1123 quit_throw_to_read_command_char ();
1125 /* One way or another, wait until input is available; then, if
1126 interrupt handlers have not read it, read it now. */
1129 wait_for_kbd_input ();
1131 /* Note SIGIO has been undef'd if FIONREAD is missing. */
1138 wait_reading_process_input (0, -1, 1);
1140 /* Note SIGIO has been undef'd if FIONREAD is missing. */
1142 if (interrupt_input
)
1145 set_waiting_for_input (0);
1148 clear_waiting_for_input ();
1149 sigunblockx (SIGIO
);
1152 interrupt_input
= 0;
1153 #endif /* not SIGIO */
1154 #endif /* subprocesses */
1156 if (!interrupt_input
&& !kbd_count
)
1158 read_avail_input (0);
1161 #endif /* not VMS */
1164 input_pending
= --kbd_count
> 0;
1165 c
= *kbd_ptr
; /* *kbd_ptr++ would have a timing error. */
1166 kbd_ptr
++; /* See kbd_buffer_store_char. */
1167 return (c
& (meta_key
? 0377 : 0177)); /* Clean up if sign was extended. */
1170 /* Force an attempt to read input regardless of what FIONREAD says. */
1175 detect_input_pending ();
1179 /* Store into *addr the number of terminal input chars available.
1180 Equivalent to ioctl (0, FIONREAD, addr) but works
1181 even if FIONREAD does not exist. */
1184 get_input_pending (addr
)
1188 /* On VMS, we always have something in the buffer
1189 if any input is available. */
1190 /*** It might be simpler to make interrupt_input 1 on VMS ***/
1191 *addr
= kbd_count
| !NULL (Vquit_flag
);
1193 /* First of all, have we already counted some input? */
1194 *addr
= kbd_count
| !NULL (Vquit_flag
);
1195 /* If input is being read as it arrives, and we have none, there is none. */
1196 if (*addr
> 0 || (interrupt_input
&& ! interrupts_deferred
&& ! force_input
))
1201 /* If we can count the input without reading it, do so. */
1202 if (ioctl (0, FIONREAD
, addr
) < 0)
1204 if (*addr
== 0 || read_socket_hook
== 0)
1206 /* If the input consists of window-events, not all of them
1207 are necessarily kbd chars. So process all the input
1208 and see how many kbd chars we got. */
1213 /* It seems there is a timing error such that a SIGIO can be handled here
1214 and cause kbd_count to become nonzero even though raising of SIGIO
1215 has already been turned off. */
1216 SIGMASKTYPE mask
= sigblock (sigmask (SIGIO
));
1218 read_avail_input (*addr
);
1222 /* If we can't count the input, read it (if any) and see what we got. */
1223 read_avail_input (*addr
);
1225 *addr
= kbd_count
| !NULL (Vquit_flag
);
1229 /* Read pending any input out of the system and into Emacs. */
1231 /* This function is temporary in Emacs 18. It is used only
1232 with X windows. X windows always turns on interrupt input
1233 if possible, so this function has nothing to do except
1234 on systems that don't have SIGIO. And they also don't have FIONREAD. */
1236 consume_available_input ()
1239 if (!interrupt_input
|| interrupts_deferred
)
1241 read_avail_input (0);
1244 /* Read any terminal input already buffered up by the system
1245 into the kbd_buffer, assuming the buffer is currently empty.
1248 If NREAD is nonzero, assume it contains # chars of raw data waiting.
1249 If it is zero, we determine that datum.
1251 Input gets into the kbd_buffer either through this function
1252 (at main program level) or at interrupt level if input
1253 is interrupt-driven. */
1256 read_avail_input (nread
)
1259 /* This function is not used on VMS. */
1261 char buf
[256 * BUFFER_SIZE_FACTOR
];
1268 get_input_pending (&nread
);
1272 if (nread
> sizeof buf
)
1275 /* Read what is waiting. */
1276 if (read_socket_hook
)
1277 nread
= (*read_socket_hook
) (0, buf
, nread
);
1279 nread
= read (0, buf
, nread
);
1281 #else /* no FIONREAD */
1284 /* When talking to Xwindows using streams, something gets screwed up
1285 if Emacs alters this flag in the descriptor. */
1286 if (!read_socket_hook
)
1288 fcntl (fileno (stdin
), F_SETFL
, O_NDELAY
);
1289 if (read_socket_hook
)
1291 nread
= (*read_socket_hook
) (0, buf
, sizeof buf
);
1295 nread
= read (fileno (stdin
), buf
, sizeof buf
);
1298 /* The kernel sometimes fails to deliver SIGHUP for ptys.
1299 This looks incorrect, but it isn't, because _BSD causes
1300 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
1301 and that causes a value other than 0 when there is no input. */
1306 if (nread
== -1 && (errno
== EAGAIN
|| errno
== EBADSLT
))
1308 if (nread
== -1 && errno
== EAGAIN
)
1312 if (!read_socket_hook
)
1314 fcntl (fileno (stdin
), F_SETFL
, 0);
1317 #endif /* not USG */
1318 #endif /* no FIONREAD */
1320 /* Scan the chars for C-g and store them in kbd_buffer. */
1322 kbd_ptr
= kbd_buffer
;
1324 for (i
= 0; i
< nread
; i
++)
1326 kbd_buffer_store_char (buf
[i
]);
1327 /* Don't look at input that follows a C-g too closely.
1328 This reduces lossage due to autorepeat on C-g. */
1329 if (buf
[i
] == quit_char
)
1332 #endif /* not VMS */
1335 #ifdef SIGIO /* for entire page */
1336 /* Note SIGIO has been undef'd if FIONREAD is missing. */
1338 /* If using interrupt input and some input chars snuck into the
1339 buffer before we enabled interrupts, fake an interrupt for them. */
1344 if (interrupt_input
)
1346 if (ioctl (0, FIONREAD
, &nread
) < 0)
1351 input_available_signal (SIGIO
);
1357 input_available_signal (signo
)
1360 unsigned char buf
[256 * BUFFER_SIZE_FACTOR
];
1363 /* Must preserve main program's value of errno. */
1364 int old_errno
= errno
;
1366 extern int select_alarmed
;
1370 /* USG systems forget handlers when they are used;
1371 must reestablish each time */
1372 signal (signo
, input_available_signal
);
1379 if (input_available_clear_word
)
1380 *input_available_clear_word
= 0;
1384 if (ioctl (0, FIONREAD
, &nread
) < 0)
1385 /* Formerly simply exited the loop, but that sometimes led to
1386 a failure of Emacs to terminate.
1387 SIGHUP seems appropriate if we can't reach the terminal. */
1388 kill (getpid (), SIGHUP
);
1392 select_alarmed
= 1; /* Force the select emulator back to life */
1394 if (read_socket_hook
)
1396 nread
= (*read_socket_hook
) (0, buf
, sizeof buf
);
1402 if (nread
> sizeof buf
)
1404 nread
= read (0, buf
, nread
);
1407 for (i
= 0; i
< nread
; i
++)
1409 kbd_buffer_store_char (buf
[i
]);
1410 /* Don't look at input that follows a C-g too closely.
1411 This reduces lossage due to autorepeat on C-g. */
1412 if (buf
[i
] == quit_char
)
1424 /* This is turned off because it didn't produce much speedup. */
1426 /* Read a single-char key sequence. Do not redisplay.
1427 Return 1 if successful, or 0 if what follows is not
1428 a single-char key. (In that case, a char has been unread.)
1429 This is used instead of read_key_sequence as an optimization
1430 just after a direct-updating command is done, since at such
1431 times we know that no redisplay is required. */
1434 fast_read_one_key (keybuf
)
1437 register Lisp_Object map
;
1439 register Lisp_Object tem
;
1442 /* Read a character, and do not redisplay. */
1443 c
= read_command_char (-1);
1446 /* Assume until further notice that we are unlucky
1447 and will return zero, so this char will be
1448 reread by read_key_sequence. */
1450 unread_command_char
= c
;
1452 if (c
< 0 || c
>= 0200)
1455 map
= current_buffer
->keymap
;
1456 if (!EQ (map
, Qnil
))
1458 tem
= get_keyelt (access_keymap (map
, c
));
1459 if (!EQ (tem
, Qnil
))
1463 XSET (map
, Lisp_Vector
, global_map
);
1465 ? get_keyelt (access_keymap (map
, c
))
1468 read_key_sequence_cmd
= tem
;
1470 /* trace symbols to their function definitions */
1472 while (XTYPE (tem
) == Lisp_Symbol
&& !NULL (tem
)
1473 && !EQ (tem
, Qunbound
))
1474 tem
= XSYMBOL (tem
)->function
;
1476 /* Is the definition a prefix character? */
1478 if (XTYPE (tem
) == Lisp_Vector
||
1479 (CONSP (tem
) && EQ (XCONS (tem
)->car
, Qkeymap
)))
1482 unread_command_char
= -1;
1489 /* Read a sequence of keys that ends with a non prefix character,
1490 and store them in KEYBUF, a buffer of size BUFSIZE.
1491 Prompt with PROMPT. Echo starting immediately unless `prompt' is 0.
1492 Return the length of the key sequence stored.
1493 NODISPLAY nonzero means don't do redisplay before the first character
1494 (just for speedup). */
1497 read_key_sequence (keybuf
, bufsize
, prompt
, nodisplay
)
1500 unsigned char *prompt
;
1504 Lisp_Object nextlocal
, nextglobal
;
1505 register int c
, nextc
;
1506 Lisp_Object local
, global
;
1511 echo_prompt (prompt
);
1512 else if (cursor_in_echo_area
)
1516 nextc
= read_command_char (nodisplay
? -1 : !prompt
);
1517 nextlocal
= current_buffer
->keymap
;
1518 XSET (nextglobal
, Lisp_Vector
, global_map
);
1521 while (!NULL (nextlocal
) || !NULL (nextglobal
))
1524 error ("key sequence too long");
1532 c
= read_command_char (!prompt
);
1541 c
= meta_prefix_char
;
1546 global
= !NULL (nextglobal
)
1547 ? get_keyelt (access_keymap (nextglobal
, c
))
1550 local
= !NULL (nextlocal
)
1551 ? get_keyelt (access_keymap (nextlocal
, c
))
1554 /* If C is not defined in either keymap
1555 and it is an uppercase letter, try corresponding lowercase. */
1557 if (NULL (global
) && NULL (local
) && UPPERCASEP (c
))
1559 global
= !NULL (nextglobal
)
1560 ? get_keyelt (access_keymap (nextglobal
, DOWNCASE (c
)))
1563 local
= !NULL (nextlocal
)
1564 ? get_keyelt (access_keymap (nextlocal
, DOWNCASE (c
)))
1567 /* If that has worked better that the original char,
1568 downcase it permanently. */
1570 if (!NULL (global
) || !NULL (local
))
1572 keybuf
[i
] = c
= DOWNCASE (c
);
1581 read_key_sequence_cmd
= !NULL (local
) ? local
: global
;
1583 /* trace symbols to their function definitions */
1585 while (XTYPE (global
) == Lisp_Symbol
&& !NULL (global
)
1586 && !EQ (global
, Qunbound
))
1587 global
= XSYMBOL (global
)->function
;
1588 while (XTYPE (local
) == Lisp_Symbol
&& !NULL (local
)
1589 && !EQ (local
, Qunbound
))
1590 local
= XSYMBOL (local
)->function
;
1592 /* Are the definitions prefix characters? */
1594 if (XTYPE (local
) == Lisp_Vector
||
1595 (CONSP (local
) && EQ (XCONS (local
)->car
, Qkeymap
))
1597 /* If nextc is set, we are processing a prefix char
1598 that represents a meta-bit.
1599 Let a global prefix definition override a local non-prefix.
1600 This is for minibuffers that redefine Escape for completion.
1601 A real Escape gets completion, but Meta bits get ESC-prefix. */
1602 ((NULL (local
) || nextc
>= 0)
1603 && (XTYPE (global
) == Lisp_Vector
||
1604 (CONSP (global
) && EQ (XCONS (global
)->car
, Qkeymap
)))))
1606 if (XTYPE (local
) == Lisp_Vector
||
1607 (CONSP (local
) && EQ (XCONS (local
)->car
, Qkeymap
)))
1612 if (XTYPE (global
) == Lisp_Vector
||
1613 (CONSP (global
) && EQ (XCONS (global
)->car
, Qkeymap
)))
1614 nextglobal
= global
;
1623 DEFUN ("read-key-sequence", Fread_key_sequence
, Sread_key_sequence
, 1, 1, 0,
1624 "Read a sequence of keystrokes and return as a string.\n\
1625 The sequence is sufficient to specify a non-prefix command\n\
1626 starting from the current local and global keymaps.\n\
1627 A C-g typed while in this function is treated like\n\
1628 any other character, and quit-flag is not set.\n\
1629 One arg, PROMPT, a prompt string or nil, meaning do not prompt specially.")
1637 CHECK_STRING (prompt
, 0);
1640 this_command_key_count
= 0;
1641 i
= read_key_sequence (keybuf
, sizeof keybuf
,
1642 (NULL (prompt
)) ? 0 : XSTRING (prompt
)->data
,
1644 return make_string (keybuf
, i
);
1647 DEFUN ("command-execute", Fcommand_execute
, Scommand_execute
, 1, 2, 0,
1648 "Execute CMD as an editor command.\n\
1649 CMD must be a symbol that satisfies the `commandp' predicate.\n\
1650 Optional second arg RECORD-FLAG non-nil\n\
1651 means unconditionally put this command in the command-history.\n\
1652 Otherwise, this is done only if an arg is read using the minibuffer.")
1654 Lisp_Object cmd
, record
;
1656 register Lisp_Object final
;
1657 register Lisp_Object tem
;
1658 Lisp_Object prefixarg
;
1659 struct backtrace backtrace
;
1660 extern int debug_on_next_call
;
1662 prefixarg
= Vprefix_arg
, Vprefix_arg
= Qnil
;
1663 Vcurrent_prefix_arg
= prefixarg
;
1664 debug_on_next_call
= 0;
1666 if (XTYPE (cmd
) == Lisp_Symbol
)
1668 tem
= Fget (cmd
, Qdisabled
);
1670 return call0 (Vdisabled_command_hook
);
1676 while (XTYPE (final
) == Lisp_Symbol
)
1678 if (EQ (Qunbound
, XSYMBOL (final
)->function
))
1679 Fsymbol_function (final
); /* Get an error! */
1680 final
= XSYMBOL (final
)->function
;
1683 if (CONSP (final
) && (tem
= Fcar (final
), EQ (tem
, Qautoload
)))
1684 do_autoload (final
, cmd
);
1689 if (CONSP (final
) || XTYPE (final
) == Lisp_Subr
)
1691 backtrace
.next
= backtrace_list
;
1692 backtrace_list
= &backtrace
;
1693 backtrace
.function
= &Qcall_interactively
;
1694 backtrace
.args
= &cmd
;
1695 backtrace
.nargs
= 1;
1696 backtrace
.evalargs
= 0;
1698 tem
= Fcall_interactively (cmd
, record
);
1700 backtrace_list
= backtrace
.next
;
1703 if (XTYPE (final
) == Lisp_String
)
1705 return Fexecute_kbd_macro (final
, prefixarg
);
1710 DEFUN ("execute-extended-command", Fexecute_extended_command
, Sexecute_extended_command
,
1712 "Read function name, then read its arguments and call it.")
1714 Lisp_Object prefixarg
;
1716 Lisp_Object function
;
1718 Lisp_Object saved_keys
;
1719 struct gcpro gcpro1
;
1721 saved_keys
= Fthis_command_keys ();
1722 GCPRO1 (saved_keys
);
1726 if (EQ (prefixarg
, Qminus
))
1728 else if (CONSP (prefixarg
) && XINT (XCONS (prefixarg
)->car
) == 4)
1729 strcpy (buf
, "C-u ");
1730 else if (CONSP (prefixarg
) && XTYPE (XCONS (prefixarg
)->car
) == Lisp_Int
)
1731 sprintf (buf
, "%d ", XINT (XCONS (prefixarg
)->car
));
1732 else if (XTYPE (prefixarg
) == Lisp_Int
)
1733 sprintf (buf
, "%d ", XINT (prefixarg
));
1735 /* This isn't strictly correct if execute-extended-command
1736 is bound to anything else */
1737 strcat (buf
, "M-x ");
1739 function
= Fcompleting_read (build_string (buf
), Vobarray
, Qcommandp
, Qt
, Qnil
);
1741 saved_keys
= concat2 (saved_keys
, function
);
1742 if (this_command_keys_size
< XSTRING (saved_keys
)->size
)
1744 /* This makes the buffer bigger than necessary, but that's okay. */
1745 this_command_keys_size
+= XSTRING (saved_keys
)->size
;
1746 this_command_keys
= (unsigned char *) xrealloc (this_command_keys
,
1747 this_command_keys_size
);
1749 bcopy (XSTRING (saved_keys
)->data
, this_command_keys
,
1750 XSTRING (saved_keys
)->size
+ 1);
1751 this_command_key_count
= XSTRING (saved_keys
)->size
;
1755 function
= Fintern (function
, Vobarray
);
1756 Vprefix_arg
= prefixarg
;
1757 this_command
= function
;
1759 return Fcommand_execute (function
, Qt
);
1762 detect_input_pending ()
1765 get_input_pending (&input_pending
);
1767 return input_pending
;
1770 /* This is called in some cases before a possible quit.
1771 It cases the next call to detect_input_pending to recompute input_pending.
1772 So calling this function unnecessarily can't do any harm. */
1773 clear_input_pending ()
1778 DEFUN ("input-pending-p", Finput_pending_p
, Sinput_pending_p
, 0, 0, 0,
1779 "T if command input is currently available with no waiting.\n\
1780 Actually, the value is NIL only if we can be sure that no input is available.")
1783 if (unread_command_char
>= 0) return Qt
;
1785 return detect_input_pending () ? Qt
: Qnil
;
1788 DEFUN ("recent-keys", Frecent_keys
, Srecent_keys
, 0, 0, 0,
1789 "Return string of last 100 chars read from terminal.")
1793 if (total_keys
< sizeof recent_keys
)
1794 return make_string (recent_keys
, total_keys
);
1796 val
= make_string (recent_keys
, sizeof recent_keys
);
1797 bcopy (recent_keys
+ recent_keys_index
,
1798 XSTRING (val
)->data
,
1799 sizeof recent_keys
- recent_keys_index
);
1801 XSTRING (val
)->data
+ sizeof recent_keys
- recent_keys_index
,
1806 DEFUN ("this-command-keys", Fthis_command_keys
, Sthis_command_keys
, 0, 0, 0,
1807 "Return string of the keystrokes that invoked this command.")
1810 return make_string (this_command_keys
, this_command_key_count
);
1813 DEFUN ("recursion-depth", Frecursion_depth
, Srecursion_depth
, 0, 0, 0,
1814 "Return the current depth in recursive edits.")
1818 XFASTINT (temp
) = command_loop_level
+ minibuf_level
;
1822 DEFUN ("open-dribble-file", Fopen_dribble_file
, Sopen_dribble_file
, 1, 1,
1823 "FOpen dribble file: ",
1824 "Start writing all keyboard characters to FILE.\n\
1825 Use nil as an argument to close the dribble file.")
1834 file
= Fexpand_file_name (file
, Qnil
);
1835 dribble
= fopen (XSTRING (file
)->data
, "w");
1840 DEFUN ("discard-input", Fdiscard_input
, Sdiscard_input
, 0, 0, 0,
1841 "Discard the contents of the terminal input buffer.\n\
1842 Also flush any kbd macro definition in progress.")
1845 defining_kbd_macro
= 0;
1846 update_mode_lines
++;
1848 unread_command_char
= -1;
1850 discard_tty_input ();
1858 DEFUN ("suspend-emacs", Fsuspend_emacs
, Ssuspend_emacs
, 0, 1, "",
1859 "Stop Emacs and return to superior process. You can resume.\n\
1860 If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
1861 to be read as terminal input by Emacs's superior shell.\n\
1862 Before suspending, if `suspend-hook' is bound and value is non-nil\n\
1863 call the value as a function of no args. Don't suspend if it returns non-nil.\n\
1864 Otherwise, suspend normally and after resumption call\n\
1865 `suspend-resume-hook' if that is bound and non-nil.")
1867 Lisp_Object stuffstring
;
1869 register Lisp_Object tem
;
1870 int count
= specpdl_ptr
- specpdl
;
1871 int old_height
, old_width
;
1873 struct gcpro gcpro1
;
1874 extern init_sys_modes ();
1876 if (!NULL (stuffstring
))
1877 CHECK_STRING (stuffstring
, 0);
1878 GCPRO1 (stuffstring
);
1880 /* Call value of suspend-hook
1881 if it is bound and value is non-nil. */
1882 tem
= intern ("suspend-hook");
1883 tem
= XSYMBOL (tem
)->value
;
1884 if (! EQ (tem
, Qunbound
) && ! EQ (tem
, Qnil
))
1887 if (!EQ (tem
, Qnil
)) return Qnil
;
1890 get_screen_size (&old_width
, &old_height
);
1892 /* sys_suspend can get an error if it tries to fork a subshell
1893 and the system resources aren't available for that. */
1894 record_unwind_protect (init_sys_modes
, 0);
1895 stuff_buffered_input (stuffstring
);
1899 /* Check if terminal/window size has changed.
1900 Note that this is not useful when we are running directly
1901 with a window system; but suspend should be disabled in that case. */
1902 get_screen_size (&width
, &height
);
1903 if (width
!= old_width
|| height
!= old_height
)
1904 change_screen_size (height
, width
, 0, 0, 0);
1906 /* Call value of suspend-resume-hook
1907 if it is bound and value is non-nil. */
1908 tem
= intern ("suspend-resume-hook");
1909 tem
= XSYMBOL (tem
)->value
;
1910 if (! EQ (tem
, Qunbound
) && ! EQ (tem
, Qnil
))
1916 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
1917 Then in any case stuff anthing Emacs has read ahead and not used. */
1919 stuff_buffered_input (stuffstring
)
1920 Lisp_Object stuffstring
;
1922 register unsigned char *p
;
1924 /* stuff_char works only in BSD, versions 4.2 and up. */
1927 if (XTYPE (stuffstring
) == Lisp_String
)
1931 p
= XSTRING (stuffstring
)->data
;
1932 count
= XSTRING (stuffstring
)->size
;
1937 /* Anything we have read ahead, put back for the shell to read. */
1940 stuff_char (*kbd_ptr
++);
1945 #endif /* BSD and not BSD4_1 */
1948 set_waiting_for_input (word_to_clear
)
1949 long *word_to_clear
;
1951 input_available_clear_word
= word_to_clear
;
1953 /* Tell interrupt_signal to throw back to read_command_char, */
1954 waiting_for_input
= 1;
1956 /* If interrupt_signal was called before and buffered a C-g,
1957 make it run again now, to avoid timing error. */
1958 if (!NULL (Vquit_flag
))
1959 quit_throw_to_read_command_char ();
1961 /* Tell alarm signal to echo right away */
1964 /* If alarm has gone off already, echo now. */
1972 clear_waiting_for_input ()
1974 /* Tell interrupt_signal not to throw back to read_command_char, */
1975 waiting_for_input
= 0;
1977 input_available_clear_word
= 0;
1980 /* This routine is called at interrupt level in response to C-G.
1981 If interrupt_input, this is the handler for SIGINT.
1982 Otherwise, it is called from kbd_buffer_store_char,
1983 in handling SIGIO or SIGTINT.
1985 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
1986 immediately throw back to read_command_char.
1988 Otherwise it sets the Lisp variable quit-flag not-nil.
1989 This causes eval to throw, when it gets a chance.
1990 If quit-flag is already non-nil, it stops the job right away. */
1995 /* Must preserve main program's value of errno. */
1996 int old_errno
= errno
;
1997 extern Lisp_Object Vwindow_system
;
2000 /* USG systems forget handlers when they are used;
2001 must reestablish each time */
2002 signal (SIGINT
, interrupt_signal
);
2003 signal (SIGQUIT
, interrupt_signal
);
2008 if (!NULL (Vquit_flag
) && NULL (Vwindow_system
))
2013 #ifdef SIGTSTP /* Support possible in later USG versions */
2015 * On systems which can suspend the current process and return to the original
2016 * shell, this command causes the user to end up back at the shell.
2017 * The "Auto-save" and "Abort" questions are not asked until
2018 * the user elects to return to emacs, at which point he can save the current
2019 * job and either dump core or continue.
2024 if (sys_suspend () == -1)
2026 printf ("Not running as a subprocess;\n");
2027 printf ("you can continue or abort.\n");
2030 /* Perhaps should really fork an inferior shell?
2031 But that would not provide any way to get back
2032 to the original shell, ever. */
2033 printf ("No support for stopping a process on this operating system;\n");
2034 printf ("you can continue or abort.\n");
2035 #endif /* not VMS */
2036 #endif /* not SIGTSTP */
2037 printf ("Auto-save? (y or n) ");
2039 if (((c
= getchar ()) & ~040) == 'Y')
2040 Fdo_auto_save (Qnil
);
2041 while (c
!= '\n') c
= getchar ();
2043 printf ("Abort (and enter debugger)? (y or n) ");
2045 printf ("Abort (and dump core)? (y or n) ");
2046 #endif /* not VMS */
2048 if (((c
= getchar ()) & ~040) == 'Y')
2050 while (c
!= '\n') c
= getchar ();
2051 printf ("Continuing...\n");
2057 /* If executing a function that wants to be interrupted out of
2058 and the user has not deferred quitting by binding `inhibit-quit'
2059 then quit right away. */
2060 if (immediate_quit
&& NULL (Vinhibit_quit
))
2064 Fsignal (Qquit
, Qnil
);
2067 /* Else request quit when it's safe */
2071 if (waiting_for_input
&& !echoing
)
2072 quit_throw_to_read_command_char ();
2077 /* Handle a C-g by making read_command_char return C-g. */
2079 quit_throw_to_read_command_char ()
2081 quit_error_check ();
2083 /* Prevent another signal from doing this before we finish. */
2084 waiting_for_input
= 0;
2086 unread_command_char
= -1;
2087 #ifdef POLL_FOR_INPUT
2088 if (poll_suppress_count
!= 1)
2091 _longjmp (getcjmp
, 1);
2094 DEFUN ("set-input-mode", Fset_input_mode
, Sset_input_mode
, 2, 3, 0,
2095 "Set mode of reading keyboard input.\n\
2096 First arg non-nil means use input interrupts; nil means use CBREAK mode.\n\
2097 Second arg non-nil means use ^S/^Q flow control for output to terminal\n\
2098 (no effect except in CBREAK mode).\n\
2099 Optional third arg non-nil specifies character to use for quitting.\n\n\
2100 Note that the arguments will change incompatibly in version 19.")
2101 (interrupt
, flow
, quit
)
2102 Lisp_Object interrupt
, flow
, quit
;
2106 /* Note SIGIO has been undef'd if FIONREAD is missing. */
2107 #ifdef NO_SOCK_SIGIO
2108 if (read_socket_hook
)
2109 interrupt_input
= 0; /* No interrupts if reading from a socket. */
2111 #endif /* NO_SOCK_SIGIO */
2112 interrupt_input
= !NULL (interrupt
);
2113 #else /* not SIGIO */
2114 interrupt_input
= 0;
2115 #endif /* not SIGIO */
2116 flow_control
= !NULL (flow
);
2119 CHECK_NUMBER (quit
, 2);
2120 quit_char
= XINT (quit
);
2121 /* Don't let this value be out of range. */
2122 quit_char
&= (meta_key
? 0377 : 0177);
2130 this_command_keys_size
= 40;
2131 this_command_keys
= (unsigned char *) xmalloc (40);
2133 command_loop_level
= -1; /* Correct, before outermost invocation. */
2134 quit_char
= Ctl ('G');
2136 unread_command_char
= -1;
2137 recent_keys_index
= 0;
2140 kbd_ptr
= kbd_buffer
;
2143 if (!noninteractive
)
2145 signal (SIGINT
, interrupt_signal
);
2147 /* On systems with TERMIO, C-g is set up for both SIGINT and SIGQUIT
2148 and we can't tell which one it will give us. */
2149 signal (SIGQUIT
, interrupt_signal
);
2150 #endif /* HAVE_TERMIO */
2151 /* Note SIGIO has been undef'd if FIONREAD is missing. */
2153 signal (SIGIO
, input_available_signal
);
2157 /* Use interrupt input by default, if it works and noninterrupt input
2158 has deficiencies. */
2160 #ifdef INTERRUPT_INPUT
2161 interrupt_input
= 1;
2163 interrupt_input
= 0;
2169 if (keyboard_init_hook
)
2170 (*keyboard_init_hook
) ();
2172 poll_suppress_count
= 1;
2173 #ifdef POLL_FOR_INPUT
2180 Qself_insert_command
= intern ("self-insert-command");
2181 staticpro (&Qself_insert_command
);
2183 Qforward_char
= intern ("forward-char");
2184 staticpro (&Qforward_char
);
2186 Qbackward_char
= intern ("backward-char");
2187 staticpro (&Qbackward_char
);
2189 Qdisabled
= intern ("disabled");
2190 staticpro (&Qdisabled
);
2192 defsubr (&Sread_key_sequence
);
2193 defsubr (&Srecursive_edit
);
2194 defsubr (&Sinput_pending_p
);
2195 defsubr (&Scommand_execute
);
2196 defsubr (&Srecent_keys
);
2197 defsubr (&Sthis_command_keys
);
2198 defsubr (&Ssuspend_emacs
);
2199 defsubr (&Sabort_recursive_edit
);
2200 defsubr (&Sexit_recursive_edit
);
2201 defsubr (&Srecursion_depth
);
2202 defsubr (&Stop_level
);
2203 defsubr (&Sdiscard_input
);
2204 defsubr (&Sopen_dribble_file
);
2205 defsubr (&Sset_input_mode
);
2206 defsubr (&Sexecute_extended_command
);
2208 DEFVAR_LISP ("disabled-command-hook", &Vdisabled_command_hook
,
2209 "Value is called instead of any command that is disabled\n\
2210 \(has a non-nil disabled property).");
2212 DEFVAR_BOOL ("meta-flag", &meta_key
,
2213 "*Non-nil means treat 0200 bit in terminal input as Meta bit.");
2215 DEFVAR_INT ("last-command-char", &last_command_char
,
2216 "Last terminal input character that was part of a command, as an integer.");
2218 DEFVAR_INT ("last-input-char", &last_input_char
,
2219 "Last terminal input character, as an integer.");
2221 DEFVAR_INT ("unread-command-char", &unread_command_char
,
2222 "Character to be read as next input from command input stream, or -1 if none.");
2224 DEFVAR_INT ("meta-prefix-char", &meta_prefix_char
,
2225 "Meta-prefix character code. Meta-foo as command input\n\
2226 turns into this character followed by foo.");
2227 meta_prefix_char
= 033;
2229 DEFVAR_LISP ("last-command", &last_command
,
2230 "The last command executed. Normally a symbol with a function definition,\n\
2231 but can be whatever was found in the keymap, or whatever the variable\n\
2232 `this-command' was set to by that command.");
2233 last_command
= Qnil
;
2235 DEFVAR_LISP ("this-command", &this_command
,
2236 "The command now being executed.\n\
2237 The command can set this variable; whatever is put here\n\
2238 will be in last-command during the following command.");
2239 this_command
= Qnil
;
2241 DEFVAR_INT ("auto-save-interval", &auto_save_interval
,
2242 "*Number of keyboard input characters between auto-saves.\n\
2243 Zero means disable autosaving.");
2244 auto_save_interval
= 300;
2246 DEFVAR_INT ("echo-keystrokes", &echo_keystrokes
,
2247 "*Nonzero means echo unfinished commands after this many seconds of pause.");
2248 echo_keystrokes
= 1;
2250 DEFVAR_INT ("polling-period", &polling_period
,
2251 "*Interval between polling for input during Lisp execution.\n\
2252 The reason for polling is to make C-g work to stop a running program.\n\
2253 Polling is needed only when using X windows and SIGIO does not work.\n\
2254 Polling is automatically disabled in all other cases.");
2257 DEFVAR_INT ("help-char", &help_char
,
2258 "Character to recognize as meaning Help.\n\
2259 When it is read, do (eval help-form), and display result if it's a string.\n\
2260 If help-form's value is nil, this char can be read normally.");
2261 help_char
= Ctl ('H');
2263 DEFVAR_LISP ("help-form", &Vhelp_form
,
2264 "Form to execute when character help-char is read.\n\
2265 If the form returns a string, that string is displayed.\n\
2266 If help-form is nil, the help char is not recognized.");
2269 DEFVAR_LISP ("top-level", &Vtop_level
,
2270 "Form to evaluate when Emacs starts up.\n\
2271 Useful to set before you dump a modified Emacs.");
2274 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table
,
2275 "String used as translate table for keyboard input, or nil.\n\
2276 Each character is looked up in this string and the contents used instead.\n\
2277 If string is of length N, character codes N and up are untranslated.");
2278 Vkeyboard_translate_table
= Qnil
;
2283 ndefkey (Vglobal_map
, Ctl ('Z'), "suspend-emacs");
2284 ndefkey (Vctl_x_map
, Ctl ('Z'), "suspend-emacs");
2285 ndefkey (Vesc_map
, Ctl ('C'), "exit-recursive-edit");
2286 ndefkey (Vglobal_map
, Ctl (']'), "abort-recursive-edit");
2287 ndefkey (Vesc_map
, 'x', "execute-extended-command");