Release 18.59
[emacs.git] / src / keyboard.c
bloba27354d0454b4b6f35b88739c9d34f65d9da74f1
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)
9 any later version.
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. */
25 #include <signal.h>
27 #include "config.h"
28 #include <stdio.h>
29 #undef NULL
30 #include "termchar.h"
31 #include "termopts.h"
32 #include "termhooks.h"
33 #include "lisp.h"
34 #include "macros.h"
35 #include "window.h"
36 #include "commands.h"
37 #include "buffer.h"
38 #include <setjmp.h>
39 #include <errno.h>
41 extern int errno;
43 /* Get FIONREAD, if it is available. */
44 #ifdef USG
45 #include <termio.h>
46 #include <fcntl.h>
47 #else /* not USG */
48 #ifndef VMS
49 #include <sys/ioctl.h>
50 #endif /* not VMS */
51 #endif /* not USG */
53 #include "emacssignal.h"
55 /* Allow m- file to inhibit use of FIONREAD. */
56 #ifdef BROKEN_FIONREAD
57 #undef FIONREAD
58 #endif
60 /* Make all keyboard buffers much bigger when using X windows. */
61 #ifdef HAVE_X_WINDOWS
62 #define BUFFER_SIZE_FACTOR 16
63 #else
64 #define BUFFER_SIZE_FACTOR 1
65 #endif
67 /* Following definition copied from eval.c */
69 struct backtrace
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 */
76 char evalargs;
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. */
99 static int echoing;
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. */
109 int quit_char;
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. */
123 int last_input_char;
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. */
136 int num_input_chars;
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. */
145 int 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 */
174 int input_pending;
176 /* Nonzero if should obey 0200 bit in input chars as "Meta" */
177 int meta_key;
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. */
185 int kbd_count;
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. */
197 int interrupt_input;
199 /* Nonzero while interrupts are temporarily deferred during redisplay. */
201 int interrupts_deferred;
203 /* nonzero means use ^S/^Q for flow control. */
205 int flow_control;
207 #ifndef BSD4_1
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 */
215 #ifdef 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)
221 #endif /* BSD4_1 */
223 /* We are unable to use interrupts if FIONREAD is not available,
224 so flush SIGIO so we won't try. */
225 #ifndef FIONREAD
226 #ifdef SIGIO
227 #undef SIGIO
228 #endif
229 #endif
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
234 #ifndef SIGIO
235 #define POLL_FOR_INPUT
236 #endif
237 #endif
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. */
264 echo_prompt (str)
265 char *str;
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;
273 echo ();
276 /* Add the character C to the echo string,
277 if echoing is going on. */
279 echo_char (c)
280 int c;
282 extern char *push_key_description ();
284 if (immediate_echo)
286 char *ptr = echoptr;
288 if (ptr - echobuf > sizeof echobuf - 6)
289 return;
291 if (echoptr != echobuf)
292 *ptr++ = ' ';
294 ptr = push_key_description (c, ptr);
295 if (echoptr == echobuf && c == help_char)
297 strcpy (ptr, " (Type ? for further options)");
298 ptr += strlen (ptr);
301 *ptr = 0;
302 echoptr = ptr;
304 echo ();
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. */
311 echo_dash ()
313 if (!immediate_echo && echoptr == echobuf)
314 return;
316 /* Put a dash at the end of the buffer temporarily,
317 but make it go away when the next character is added. */
318 echoptr[0] = '-';
319 echoptr[1] = 0;
321 echo ();
324 /* Display the current echo string, and begin echoing if not already
325 doing so. */
327 echo ()
329 if (!immediate_echo)
331 int i;
332 immediate_echo = 1;
334 for (i = 0; i < this_command_key_count; i++)
335 echo_char (this_command_keys[i]);
336 echo_dash ();
339 echoing = 1;
340 message1 (echobuf);
341 echoing = 0;
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. */
349 cancel_echoing ()
351 immediate_echo = 0;
352 echoptr = echobuf;
355 /* When an auto-save happens, record the "time", and don't do again soon. */
356 record_auto_save ()
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\
368 to begin editing.")
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)
378 ? Fcurrent_buffer ()
379 : Qnil));
381 recursive_edit_1 ();
383 unbind_to (count);
384 return Qnil;
387 Lisp_Object
388 recursive_edit_1 ()
390 int count = specpdl_ptr - specpdl;
391 Lisp_Object val;
393 if (command_loop_level > 0)
395 specbind (Qstandard_output, Qt);
396 specbind (Qstandard_input, Qt);
399 val = command_loop ();
400 if (EQ (val, Qt))
401 Fsignal (Qquit, Qnil);
403 unbind_to (count);
404 return Qnil;
407 Lisp_Object
408 recursive_edit_unwind (buffer)
409 Lisp_Object buffer;
411 if (!NULL (buffer))
412 Fset_buffer (buffer);
413 command_loop_level--;
414 update_mode_lines = 1;
415 return Qnil;
418 Lisp_Object
419 cmd_error (data)
420 Lisp_Object data;
422 Lisp_Object errmsg, tail, errname, file_error;
423 struct gcpro gcpro1;
424 int i;
426 Vquit_flag = Qnil;
427 Vinhibit_quit = Qt;
428 Vstandard_output = Qt;
429 Vstandard_input = Qt;
430 Vexecuting_macro = Qnil;
431 echo_area_contents = 0;
433 Fdiscard_input ();
434 bell ();
436 errname = Fcar (data);
438 if (EQ (errname, Qerror))
440 data = Fcdr (data);
441 if (!CONSP (data)) data = Qnil;
442 errmsg = Fcar (data);
443 file_error = Qnil;
445 else
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;
457 tail = Fcdr (data);
458 GCPRO1 (tail);
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)
466 Fprinc (errmsg, Qt);
467 else
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);
475 else
476 Fprin1 (Fcar (tail), Qt);
478 UNGCPRO;
480 /* In -batch mode, force out the error message and newlines after it
481 and then die. */
482 if (noninteractive)
484 message ("");
485 Fkill_emacs (make_number (-1));
488 Vquit_flag = Qnil;
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. */
503 Lisp_Object
504 command_loop ()
506 if (command_loop_level > 0 || minibuf_level > 0)
508 return internal_catch (Qexit, command_loop_2, Qnil);
510 else
511 while (1)
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. */
516 if (noninteractive)
517 Fkill_emacs (Qt);
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). */
527 Lisp_Object
528 command_loop_2 ()
530 register Lisp_Object val;
532 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
533 while (!NULL (val));
534 return Qnil;
537 Lisp_Object
538 top_level_2 ()
540 return Feval (Vtop_level);
543 Lisp_Object
544 top_level_1 ()
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)");
551 else
552 message ("Bare Emacs (standard Lisp code not loaded)");
553 return Qnil;
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)
577 Fthrow (Qexit, Qt);
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 ();
586 Lisp_Object
587 command_loop_1 ()
589 Lisp_Object cmd;
590 int lose;
591 int nonundocount;
592 char keybuf[30];
593 int i;
594 int no_redisplay;
595 int no_direct;
597 Vprefix_arg = Qnil;
598 waiting_for_input = 0;
599 cancel_echoing ();
601 /* Don't clear out last_command at the beginning of a macro. */
602 if (NULL (Vexecuting_macro)
603 || XTYPE (Vexecuting_macro) != Lisp_String)
604 last_command = Qt;
605 nonundocount = 0;
606 no_redisplay = 0;
607 this_command_key_count = 0;
609 while (1)
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 ();
625 no_direct = 0;
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);
635 unbind_to (count);
637 echo_area_contents = 0;
638 no_direct = 1;
639 if (!NULL (Vquit_flag))
641 Vquit_flag = Qnil;
642 unread_command_char = quit_char;
646 i = 0;
647 #if 0
648 /* If prev. command was directly displayed, we don't need
649 redisplay. Try shortcut for reading single-char key sequence. */
650 if (no_redisplay)
651 i = fast_read_one_key (keybuf);
652 #endif /* 0 */
653 /* Shortcut not applicable or found a prefix key.
654 Take full precautions and read key sequence the hard way. */
655 if (i == 0)
657 #ifdef C_ALLOCA
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. */
689 no_redisplay = 0;
691 /* Execute the command. */
693 if (NULL (cmd))
695 /* nil means key is undefined. */
696 bell ();
697 defining_kbd_macro = 0;
698 update_mode_lines++;
699 Vprefix_arg = Qnil;
701 else
703 this_command = cmd;
704 if (NULL (Vprefix_arg) && ! no_direct)
706 if (EQ (cmd, Qforward_char) && point < ZV)
708 lose = FETCH_CHAR (point);
709 SET_PT (point + 1);
710 if (lose >= ' ' && lose < 0177
711 && (XFASTINT (XWINDOW (selected_window)->last_modified)
712 >= MODIFF)
713 && (XFASTINT (XWINDOW (selected_window)->last_point)
714 == 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);
720 goto directly_done;
722 else if (EQ (cmd, Qbackward_char) && point > BEGV)
724 SET_PT (point - 1);
725 lose = FETCH_CHAR (point);
726 if (lose >= ' ' && lose < 0177
727 && (XFASTINT (XWINDOW (selected_window)->last_modified)
728 >= MODIFF)
729 && (XFASTINT (XWINDOW (selected_window)->last_point)
730 == 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);
736 goto directly_done;
738 else if (EQ (cmd, Qself_insert_command))
740 if (NULL (Vexecuting_macro) &&
741 !EQ (minibuf_window, selected_window))
743 if (!nonundocount || nonundocount >= 20)
745 Fundo_boundary ();
746 nonundocount = 0;
748 nonundocount++;
750 lose = (XFASTINT (XWINDOW (selected_window)->last_modified)
751 < MODIFF)
752 || (XFASTINT (XWINDOW (selected_window)->last_point)
753 != 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))
761 lose = 1;
762 nonundocount = 0;
764 if (!lose
765 && (point == ZV || FETCH_CHAR (point) == '\n')
766 && last_command_char >= ' '
767 && last_command_char < 0177)
768 no_redisplay
769 = direct_output_for_insert (last_command_char);
770 goto directly_done;
774 /* Here for a command that isn't executed directly */
776 nonundocount = 0;
777 if (NULL (Vprefix_arg))
778 Fundo_boundary ();
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. */
783 directly_done: ;
785 if (NULL (Vprefix_arg))
787 last_command = this_command;
788 this_command_key_count = 0;
789 cancel_echoing ();
794 /* Input of single characters from keyboard */
796 Lisp_Object print_help ();
798 int echo_flag;
799 int echo_now;
801 /* Alarm interrupt calls this and requests echoing at earliest safe time. */
802 request_echo ()
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. */
808 #ifdef subprocesses
809 #ifdef BSD4_1
810 extern int select_alarmed;
811 if (select_alarmed == 0)
813 select_alarmed = 1;
814 sigrelse (SIGALRM);
815 return;
817 #endif
818 #endif
820 #ifdef BSD4_1
821 sigisheld (SIGALRM);
822 #endif
824 if (echo_now)
825 echo ();
826 else
827 echo_flag = 1;
829 #ifdef BSD4_1
830 sigunhold (SIGALRM);
831 #endif
833 errno = old_errno;
836 /* Nonzero means polling for input is temporarily suppresed. */
837 int poll_suppress_count;
839 /* Number of seconds between polling for input. */
840 int polling_period;
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. */
848 input_poll_signal ()
850 int junk;
852 if (!waiting_for_input)
853 read_avail_input (&junk);
854 signal (SIGALRM, input_poll_signal);
855 alarm (polling_period);
858 #endif
860 /* Begin signals to poll for input, if they are appropriate.
861 This function is called unconditionally from various places. */
863 start_polling ()
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);
876 #endif
879 /* Turn off polling. */
881 stop_polling ()
883 #ifdef POLL_FOR_INPUT
884 if (read_socket_hook)
886 if (poll_suppress_count == 0)
888 polling_for_input = 0;
889 alarm (0);
891 poll_suppress_count++;
893 #endif
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.
899 1 means do both. */
901 read_command_char (commandflag)
902 int commandflag;
904 register int c;
905 int alarmtime;
906 int count;
907 Lisp_Object tem;
908 jmp_buf save_jump;
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)
916 goto reread_first;
917 goto reread;
920 if (!NULL (Vexecuting_macro))
922 if (XTYPE (Vexecuting_macro) != Lisp_String
923 || XSTRING (Vexecuting_macro)->size <= executing_macro_index)
924 return -1;
925 QUIT;
926 c = XSTRING (Vexecuting_macro)->data[executing_macro_index++];
927 goto from_macro;
930 /* Save outer setjmp data, in case called recursively. */
931 bcopy (getcjmp, save_jump, sizeof getcjmp);
933 stop_polling ();
935 if (commandflag >= 0 && !detect_input_pending ())
936 redisplay ();
938 if (commandflag != 0
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))
946 c = quit_char;
947 waiting_for_input = 0;
948 input_available_clear_word = 0;
950 goto non_reread;
953 /* Message turns off echoing unless more keystrokes turn it on again. */
954 if (echo_area_contents && *echo_area_contents && echo_area_contents != echobuf)
955 cancel_echoing ();
956 else
957 /* If already echoing, continue, and prompt. */
958 echo_dash ();
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);
971 echo_flag = 0;
972 echo_now = 0;
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));
983 non_reread:
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];
1000 total_keys++;
1001 recent_keys[recent_keys_index] = c;
1002 recent_keys_index = (recent_keys_index + 1) % sizeof recent_keys;
1004 if (dribble)
1006 putc (c, dribble);
1007 fflush (dribble);
1010 store_kbd_macro_char (c);
1012 start_polling ();
1014 from_macro:
1015 reread_first: /* Rereading a char and it is the first in a command. */
1017 echo_char (c);
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;
1023 this_command_keys
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. */
1030 reread:
1032 last_input_char = c;
1034 num_input_chars++;
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);
1048 cancel_echoing ();
1049 c = read_command_char (0);
1050 /* Remove the help from the screen */
1051 unbind_to (count);
1052 redisplay ();
1053 if (c == 040)
1055 cancel_echoing ();
1056 c = read_command_char (0);
1060 return c;
1063 Lisp_Object
1064 print_help (object)
1065 Lisp_Object object;
1067 Fprinc (object, Qnil);
1068 return 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 */
1076 int stop_character;
1078 /* Store a character obtained at interrupt level into kbd_buffer, fifo */
1079 kbd_buffer_store_char (c)
1080 register int c;
1082 c &= 0377;
1084 if (c == quit_char
1085 || ((c == (0200 | quit_char)) && !meta_key))
1087 interrupt_signal ();
1088 return;
1091 if (c && c == stop_character)
1093 sys_suspend ();
1094 return;
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 ()
1111 register int c;
1113 if (noninteractive)
1115 c = getchar ();
1116 return c;
1119 /* Either ordinary input buffer or C-g buffered means we can return. */
1120 while (!kbd_count)
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. */
1128 #ifdef VMS
1129 wait_for_kbd_input ();
1130 #else
1131 /* Note SIGIO has been undef'd if FIONREAD is missing. */
1132 #ifdef SIGIO
1133 gobble_input ();
1134 #endif /* SIGIO */
1135 if (!kbd_count)
1137 #ifdef subprocesses
1138 wait_reading_process_input (0, -1, 1);
1139 #else
1140 /* Note SIGIO has been undef'd if FIONREAD is missing. */
1141 #ifdef SIGIO
1142 if (interrupt_input)
1144 sigblockx (SIGIO);
1145 set_waiting_for_input (0);
1146 while (!kbd_count)
1147 sigpausex (SIGIO);
1148 clear_waiting_for_input ();
1149 sigunblockx (SIGIO);
1151 #else
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. */
1172 force_input_read ()
1174 force_input = 1;
1175 detect_input_pending ();
1176 force_input = 0;
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. */
1183 static void
1184 get_input_pending (addr)
1185 int *addr;
1187 #ifdef VMS
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);
1192 #else
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))
1197 return;
1198 #ifdef FIONREAD
1199 if (! force_input)
1201 /* If we can count the input without reading it, do so. */
1202 if (ioctl (0, FIONREAD, addr) < 0)
1203 *addr = 0;
1204 if (*addr == 0 || read_socket_hook == 0)
1205 return;
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. */
1210 #endif
1211 #ifdef SIGIO
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));
1217 if (kbd_count == 0)
1218 read_avail_input (*addr);
1219 sigsetmask (mask);
1221 #else
1222 /* If we can't count the input, read it (if any) and see what we got. */
1223 read_avail_input (*addr);
1224 #endif
1225 *addr = kbd_count | !NULL (Vquit_flag);
1226 #endif
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. */
1235 void
1236 consume_available_input ()
1238 #ifdef SIGIO
1239 if (!interrupt_input || interrupts_deferred)
1240 #endif
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.
1246 Never waits.
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. */
1255 static void
1256 read_avail_input (nread)
1257 int nread;
1259 /* This function is not used on VMS. */
1260 #ifndef VMS
1261 char buf[256 * BUFFER_SIZE_FACTOR];
1262 register int i;
1264 #ifdef FIONREAD
1265 if (! force_input)
1267 if (nread == 0)
1268 get_input_pending (&nread);
1269 if (nread == 0)
1270 return;
1272 if (nread > sizeof buf)
1273 nread = sizeof buf;
1275 /* Read what is waiting. */
1276 if (read_socket_hook)
1277 nread = (*read_socket_hook) (0, buf, nread);
1278 else
1279 nread = read (0, buf, nread);
1281 #else /* no FIONREAD */
1282 #ifdef USG
1283 #ifdef SYSV_STREAMS
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)
1287 #endif
1288 fcntl (fileno (stdin), F_SETFL, O_NDELAY);
1289 if (read_socket_hook)
1291 nread = (*read_socket_hook) (0, buf, sizeof buf);
1293 else
1295 nread = read (fileno (stdin), buf, sizeof buf);
1297 #ifdef AIX
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. */
1302 if (nread == 0)
1303 kill (SIGHUP, 0);
1304 #endif
1305 #ifdef EBADSLT
1306 if (nread == -1 && (errno == EAGAIN || errno == EBADSLT))
1307 #else
1308 if (nread == -1 && errno == EAGAIN)
1309 #endif
1310 nread = 0;
1311 #ifdef SYSV_STREAMS
1312 if (!read_socket_hook)
1313 #endif
1314 fcntl (fileno (stdin), F_SETFL, 0);
1315 #else /* not USG */
1316 you lose
1317 #endif /* not USG */
1318 #endif /* no FIONREAD */
1320 /* Scan the chars for C-g and store them in kbd_buffer. */
1321 if (kbd_count == 0)
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)
1330 break;
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. */
1341 gobble_input ()
1343 int nread;
1344 if (interrupt_input)
1346 if (ioctl (0, FIONREAD, &nread) < 0)
1347 nread = 0;
1348 if (nread)
1350 sigholdx (SIGIO);
1351 input_available_signal (SIGIO);
1352 sigfree ();
1357 input_available_signal (signo)
1358 int signo;
1360 unsigned char buf[256 * BUFFER_SIZE_FACTOR];
1361 int nread;
1362 register int i;
1363 /* Must preserve main program's value of errno. */
1364 int old_errno = errno;
1365 #ifdef BSD4_1
1366 extern int select_alarmed;
1367 #endif
1369 #ifdef USG
1370 /* USG systems forget handlers when they are used;
1371 must reestablish each time */
1372 signal (signo, input_available_signal);
1373 #endif /* USG */
1375 #ifdef BSD4_1
1376 sigisheld (SIGIO);
1377 #endif
1379 if (input_available_clear_word)
1380 *input_available_clear_word = 0;
1382 while (1)
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);
1389 if (nread <= 0)
1390 break;
1391 #ifdef BSD4_1
1392 select_alarmed = 1; /* Force the select emulator back to life */
1393 #endif
1394 if (read_socket_hook)
1396 nread = (*read_socket_hook) (0, buf, sizeof buf);
1397 if (!nread)
1398 continue;
1400 else
1402 if (nread > sizeof buf)
1403 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)
1413 break;
1416 #ifdef BSD4_1
1417 sigfree ();
1418 #endif
1419 errno = old_errno;
1421 #endif /* SIGIO */
1423 #if 0
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)
1435 char *keybuf;
1437 register Lisp_Object map;
1438 register int c;
1439 register Lisp_Object tem;
1441 keys_prompt = 0;
1442 /* Read a character, and do not redisplay. */
1443 c = read_command_char (-1);
1444 Vquit_flag = Qnil;
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)
1453 return 0;
1455 map = current_buffer->keymap;
1456 if (!EQ (map, Qnil))
1458 tem = get_keyelt (access_keymap (map, c));
1459 if (!EQ (tem, Qnil))
1460 return 0;
1463 XSET (map, Lisp_Vector, global_map);
1464 tem = !NULL (map)
1465 ? get_keyelt (access_keymap (map, c))
1466 : Qnil;
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)))
1480 return 0;
1482 unread_command_char = -1;
1483 keybuf[0] = c;
1484 return 1;
1487 #endif /* 0 */
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)
1498 char *keybuf;
1499 int bufsize;
1500 unsigned char *prompt;
1501 int nodisplay;
1503 register int i;
1504 Lisp_Object nextlocal, nextglobal;
1505 register int c, nextc;
1506 Lisp_Object local, global;
1508 if (FROM_KBD)
1510 if (prompt)
1511 echo_prompt (prompt);
1512 else if (cursor_in_echo_area)
1513 echo_dash ();
1516 nextc = read_command_char (nodisplay ? -1 : !prompt);
1517 nextlocal = current_buffer->keymap;
1518 XSET (nextglobal, Lisp_Vector, global_map);
1520 i = 0;
1521 while (!NULL (nextlocal) || !NULL (nextglobal))
1523 if (i == bufsize)
1524 error ("key sequence too long");
1526 if (nextc >= 0)
1528 c = nextc;
1529 nextc = -1;
1531 else
1532 c = read_command_char (!prompt);
1533 Vquit_flag = Qnil;
1534 nodisplay = 0;
1536 if (c < 0)
1537 return 0;
1538 if (c >= 0200)
1540 nextc = c & 0177;
1541 c = meta_prefix_char;
1544 keybuf[i] = c;
1546 global = !NULL (nextglobal)
1547 ? get_keyelt (access_keymap (nextglobal, c))
1548 : Qnil;
1550 local = !NULL (nextlocal)
1551 ? get_keyelt (access_keymap (nextlocal, c))
1552 : Qnil;
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)))
1561 : Qnil;
1563 local = !NULL (nextlocal)
1564 ? get_keyelt (access_keymap (nextlocal, DOWNCASE (c)))
1565 : Qnil;
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);
1576 i++;
1578 nextlocal = Qnil;
1579 nextglobal = Qnil;
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)))
1608 nextlocal = local;
1609 else
1610 nextlocal = Qnil;
1612 if (XTYPE (global) == Lisp_Vector ||
1613 (CONSP (global) && EQ (XCONS (global)->car, Qkeymap)))
1614 nextglobal = global;
1615 else
1616 nextglobal = Qnil;
1620 return i;
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.")
1630 (prompt)
1631 Lisp_Object prompt;
1633 char keybuf[30];
1634 register int i;
1636 if (!NULL (prompt))
1637 CHECK_STRING (prompt, 0);
1638 QUIT;
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.")
1653 (cmd, record)
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);
1669 if (!NULL (tem))
1670 return call0 (Vdisabled_command_hook);
1673 while (1)
1675 final = cmd;
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);
1685 else
1686 break;
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;
1701 return tem;
1703 if (XTYPE (final) == Lisp_String)
1705 return Fexecute_kbd_macro (final, prefixarg);
1707 return Qnil;
1710 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
1711 1, 1, "P",
1712 "Read function name, then read its arguments and call it.")
1713 (prefixarg)
1714 Lisp_Object prefixarg;
1716 Lisp_Object function;
1717 char buf[40];
1718 Lisp_Object saved_keys;
1719 struct gcpro gcpro1;
1721 saved_keys = Fthis_command_keys ();
1722 GCPRO1 (saved_keys);
1724 buf[0] = 0;
1726 if (EQ (prefixarg, Qminus))
1727 strcpy (buf, "- ");
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;
1753 UNGCPRO;
1755 function = Fintern (function, Vobarray);
1756 Vprefix_arg = prefixarg;
1757 this_command = function;
1759 return Fcommand_execute (function, Qt);
1762 detect_input_pending ()
1764 if (!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 ()
1775 input_pending = 0;
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.")
1792 Lisp_Object val;
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);
1800 bcopy (recent_keys,
1801 XSTRING (val)->data + sizeof recent_keys - recent_keys_index,
1802 recent_keys_index);
1803 return val;
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.")
1817 Lisp_Object temp;
1818 XFASTINT (temp) = command_loop_level + minibuf_level;
1819 return temp;
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.")
1826 (file)
1827 Lisp_Object file;
1829 if (dribble != 0)
1830 fclose (dribble);
1831 dribble = 0;
1832 if (!NULL (file))
1834 file = Fexpand_file_name (file, Qnil);
1835 dribble = fopen (XSTRING (file)->data, "w");
1837 return Qnil;
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;
1849 Vquit_flag = Qnil;
1850 discard_tty_input ();
1852 kbd_count = 0;
1853 input_pending = 0;
1855 return Qnil;
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.")
1866 (stuffstring)
1867 Lisp_Object stuffstring;
1869 register Lisp_Object tem;
1870 int count = specpdl_ptr - specpdl;
1871 int old_height, old_width;
1872 int width, height;
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))
1886 tem = call0 (tem);
1887 if (!EQ (tem, Qnil)) return Qnil;
1890 get_screen_size (&old_width, &old_height);
1891 reset_sys_modes ();
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);
1896 sys_suspend ();
1897 unbind_to (count);
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))
1911 call0 (tem);
1912 UNGCPRO;
1913 return 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. */
1925 #ifdef BSD
1926 #ifndef BSD4_1
1927 if (XTYPE (stuffstring) == Lisp_String)
1929 register int count;
1931 p = XSTRING (stuffstring)->data;
1932 count = XSTRING (stuffstring)->size;
1933 while (count-- > 0)
1934 stuff_char (*p++);
1935 stuff_char ('\n');
1937 /* Anything we have read ahead, put back for the shell to read. */
1938 while (kbd_count)
1940 stuff_char (*kbd_ptr++);
1941 kbd_count--;
1943 input_pending = 0;
1944 #endif
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 */
1962 echo_now = 1;
1964 /* If alarm has gone off already, echo now. */
1965 if (echo_flag)
1967 echo ();
1968 echo_flag = 0;
1972 clear_waiting_for_input ()
1974 /* Tell interrupt_signal not to throw back to read_command_char, */
1975 waiting_for_input = 0;
1976 echo_now = 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. */
1992 interrupt_signal ()
1994 char c;
1995 /* Must preserve main program's value of errno. */
1996 int old_errno = errno;
1997 extern Lisp_Object Vwindow_system;
1999 #ifdef USG
2000 /* USG systems forget handlers when they are used;
2001 must reestablish each time */
2002 signal (SIGINT, interrupt_signal);
2003 signal (SIGQUIT, interrupt_signal);
2004 #endif /* USG */
2006 cancel_echoing ();
2008 if (!NULL (Vquit_flag) && NULL (Vwindow_system))
2010 fflush (stdout);
2011 reset_sys_modes ();
2012 sigfree ();
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.
2021 sys_suspend ();
2022 #else
2023 #ifdef VMS
2024 if (sys_suspend () == -1)
2026 printf ("Not running as a subprocess;\n");
2027 printf ("you can continue or abort.\n");
2029 #else /* not VMS */
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) ");
2038 fflush (stdout);
2039 if (((c = getchar ()) & ~040) == 'Y')
2040 Fdo_auto_save (Qnil);
2041 while (c != '\n') c = getchar ();
2042 #ifdef VMS
2043 printf ("Abort (and enter debugger)? (y or n) ");
2044 #else /* not VMS */
2045 printf ("Abort (and dump core)? (y or n) ");
2046 #endif /* not VMS */
2047 fflush (stdout);
2048 if (((c = getchar ()) & ~040) == 'Y')
2049 abort ();
2050 while (c != '\n') c = getchar ();
2051 printf ("Continuing...\n");
2052 fflush (stdout);
2053 init_sys_modes ();
2055 else
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))
2062 immediate_quit = 0;
2063 sigfree ();
2064 Fsignal (Qquit, Qnil);
2066 else
2067 /* Else request quit when it's safe */
2068 Vquit_flag = Qt;
2071 if (waiting_for_input && !echoing)
2072 quit_throw_to_read_command_char ();
2074 errno = old_errno;
2077 /* Handle a C-g by making read_command_char return C-g. */
2079 quit_throw_to_read_command_char ()
2081 quit_error_check ();
2082 sigfree ();
2083 /* Prevent another signal from doing this before we finish. */
2084 waiting_for_input = 0;
2085 input_pending = 0;
2086 unread_command_char = -1;
2087 #ifdef POLL_FOR_INPUT
2088 if (poll_suppress_count != 1)
2089 abort ();
2090 #endif
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;
2104 reset_sys_modes ();
2105 #ifdef SIGIO
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. */
2110 else
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);
2117 if (!NULL (quit))
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);
2124 init_sys_modes ();
2125 return Qnil;
2128 init_keyboard ()
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');
2135 immediate_quit = 0;
2136 unread_command_char = -1;
2137 recent_keys_index = 0;
2138 total_keys = 0;
2139 kbd_count = 0;
2140 kbd_ptr = kbd_buffer;
2141 input_pending = 0;
2142 force_input = 0;
2143 if (!noninteractive)
2145 signal (SIGINT, interrupt_signal);
2146 #ifdef HAVE_TERMIO
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. */
2152 #ifdef SIGIO
2153 signal (SIGIO, input_available_signal);
2154 #endif /* SIGIO */
2157 /* Use interrupt input by default, if it works and noninterrupt input
2158 has deficiencies. */
2160 #ifdef INTERRUPT_INPUT
2161 interrupt_input = 1;
2162 #else
2163 interrupt_input = 0;
2164 #endif
2166 sigfree ();
2167 dribble = 0;
2169 if (keyboard_init_hook)
2170 (*keyboard_init_hook) ();
2172 poll_suppress_count = 1;
2173 #ifdef POLL_FOR_INPUT
2174 start_polling ();
2175 #endif
2178 syms_of_keyboard ()
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.");
2255 polling_period = 2;
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.");
2267 Vhelp_form = Qnil;
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.");
2272 Vtop_level = Qnil;
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;
2281 keys_of_keyboard ()
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");