]> code.delx.au - gnu-emacs/blob - src/keyboard.c
*** empty log message ***
[gnu-emacs] / src / keyboard.c
1 /* Keyboard and mouse input; editor command loop.
2 Copyright (C) 1985,86,87,88,89,93,94 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
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 2, or (at your option)
9 any later version.
10
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.
15
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. */
19
20 /* Allow config.h to undefine symbols found here. */
21 #include <signal.h>
22
23 #include <config.h>
24 #include <stdio.h>
25 #undef NULL
26 #include "termchar.h"
27 #include "termopts.h"
28 #include "lisp.h"
29 #include "termhooks.h"
30 #include "macros.h"
31 #include "frame.h"
32 #include "window.h"
33 #include "commands.h"
34 #include "buffer.h"
35 #include "disptab.h"
36 #include "dispextern.h"
37 #include "keyboard.h"
38 #include "intervals.h"
39 #include "blockinput.h"
40 #include <setjmp.h>
41 #include <errno.h>
42
43 #ifdef MSDOS
44 #include "msdos.h"
45 #include <time.h>
46 #else /* not MSDOS */
47 #ifndef VMS
48 #include <sys/ioctl.h>
49 #endif
50 #endif /* not MSDOS */
51
52 #include "syssignal.h"
53 #include "systty.h"
54
55 /* This is to get the definitions of the XK_ symbols. */
56 #ifdef HAVE_X_WINDOWS
57 #include "xterm.h"
58 #endif
59
60 /* Include systime.h after xterm.h to avoid double inclusion of time.h. */
61 #include "systime.h"
62
63 extern int errno;
64
65 /* Variables for blockinput.h: */
66
67 /* Non-zero if interrupt input is blocked right now. */
68 int interrupt_input_blocked;
69
70 /* Nonzero means an input interrupt has arrived
71 during the current critical section. */
72 int interrupt_input_pending;
73
74
75 #ifdef HAVE_X_WINDOWS
76 extern Lisp_Object Vmouse_grabbed;
77
78 /* Make all keyboard buffers much bigger when using X windows. */
79 #define KBD_BUFFER_SIZE 4096
80 #else /* No X-windows, character input */
81 #define KBD_BUFFER_SIZE 256
82 #endif /* No X-windows */
83
84 /* Following definition copied from eval.c */
85
86 struct backtrace
87 {
88 struct backtrace *next;
89 Lisp_Object *function;
90 Lisp_Object *args; /* Points to vector of args. */
91 int nargs; /* length of vector. If nargs is UNEVALLED,
92 args points to slot holding list of
93 unevalled args */
94 char evalargs;
95 };
96
97 /* Non-nil disable property on a command means
98 do not execute it; call disabled-command-hook's value instead. */
99 Lisp_Object Qdisabled, Qdisabled_command_hook;
100
101 #define NUM_RECENT_KEYS (100)
102 int recent_keys_index; /* Index for storing next element into recent_keys */
103 int total_keys; /* Total number of elements stored into recent_keys */
104 Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
105
106 /* Vector holding the key sequence that invoked the current command.
107 It is reused for each command, and it may be longer than the current
108 sequence; this_command_key_count indicates how many elements
109 actually mean something.
110 It's easier to staticpro a single Lisp_Object than an array. */
111 Lisp_Object this_command_keys;
112 int this_command_key_count;
113
114 extern int minbuf_level;
115
116 extern struct backtrace *backtrace_list;
117
118 /* Nonzero means do menu prompting. */
119 static int menu_prompting;
120
121 /* Character to see next line of menu prompt. */
122 static Lisp_Object menu_prompt_more_char;
123
124 /* For longjmp to where kbd input is being done. */
125 static jmp_buf getcjmp;
126
127 /* True while doing kbd input. */
128 int waiting_for_input;
129
130 /* True while displaying for echoing. Delays C-g throwing. */
131 static int echoing;
132
133 /* Nonzero means disregard local maps for the menu bar. */
134 static int inhibit_local_menu_bar_menus;
135
136 /* Nonzero means C-g should cause immediate error-signal. */
137 int immediate_quit;
138
139 /* Character to recognize as the help char. */
140 Lisp_Object Vhelp_char;
141
142 /* Form to execute when help char is typed. */
143 Lisp_Object Vhelp_form;
144
145 /* Command to run when the help character follows a prefix key. */
146 Lisp_Object Vprefix_help_command;
147
148 /* List of items that should move to the end of the menu bar. */
149 Lisp_Object Vmenu_bar_final_items;
150
151 /* Character that causes a quit. Normally C-g.
152
153 If we are running on an ordinary terminal, this must be an ordinary
154 ASCII char, since we want to make it our interrupt character.
155
156 If we are not running on an ordinary terminal, it still needs to be
157 an ordinary ASCII char. This character needs to be recognized in
158 the input interrupt handler. At this point, the keystroke is
159 represented as a struct input_event, while the desired quit
160 character is specified as a lispy event. The mapping from struct
161 input_events to lispy events cannot run in an interrupt handler,
162 and the reverse mapping is difficult for anything but ASCII
163 keystrokes.
164
165 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
166 ASCII character. */
167 int quit_char;
168
169 extern Lisp_Object current_global_map;
170 extern int minibuf_level;
171
172 /* If non-nil, this is a map that overrides all other local maps. */
173 Lisp_Object Voverriding_local_map;
174
175 /* Current depth in recursive edits. */
176 int command_loop_level;
177
178 /* Total number of times command_loop has read a key sequence. */
179 int num_input_keys;
180
181 /* Last input character read as a command. */
182 Lisp_Object last_command_char;
183
184 /* Last input character read as a command, not counting menus
185 reached by the mouse. */
186 Lisp_Object last_nonmenu_event;
187
188 /* Last input character read for any purpose. */
189 Lisp_Object last_input_char;
190
191 /* If not Qnil, a list of objects to be read as subsequent command input. */
192 Lisp_Object Vunread_command_events;
193
194 /* If not -1, an event to be read as subsequent command input. */
195 int unread_command_char;
196
197 /* If not Qnil, this is a switch-frame event which we decided to put
198 off until the end of a key sequence. This should be read as the
199 next command input, after any unread_command_events.
200
201 read_key_sequence uses this to delay switch-frame events until the
202 end of the key sequence; Fread_char uses it to put off switch-frame
203 events until a non-ASCII event is acceptable as input. */
204 Lisp_Object unread_switch_frame;
205
206 /* A mask of extra modifier bits to put into every keyboard char. */
207 int extra_keyboard_modifiers;
208
209 /* Char to use as prefix when a meta character is typed in.
210 This is bound on entry to minibuffer in case ESC is changed there. */
211
212 Lisp_Object meta_prefix_char;
213
214 /* Last size recorded for a current buffer which is not a minibuffer. */
215 static int last_non_minibuf_size;
216
217 /* Number of idle seconds before an auto-save and garbage collection. */
218 static Lisp_Object Vauto_save_timeout;
219
220 /* Total number of times read_char has returned. */
221 int num_input_chars;
222
223 /* Total number of times read_char has returned, outside of macros. */
224 int num_nonmacro_input_chars;
225
226 /* Auto-save automatically when this many characters have been typed
227 since the last time. */
228
229 static int auto_save_interval;
230
231 /* Value of num_nonmacro_input_chars as of last auto save. */
232
233 int last_auto_save;
234
235 /* Last command executed by the editor command loop, not counting
236 commands that set the prefix argument. */
237
238 Lisp_Object last_command;
239
240 /* The command being executed by the command loop.
241 Commands may set this, and the value set will be copied into last_command
242 instead of the actual command. */
243 Lisp_Object this_command;
244
245 /* The value of point when the last command was executed. */
246 int last_point_position;
247
248 /* The buffer that was current when the last command was started. */
249 Lisp_Object last_point_position_buffer;
250
251 #ifdef MULTI_FRAME
252 /* The frame in which the last input event occurred, or Qmacro if the
253 last event came from a macro. We use this to determine when to
254 generate switch-frame events. This may be cleared by functions
255 like Fselect_frame, to make sure that a switch-frame event is
256 generated by the next character. */
257 Lisp_Object internal_last_event_frame;
258 #endif
259
260 /* A user-visible version of the above, intended to allow users to
261 figure out where the last event came from, if the event doesn't
262 carry that information itself (i.e. if it was a character). */
263 Lisp_Object Vlast_event_frame;
264
265 /* The timestamp of the last input event we received from the X server.
266 X Windows wants this for selection ownership. */
267 unsigned long last_event_timestamp;
268
269 Lisp_Object Qself_insert_command;
270 Lisp_Object Qforward_char;
271 Lisp_Object Qbackward_char;
272 Lisp_Object Qundefined;
273
274 /* read_key_sequence stores here the command definition of the
275 key sequence that it reads. */
276 Lisp_Object read_key_sequence_cmd;
277
278 /* Form to evaluate (if non-nil) when Emacs is started. */
279 Lisp_Object Vtop_level;
280
281 /* User-supplied string to translate input characters through. */
282 Lisp_Object Vkeyboard_translate_table;
283
284 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
285 extern Lisp_Object Vfunction_key_map;
286
287 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
288 Lisp_Object Vkey_translation_map;
289
290 /* Non-nil means deactivate the mark at end of this command. */
291 Lisp_Object Vdeactivate_mark;
292
293 /* Menu bar specified in Lucid Emacs fashion. */
294
295 Lisp_Object Vlucid_menu_bar_dirty_flag;
296 Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
297
298 /* Hooks to run before and after each command. */
299 Lisp_Object Qpre_command_hook, Qpost_command_hook;
300 Lisp_Object Vpre_command_hook, Vpost_command_hook;
301 Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
302
303 /* List of deferred actions to be performed at a later time.
304 The precise format isn't relevant here; we just check whether it is nil. */
305 Lisp_Object Vdeferred_action_list;
306
307 /* Function to call to handle deferred actions, when there are any. */
308 Lisp_Object Vdeferred_action_function;
309
310 /* File in which we write all commands we read. */
311 FILE *dribble;
312
313 /* Nonzero if input is available. */
314 int input_pending;
315
316 /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
317 keep 0200 bit in input chars. 0 to ignore the 0200 bit. */
318
319 int meta_key;
320
321 extern char *pending_malloc_warning;
322
323 /* Circular buffer for pre-read keyboard input. */
324 static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
325
326 /* Vector to GCPRO the frames and windows mentioned in kbd_buffer.
327
328 The interrupt-level event handlers will never enqueue an event on a
329 frame which is not in Vframe_list, and once an event is dequeued,
330 internal_last_event_frame or the event itself points to the frame.
331 So that's all fine.
332
333 But while the event is sitting in the queue, it's completely
334 unprotected. Suppose the user types one command which will run for
335 a while and then delete a frame, and then types another event at
336 the frame that will be deleted, before the command gets around to
337 it. Suppose there are no references to this frame elsewhere in
338 Emacs, and a GC occurs before the second event is dequeued. Now we
339 have an event referring to a freed frame, which will crash Emacs
340 when it is dequeued.
341
342 Similar things happen when an event on a scroll bar is enqueued; the
343 window may be deleted while the event is in the queue.
344
345 So, we use this vector to protect the frame_or_window field in the
346 event queue. That way, they'll be dequeued as dead frames or
347 windows, but still valid lisp objects.
348
349 If kbd_buffer[i].kind != no_event, then
350 (XVECTOR (kbd_buffer_frame_or_window)->contents[i]
351 == kbd_buffer[i].frame_or_window. */
352 static Lisp_Object kbd_buffer_frame_or_window;
353
354 /* Pointer to next available character in kbd_buffer.
355 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
356 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
357 next available char is in kbd_buffer[0]. */
358 static struct input_event *kbd_fetch_ptr;
359
360 /* Pointer to next place to store character in kbd_buffer. This
361 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
362 character should go in kbd_buffer[0]. */
363 static volatile struct input_event *kbd_store_ptr;
364
365 /* The above pair of variables forms a "queue empty" flag. When we
366 enqueue a non-hook event, we increment kbd_write_count. When we
367 dequeue a non-hook event, we increment kbd_read_count. We say that
368 there is input available iff the two counters are not equal.
369
370 Why not just have a flag set and cleared by the enqueuing and
371 dequeuing functions? Such a flag could be screwed up by interrupts
372 at inopportune times. */
373
374 /* If this flag is non-zero, we check mouse_moved to see when the
375 mouse moves, and motion events will appear in the input stream. If
376 it is zero, mouse motion is ignored. */
377 static int do_mouse_tracking;
378
379 /* The window system handling code should set this if the mouse has
380 moved since the last call to the mouse_position_hook. Calling that
381 hook should clear this. Code assumes that if this is set, it can
382 call mouse_position_hook to get the promised position, so don't set
383 it unless you're prepared to substantiate the claim! */
384 int mouse_moved;
385
386 /* True iff there is an event in kbd_buffer, or if mouse tracking is
387 enabled and there is a new mouse position in the mouse movement
388 buffer. Note that if this is false, that doesn't mean that there
389 is readable input; all the events in the queue might be button-up
390 events, and do_mouse_tracking might be off. */
391 #define EVENT_QUEUES_EMPTY \
392 ((kbd_fetch_ptr == kbd_store_ptr) && (!do_mouse_tracking || !mouse_moved))
393
394
395 /* Symbols to head events. */
396 Lisp_Object Qmouse_movement;
397 Lisp_Object Qscroll_bar_movement;
398 Lisp_Object Qswitch_frame;
399 Lisp_Object Qdelete_frame;
400
401 /* Symbols to denote kinds of events. */
402 Lisp_Object Qfunction_key;
403 Lisp_Object Qmouse_click;
404 /* Lisp_Object Qmouse_movement; - also an event header */
405
406 /* Properties of event headers. */
407 Lisp_Object Qevent_kind;
408 Lisp_Object Qevent_symbol_elements;
409
410 Lisp_Object Qmenu_enable;
411
412 /* An event header symbol HEAD may have a property named
413 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
414 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
415 mask of modifiers applied to it. If present, this is used to help
416 speed up parse_modifiers. */
417 Lisp_Object Qevent_symbol_element_mask;
418
419 /* An unmodified event header BASE may have a property named
420 Qmodifier_cache, which is an alist mapping modifier masks onto
421 modified versions of BASE. If present, this helps speed up
422 apply_modifiers. */
423 Lisp_Object Qmodifier_cache;
424
425 /* Symbols to use for parts of windows. */
426 Lisp_Object Qmode_line;
427 Lisp_Object Qvertical_line;
428 Lisp_Object Qvertical_scroll_bar;
429 Lisp_Object Qmenu_bar;
430
431 extern Lisp_Object Qmenu_enable;
432
433 Lisp_Object recursive_edit_unwind (), command_loop ();
434 Lisp_Object Fthis_command_keys ();
435 Lisp_Object Qextended_command_history;
436
437 Lisp_Object Qpolling_period;
438
439 /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
440 happens. */
441 EMACS_TIME *input_available_clear_time;
442
443 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
444 Default is 1 if INTERRUPT_INPUT is defined. */
445 int interrupt_input;
446
447 /* Nonzero while interrupts are temporarily deferred during redisplay. */
448 int interrupts_deferred;
449
450 /* nonzero means use ^S/^Q for flow control. */
451 int flow_control;
452
453 /* Allow m- file to inhibit use of FIONREAD. */
454 #ifdef BROKEN_FIONREAD
455 #undef FIONREAD
456 #endif
457
458 /* We are unable to use interrupts if FIONREAD is not available,
459 so flush SIGIO so we won't try. */
460 #ifndef FIONREAD
461 #ifdef SIGIO
462 #undef SIGIO
463 #endif
464 #endif
465
466 /* If we support X Windows, turn on the code to poll periodically
467 to detect C-g. It isn't actually used when doing interrupt input. */
468 #ifdef HAVE_X_WINDOWS
469 #define POLL_FOR_INPUT
470 #endif
471 \f
472 /* Global variable declarations. */
473
474 /* Function for init_keyboard to call with no args (if nonzero). */
475 void (*keyboard_init_hook) ();
476
477 static int read_avail_input ();
478 static void get_input_pending ();
479 static int readable_events ();
480 static Lisp_Object read_char_x_menu_prompt ();
481 static Lisp_Object read_char_minibuf_menu_prompt ();
482 static Lisp_Object make_lispy_event ();
483 static Lisp_Object make_lispy_movement ();
484 static Lisp_Object modify_event_symbol ();
485 static Lisp_Object make_lispy_switch_frame ();
486
487 /* > 0 if we are to echo keystrokes. */
488 static int echo_keystrokes;
489
490 /* Nonzero means echo each character as typed. */
491 static int immediate_echo;
492
493 /* The text we're echoing in the modeline - partial key sequences,
494 usually. '\0'-terminated. This really shouldn't have a fixed size. */
495 static char echobuf[300];
496
497 /* Where to append more text to echobuf if we want to. */
498 static char *echoptr;
499
500 /* If we have echoed a prompt string specified by the user,
501 this is its length. Otherwise this is -1. */
502 static int echo_after_prompt;
503
504 /* Nonzero means don't try to suspend even if the operating system seems
505 to support it. */
506 static int cannot_suspend;
507
508 #define min(a,b) ((a)<(b)?(a):(b))
509 #define max(a,b) ((a)>(b)?(a):(b))
510
511 /* Install the string STR as the beginning of the string of echoing,
512 so that it serves as a prompt for the next character.
513 Also start echoing. */
514
515 echo_prompt (str)
516 char *str;
517 {
518 int len = strlen (str);
519
520 if (len > sizeof echobuf - 4)
521 len = sizeof echobuf - 4;
522 bcopy (str, echobuf, len);
523 echoptr = echobuf + len;
524 *echoptr = '\0';
525
526 echo_after_prompt = len;
527
528 echo ();
529 }
530
531 /* Add C to the echo string, if echoing is going on.
532 C can be a character, which is printed prettily ("M-C-x" and all that
533 jazz), or a symbol, whose name is printed. */
534
535 echo_char (c)
536 Lisp_Object c;
537 {
538 extern char *push_key_description ();
539
540 if (immediate_echo)
541 {
542 char *ptr = echoptr;
543
544 if (ptr != echobuf)
545 *ptr++ = ' ';
546
547 /* If someone has passed us a composite event, use its head symbol. */
548 c = EVENT_HEAD (c);
549
550 if (INTEGERP (c))
551 {
552 if (ptr - echobuf > sizeof echobuf - 6)
553 return;
554
555 ptr = push_key_description (XINT (c), ptr);
556 }
557 else if (SYMBOLP (c))
558 {
559 struct Lisp_String *name = XSYMBOL (c)->name;
560 if (((ptr - echobuf) + name->size + 4) > sizeof echobuf)
561 return;
562 bcopy (name->data, ptr, name->size);
563 ptr += name->size;
564 }
565
566 if (echoptr == echobuf && EQ (c, Vhelp_char))
567 {
568 strcpy (ptr, " (Type ? for further options)");
569 ptr += strlen (ptr);
570 }
571
572 *ptr = 0;
573 echoptr = ptr;
574
575 echo ();
576 }
577 }
578
579 /* Temporarily add a dash to the end of the echo string if it's not
580 empty, so that it serves as a mini-prompt for the very next character. */
581
582 echo_dash ()
583 {
584 if (!immediate_echo && echoptr == echobuf)
585 return;
586 /* Do nothing if we just printed a prompt. */
587 if (echo_after_prompt == echoptr - echobuf)
588 return;
589 /* Do nothing if not echoing at all. */
590 if (echoptr == 0)
591 return;
592
593 /* Put a dash at the end of the buffer temporarily,
594 but make it go away when the next character is added. */
595 echoptr[0] = '-';
596 echoptr[1] = 0;
597
598 echo ();
599 }
600
601 /* Display the current echo string, and begin echoing if not already
602 doing so. */
603
604 echo ()
605 {
606 if (!immediate_echo)
607 {
608 int i;
609 immediate_echo = 1;
610
611 for (i = 0; i < this_command_key_count; i++)
612 {
613 Lisp_Object c;
614 c = XVECTOR (this_command_keys)->contents[i];
615 if (! (EVENT_HAS_PARAMETERS (c)
616 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
617 echo_char (c);
618 }
619 echo_dash ();
620 }
621
622 echoing = 1;
623 message1 (echobuf);
624 echoing = 0;
625
626 if (waiting_for_input && !NILP (Vquit_flag))
627 quit_throw_to_read_char ();
628 }
629
630 /* Turn off echoing, for the start of a new command. */
631
632 cancel_echoing ()
633 {
634 immediate_echo = 0;
635 echoptr = echobuf;
636 echo_after_prompt = -1;
637 }
638
639 /* Return the length of the current echo string. */
640
641 static int
642 echo_length ()
643 {
644 return echoptr - echobuf;
645 }
646
647 /* Truncate the current echo message to its first LEN chars.
648 This and echo_char get used by read_key_sequence when the user
649 switches frames while entering a key sequence. */
650
651 static void
652 echo_truncate (len)
653 int len;
654 {
655 echobuf[len] = '\0';
656 echoptr = echobuf + len;
657 truncate_echo_area (len);
658 }
659
660 \f
661 /* Functions for manipulating this_command_keys. */
662 static void
663 add_command_key (key)
664 Lisp_Object key;
665 {
666 int size = XVECTOR (this_command_keys)->size;
667
668 if (this_command_key_count >= size)
669 {
670 Lisp_Object new_keys;
671
672 new_keys = Fmake_vector (make_number (size * 2), Qnil);
673 bcopy (XVECTOR (this_command_keys)->contents,
674 XVECTOR (new_keys)->contents,
675 size * sizeof (Lisp_Object));
676
677 this_command_keys = new_keys;
678 }
679
680 XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
681 }
682 \f
683 Lisp_Object
684 recursive_edit_1 ()
685 {
686 int count = specpdl_ptr - specpdl;
687 Lisp_Object val;
688
689 if (command_loop_level > 0)
690 {
691 specbind (Qstandard_output, Qt);
692 specbind (Qstandard_input, Qt);
693 }
694
695 val = command_loop ();
696 if (EQ (val, Qt))
697 Fsignal (Qquit, Qnil);
698
699 return unbind_to (count, Qnil);
700 }
701
702 /* When an auto-save happens, record the "time", and don't do again soon. */
703
704 record_auto_save ()
705 {
706 last_auto_save = num_nonmacro_input_chars;
707 }
708
709 /* Make an auto save happen as soon as possible at command level. */
710
711 force_auto_save_soon ()
712 {
713 last_auto_save = - auto_save_interval - 1;
714
715 record_asynch_buffer_change ();
716 }
717 \f
718 DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
719 "Invoke the editor command loop recursively.\n\
720 To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
721 that tells this function to return.\n\
722 Alternately, `(throw 'exit t)' makes this function signal an error.\n\
723 This function is called by the editor initialization to begin editing.")
724 ()
725 {
726 int count = specpdl_ptr - specpdl;
727 Lisp_Object val;
728
729 command_loop_level++;
730 update_mode_lines = 1;
731
732 record_unwind_protect (recursive_edit_unwind,
733 (command_loop_level
734 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
735 ? Fcurrent_buffer ()
736 : Qnil);
737 recursive_edit_1 ();
738 return unbind_to (count, Qnil);
739 }
740
741 Lisp_Object
742 recursive_edit_unwind (buffer)
743 Lisp_Object buffer;
744 {
745 if (!NILP (buffer))
746 Fset_buffer (buffer);
747
748 command_loop_level--;
749 update_mode_lines = 1;
750 return Qnil;
751 }
752 \f
753 Lisp_Object
754 cmd_error (data)
755 Lisp_Object data;
756 {
757 Vstandard_output = Qt;
758 Vstandard_input = Qt;
759 Vexecuting_macro = Qnil;
760 cmd_error_internal (data, 0);
761
762 Vquit_flag = Qnil;
763
764 Vinhibit_quit = Qnil;
765
766 return make_number (0);
767 }
768
769 cmd_error_internal (data, context)
770 Lisp_Object data;
771 char *context;
772 {
773 Lisp_Object errmsg, tail, errname, file_error;
774 Lisp_Object stream;
775 struct gcpro gcpro1;
776 int i;
777
778 Vquit_flag = Qnil;
779 Vinhibit_quit = Qt;
780 echo_area_glyphs = 0;
781
782 /* If the window system or terminal frame hasn't been initialized
783 yet, or we're not interactive, it's best to dump this message out
784 to stderr and exit. */
785 if (! FRAME_MESSAGE_BUF (selected_frame)
786 || noninteractive)
787 stream = Qexternal_debugging_output;
788 else
789 {
790 Fdiscard_input ();
791 bitch_at_user ();
792 stream = Qt;
793 }
794
795 if (context != 0)
796 write_string_1 (context, -1, stream);
797
798 errname = Fcar (data);
799
800 if (EQ (errname, Qerror))
801 {
802 data = Fcdr (data);
803 if (!CONSP (data)) data = Qnil;
804 errmsg = Fcar (data);
805 file_error = Qnil;
806 }
807 else
808 {
809 errmsg = Fget (errname, Qerror_message);
810 file_error = Fmemq (Qfile_error,
811 Fget (errname, Qerror_conditions));
812 }
813
814 /* Print an error message including the data items.
815 This is done by printing it into a scratch buffer
816 and then making a copy of the text in the buffer. */
817
818 if (!CONSP (data)) data = Qnil;
819 tail = Fcdr (data);
820 GCPRO1 (tail);
821
822 /* For file-error, make error message by concatenating
823 all the data items. They are all strings. */
824 if (!NILP (file_error) && !NILP (tail))
825 errmsg = XCONS (tail)->car, tail = XCONS (tail)->cdr;
826
827 if (STRINGP (errmsg))
828 Fprinc (errmsg, stream);
829 else
830 write_string_1 ("peculiar error", -1, stream);
831
832 for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
833 {
834 write_string_1 (i ? ", " : ": ", 2, stream);
835 if (!NILP (file_error))
836 Fprinc (Fcar (tail), stream);
837 else
838 Fprin1 (Fcar (tail), stream);
839 }
840 UNGCPRO;
841
842 /* If the window system or terminal frame hasn't been initialized
843 yet, or we're in -batch mode, this error should cause Emacs to exit. */
844 if (! FRAME_MESSAGE_BUF (selected_frame)
845 || noninteractive)
846 {
847 Fterpri (stream);
848 Fkill_emacs (make_number (-1));
849 }
850 }
851 \f
852 Lisp_Object command_loop_1 ();
853 Lisp_Object command_loop_2 ();
854 Lisp_Object top_level_1 ();
855
856 /* Entry to editor-command-loop.
857 This level has the catches for exiting/returning to editor command loop.
858 It returns nil to exit recursive edit, t to abort it. */
859
860 Lisp_Object
861 command_loop ()
862 {
863 if (command_loop_level > 0 || minibuf_level > 0)
864 {
865 return internal_catch (Qexit, command_loop_2, Qnil);
866 }
867 else
868 while (1)
869 {
870 internal_catch (Qtop_level, top_level_1, Qnil);
871 internal_catch (Qtop_level, command_loop_2, Qnil);
872
873 /* End of file in -batch run causes exit here. */
874 if (noninteractive)
875 Fkill_emacs (Qt);
876 }
877 }
878
879 /* Here we catch errors in execution of commands within the
880 editing loop, and reenter the editing loop.
881 When there is an error, cmd_error runs and returns a non-nil
882 value to us. A value of nil means that cmd_loop_1 itself
883 returned due to end of file (or end of kbd macro). */
884
885 Lisp_Object
886 command_loop_2 ()
887 {
888 register Lisp_Object val;
889
890 do
891 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
892 while (!NILP (val));
893
894 return Qnil;
895 }
896
897 Lisp_Object
898 top_level_2 ()
899 {
900 return Feval (Vtop_level);
901 }
902
903 Lisp_Object
904 top_level_1 ()
905 {
906 /* On entry to the outer level, run the startup file */
907 if (!NILP (Vtop_level))
908 internal_condition_case (top_level_2, Qerror, cmd_error);
909 else if (!NILP (Vpurify_flag))
910 message ("Bare impure Emacs (standard Lisp code not loaded)");
911 else
912 message ("Bare Emacs (standard Lisp code not loaded)");
913 return Qnil;
914 }
915
916 DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
917 "Exit all recursive editing levels.")
918 ()
919 {
920 Fthrow (Qtop_level, Qnil);
921 }
922
923 DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
924 "Exit from the innermost recursive edit or minibuffer.")
925 ()
926 {
927 if (command_loop_level > 0 || minibuf_level > 0)
928 Fthrow (Qexit, Qnil);
929
930 error ("No recursive edit is in progress");
931 }
932
933 DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
934 "Abort the command that requested this recursive edit or minibuffer input.")
935 ()
936 {
937 if (command_loop_level > 0 || minibuf_level > 0)
938 Fthrow (Qexit, Qt);
939
940 error ("No recursive edit is in progress");
941 }
942 \f
943 /* This is the actual command reading loop,
944 sans error-handling encapsulation. */
945
946 Lisp_Object Fcommand_execute ();
947 static int read_key_sequence ();
948 static void safe_run_hooks ();
949
950 Lisp_Object
951 command_loop_1 ()
952 {
953 Lisp_Object cmd, tem;
954 int lose;
955 int nonundocount;
956 Lisp_Object keybuf[30];
957 int i;
958 int no_redisplay;
959 int no_direct;
960 int prev_modiff;
961 struct buffer *prev_buffer;
962
963 Vprefix_arg = Qnil;
964 Vdeactivate_mark = Qnil;
965 waiting_for_input = 0;
966 cancel_echoing ();
967
968 nonundocount = 0;
969 no_redisplay = 0;
970 this_command_key_count = 0;
971
972 /* Make sure this hook runs after commands that get errors and
973 throw to top level. */
974 /* Note that the value cell will never directly contain nil
975 if the symbol is a local variable. */
976 if (!NILP (XSYMBOL (Qpost_command_hook)->value) && !NILP (Vrun_hooks))
977 safe_run_hooks (Qpost_command_hook);
978
979 if (!NILP (Vdeferred_action_list))
980 call0 (Vdeferred_action_function);
981
982 /* Do this after running Vpost_command_hook, for consistency. */
983 last_command = this_command;
984
985 while (1)
986 {
987 /* Install chars successfully executed in kbd macro. */
988
989 if (defining_kbd_macro && NILP (Vprefix_arg))
990 finalize_kbd_macro_chars ();
991
992 /* Make sure the current window's buffer is selected. */
993 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
994 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
995
996 /* Display any malloc warning that just came out. Use while because
997 displaying one warning can cause another. */
998
999 while (pending_malloc_warning)
1000 display_malloc_warning ();
1001
1002 no_direct = 0;
1003
1004 Vdeactivate_mark = Qnil;
1005
1006 /* If minibuffer on and echo area in use,
1007 wait 2 sec and redraw minibuffer. */
1008
1009 if (minibuf_level && echo_area_glyphs)
1010 {
1011 /* Bind inhibit-quit to t so that C-g gets read in
1012 rather than quitting back to the minibuffer. */
1013 int count = specpdl_ptr - specpdl;
1014 specbind (Qinhibit_quit, Qt);
1015 Fsit_for (make_number (2), Qnil, Qnil);
1016 unbind_to (count, Qnil);
1017
1018 echo_area_glyphs = 0;
1019 no_direct = 1;
1020 if (!NILP (Vquit_flag))
1021 {
1022 Vquit_flag = Qnil;
1023 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1024 }
1025 }
1026
1027 #ifdef C_ALLOCA
1028 alloca (0); /* Cause a garbage collection now */
1029 /* Since we can free the most stuff here. */
1030 #endif /* C_ALLOCA */
1031
1032 #if 0
1033 #ifdef MULTI_FRAME
1034 /* Select the frame that the last event came from. Usually,
1035 switch-frame events will take care of this, but if some lisp
1036 code swallows a switch-frame event, we'll fix things up here.
1037 Is this a good idea? */
1038 if (FRAMEP (internal_last_event_frame)
1039 && XFRAME (internal_last_event_frame) != selected_frame)
1040 Fselect_frame (internal_last_event_frame, Qnil);
1041 #endif
1042 #endif
1043 /* If it has changed current-menubar from previous value,
1044 really recompute the menubar from the value. */
1045 if (! NILP (Vlucid_menu_bar_dirty_flag)
1046 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
1047 call0 (Qrecompute_lucid_menubar);
1048
1049 /* Read next key sequence; i gets its length. */
1050 i = read_key_sequence (keybuf, (sizeof keybuf / sizeof (keybuf[0])), Qnil);
1051
1052 ++num_input_keys;
1053
1054 /* Now we have read a key sequence of length I,
1055 or else I is 0 and we found end of file. */
1056
1057 if (i == 0) /* End of file -- happens only in */
1058 return Qnil; /* a kbd macro, at the end. */
1059 /* -1 means read_key_sequence got a menu that was rejected.
1060 Just loop around and read another command. */
1061 if (i == -1)
1062 {
1063 cancel_echoing ();
1064 this_command_key_count = 0;
1065 continue;
1066 }
1067
1068 last_command_char = keybuf[i - 1];
1069
1070 /* If the previous command tried to force a specific window-start,
1071 forget about that, in case this command moves point far away
1072 from that position. */
1073 XWINDOW (selected_window)->force_start = Qnil;
1074
1075 cmd = read_key_sequence_cmd;
1076 if (!NILP (Vexecuting_macro))
1077 {
1078 if (!NILP (Vquit_flag))
1079 {
1080 Vexecuting_macro = Qt;
1081 QUIT; /* Make some noise. */
1082 /* Will return since macro now empty. */
1083 }
1084 }
1085
1086 /* Do redisplay processing after this command except in special
1087 cases identified below that set no_redisplay to 1.
1088 (actually, there's currently no way to prevent the redisplay,
1089 and no_redisplay is ignored.
1090 Perhaps someday we will really implement it. */
1091 no_redisplay = 0;
1092
1093 prev_buffer = current_buffer;
1094 prev_modiff = MODIFF;
1095 last_point_position = PT;
1096 XSETBUFFER (last_point_position_buffer, prev_buffer);
1097
1098 /* Execute the command. */
1099
1100 this_command = cmd;
1101 /* Note that the value cell will never directly contain nil
1102 if the symbol is a local variable. */
1103 if (!NILP (XSYMBOL (Qpre_command_hook)->value) && !NILP (Vrun_hooks))
1104 safe_run_hooks (Qpre_command_hook);
1105
1106 if (NILP (this_command))
1107 {
1108 /* nil means key is undefined. */
1109 bitch_at_user ();
1110 defining_kbd_macro = 0;
1111 update_mode_lines = 1;
1112 Vprefix_arg = Qnil;
1113
1114 }
1115 else
1116 {
1117 if (NILP (Vprefix_arg) && ! no_direct)
1118 {
1119 /* Recognize some common commands in common situations and
1120 do them directly. */
1121 if (EQ (this_command, Qforward_char) && PT < ZV)
1122 {
1123 struct Lisp_Vector *dp
1124 = window_display_table (XWINDOW (selected_window));
1125 lose = FETCH_CHAR (PT);
1126 SET_PT (PT + 1);
1127 if ((dp
1128 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1129 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1130 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1131 && (lose >= 0x20 && lose < 0x7f)))
1132 : (lose >= 0x20 && lose < 0x7f))
1133 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1134 >= MODIFF)
1135 && (XFASTINT (XWINDOW (selected_window)->last_point)
1136 == PT - 1)
1137 && !windows_or_buffers_changed
1138 && EQ (current_buffer->selective_display, Qnil)
1139 && !detect_input_pending ()
1140 && NILP (Vexecuting_macro))
1141 no_redisplay = direct_output_forward_char (1);
1142 goto directly_done;
1143 }
1144 else if (EQ (this_command, Qbackward_char) && PT > BEGV)
1145 {
1146 struct Lisp_Vector *dp
1147 = window_display_table (XWINDOW (selected_window));
1148 SET_PT (PT - 1);
1149 lose = FETCH_CHAR (PT);
1150 if ((dp
1151 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1152 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1153 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1154 && (lose >= 0x20 && lose < 0x7f)))
1155 : (lose >= 0x20 && lose < 0x7f))
1156 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1157 >= MODIFF)
1158 && (XFASTINT (XWINDOW (selected_window)->last_point)
1159 == PT + 1)
1160 && !windows_or_buffers_changed
1161 && EQ (current_buffer->selective_display, Qnil)
1162 && !detect_input_pending ()
1163 && NILP (Vexecuting_macro))
1164 no_redisplay = direct_output_forward_char (-1);
1165 goto directly_done;
1166 }
1167 else if (EQ (this_command, Qself_insert_command)
1168 /* Try this optimization only on ascii keystrokes. */
1169 && INTEGERP (last_command_char))
1170 {
1171 unsigned char c = XINT (last_command_char);
1172 int value;
1173
1174 if (NILP (Vexecuting_macro)
1175 && !EQ (minibuf_window, selected_window))
1176 {
1177 if (!nonundocount || nonundocount >= 20)
1178 {
1179 Fundo_boundary ();
1180 nonundocount = 0;
1181 }
1182 nonundocount++;
1183 }
1184 lose = ((XFASTINT (XWINDOW (selected_window)->last_modified)
1185 < MODIFF)
1186 || (XFASTINT (XWINDOW (selected_window)->last_point)
1187 != PT)
1188 || MODIFF <= current_buffer->save_modified
1189 || windows_or_buffers_changed
1190 || !EQ (current_buffer->selective_display, Qnil)
1191 || detect_input_pending ()
1192 || !NILP (Vexecuting_macro));
1193 value = internal_self_insert (c, 0);
1194 if (value)
1195 lose = 1;
1196 if (value == 2)
1197 nonundocount = 0;
1198
1199 if (!lose
1200 && (PT == ZV || FETCH_CHAR (PT) == '\n'))
1201 {
1202 struct Lisp_Vector *dp
1203 = window_display_table (XWINDOW (selected_window));
1204 int lose = c;
1205
1206 if (dp)
1207 {
1208 Lisp_Object obj;
1209
1210 obj = DISP_CHAR_VECTOR (dp, lose);
1211 if (NILP (obj))
1212 {
1213 /* Do it only for char codes
1214 that by default display as themselves. */
1215 if (lose >= 0x20 && lose <= 0x7e)
1216 no_redisplay = direct_output_for_insert (lose);
1217 }
1218 else if (VECTORP (obj)
1219 && XVECTOR (obj)->size == 1
1220 && (obj = XVECTOR (obj)->contents[0],
1221 INTEGERP (obj))
1222 /* Insist face not specified in glyph. */
1223 && (XINT (obj) & ((-1) << 8)) == 0)
1224 no_redisplay
1225 = direct_output_for_insert (XINT (obj));
1226 }
1227 else
1228 {
1229 if (lose >= 0x20 && lose <= 0x7e)
1230 no_redisplay = direct_output_for_insert (lose);
1231 }
1232 }
1233 goto directly_done;
1234 }
1235 }
1236
1237 /* Here for a command that isn't executed directly */
1238
1239 nonundocount = 0;
1240 if (NILP (Vprefix_arg))
1241 Fundo_boundary ();
1242 Fcommand_execute (this_command, Qnil);
1243
1244 }
1245 directly_done: ;
1246
1247 /* Note that the value cell will never directly contain nil
1248 if the symbol is a local variable. */
1249 if (!NILP (XSYMBOL (Qpost_command_hook)->value) && !NILP (Vrun_hooks))
1250 safe_run_hooks (Qpost_command_hook);
1251
1252 if (!NILP (Vdeferred_action_list))
1253 call0 (Vdeferred_action_function);
1254
1255 /* If there is a prefix argument,
1256 1) We don't want last_command to be ``universal-argument''
1257 (that would be dumb), so don't set last_command,
1258 2) we want to leave echoing on so that the prefix will be
1259 echoed as part of this key sequence, so don't call
1260 cancel_echoing, and
1261 3) we want to leave this_command_key_count non-zero, so that
1262 read_char will realize that it is re-reading a character, and
1263 not echo it a second time. */
1264 if (NILP (Vprefix_arg))
1265 {
1266 last_command = this_command;
1267 cancel_echoing ();
1268 this_command_key_count = 0;
1269 }
1270
1271 if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
1272 {
1273 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1274 {
1275 current_buffer->mark_active = Qnil;
1276 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1277 }
1278 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1279 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1280 }
1281 }
1282 }
1283
1284 /* If we get an error while running the hook, cause the hook variable
1285 to be nil. Also inhibit quits, so that C-g won't cause the hook
1286 to mysteriously evaporate. */
1287 static void
1288 safe_run_hooks (hook)
1289 Lisp_Object hook;
1290 {
1291 Lisp_Object value;
1292 int count = specpdl_ptr - specpdl;
1293 specbind (Qinhibit_quit, Qt);
1294
1295 /* We read and set the variable with functions,
1296 in case it's buffer-local. */
1297 value = Vcommand_hook_internal = Fsymbol_value (hook);
1298 Fset (hook, Qnil);
1299 call1 (Vrun_hooks, Qcommand_hook_internal);
1300 Fset (hook, value);
1301
1302 unbind_to (count, Qnil);
1303 }
1304 \f
1305 /* Number of seconds between polling for input. */
1306 int polling_period;
1307
1308 /* Nonzero means polling for input is temporarily suppressed. */
1309 int poll_suppress_count;
1310
1311 /* Nonzero if polling_for_input is actually being used. */
1312 int polling_for_input;
1313
1314 #ifdef POLL_FOR_INPUT
1315
1316 /* Handle an alarm once each second and read pending input
1317 so as to handle a C-g if it comces in. */
1318
1319 SIGTYPE
1320 input_poll_signal ()
1321 {
1322 if (interrupt_input_blocked == 0
1323 && !waiting_for_input)
1324 read_avail_input (0);
1325 signal (SIGALRM, input_poll_signal);
1326 alarm (polling_period);
1327 }
1328
1329 #endif
1330
1331 /* Begin signals to poll for input, if they are appropriate.
1332 This function is called unconditionally from various places. */
1333
1334 start_polling ()
1335 {
1336 #ifdef POLL_FOR_INPUT
1337 if (read_socket_hook && !interrupt_input)
1338 {
1339 poll_suppress_count--;
1340 if (poll_suppress_count == 0)
1341 {
1342 signal (SIGALRM, input_poll_signal);
1343 polling_for_input = 1;
1344 alarm (polling_period);
1345 }
1346 }
1347 #endif
1348 }
1349
1350 /* Nonzero if we are using polling to handle input asynchronously. */
1351
1352 int
1353 input_polling_used ()
1354 {
1355 #ifdef POLL_FOR_INPUT
1356 return read_socket_hook && !interrupt_input;
1357 #else
1358 return 0;
1359 #endif
1360 }
1361
1362 /* Turn off polling. */
1363
1364 stop_polling ()
1365 {
1366 #ifdef POLL_FOR_INPUT
1367 if (read_socket_hook && !interrupt_input)
1368 {
1369 if (poll_suppress_count == 0)
1370 {
1371 polling_for_input = 0;
1372 alarm (0);
1373 }
1374 poll_suppress_count++;
1375 }
1376 #endif
1377 }
1378
1379 /* Set the value of poll_suppress_count to COUNT
1380 and start or stop polling accordingly. */
1381
1382 void
1383 set_poll_suppress_count (count)
1384 int count;
1385 {
1386 #ifdef POLL_FOR_INPUT
1387 if (count == 0 && poll_suppress_count != 0)
1388 {
1389 poll_suppress_count = 1;
1390 start_polling ();
1391 }
1392 else if (count != 0 && poll_suppress_count == 0)
1393 {
1394 stop_polling ();
1395 }
1396 poll_suppress_count = count;
1397 #endif
1398 }
1399
1400 /* Bind polling_period to a value at least N.
1401 But don't decrease it. */
1402
1403 bind_polling_period (n)
1404 int n;
1405 {
1406 #ifdef POLL_FOR_INPUT
1407 int new = polling_period;
1408
1409 if (n > new)
1410 new = n;
1411
1412 stop_polling ();
1413 specbind (Qpolling_period, make_number (new));
1414 /* Start a new alarm with the new period. */
1415 start_polling ();
1416 #endif
1417 }
1418 \f
1419 /* Applying the control modifier to CHARACTER. */
1420 int
1421 make_ctrl_char (c)
1422 int c;
1423 {
1424 /* Save the upper bits here. */
1425 int upper = c & ~0177;
1426
1427 c &= 0177;
1428
1429 /* Everything in the columns containing the upper-case letters
1430 denotes a control character. */
1431 if (c >= 0100 && c < 0140)
1432 {
1433 int oc = c;
1434 c &= ~0140;
1435 /* Set the shift modifier for a control char
1436 made from a shifted letter. But only for letters! */
1437 if (oc >= 'A' && oc <= 'Z')
1438 c |= shift_modifier;
1439 }
1440
1441 /* The lower-case letters denote control characters too. */
1442 else if (c >= 'a' && c <= 'z')
1443 c &= ~0140;
1444
1445 /* Include the bits for control and shift
1446 only if the basic ASCII code can't indicate them. */
1447 else if (c >= ' ')
1448 c |= ctrl_modifier;
1449
1450 /* Replace the high bits. */
1451 c |= (upper & ~ctrl_modifier);
1452
1453 return c;
1454 }
1455
1456
1457 \f
1458 /* Input of single characters from keyboard */
1459
1460 Lisp_Object print_help ();
1461 static Lisp_Object kbd_buffer_get_event ();
1462
1463 /* read a character from the keyboard; call the redisplay if needed */
1464 /* commandflag 0 means do not do auto-saving, but do do redisplay.
1465 -1 means do not do redisplay, but do do autosaving.
1466 1 means do both. */
1467
1468 /* The arguments MAPS and NMAPS are for menu prompting.
1469 MAPS is an array of keymaps; NMAPS is the length of MAPS.
1470
1471 PREV_EVENT is the previous input event, or nil if we are reading
1472 the first event of a key sequence.
1473
1474 If USED_MOUSE_MENU is non-zero, then we set *USED_MOUSE_MENU to 1
1475 if we used a mouse menu to read the input, or zero otherwise. If
1476 USED_MOUSE_MENU is zero, *USED_MOUSE_MENU is left alone.
1477
1478 Value is t if we showed a menu and the user rejected it. */
1479
1480 Lisp_Object
1481 read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
1482 int commandflag;
1483 int nmaps;
1484 Lisp_Object *maps;
1485 Lisp_Object prev_event;
1486 int *used_mouse_menu;
1487 {
1488 register Lisp_Object c;
1489 int count;
1490 jmp_buf save_jump;
1491 int key_already_recorded = 0;
1492
1493 if (CONSP (Vunread_command_events))
1494 {
1495 c = XCONS (Vunread_command_events)->car;
1496 Vunread_command_events = XCONS (Vunread_command_events)->cdr;
1497
1498 if (this_command_key_count == 0)
1499 goto reread_first;
1500 else
1501 goto reread;
1502 }
1503
1504 if (unread_command_char != -1)
1505 {
1506 XSETINT (c, unread_command_char);
1507 unread_command_char = -1;
1508
1509 if (this_command_key_count == 0)
1510 goto reread_first;
1511 else
1512 goto reread;
1513 }
1514
1515 if (!NILP (Vexecuting_macro))
1516 {
1517 #ifdef MULTI_FRAME
1518 /* We set this to Qmacro; since that's not a frame, nobody will
1519 try to switch frames on us, and the selected window will
1520 remain unchanged.
1521
1522 Since this event came from a macro, it would be misleading to
1523 leave internal_last_event_frame set to wherever the last
1524 real event came from. Normally, a switch-frame event selects
1525 internal_last_event_frame after each command is read, but
1526 events read from a macro should never cause a new frame to be
1527 selected. */
1528 Vlast_event_frame = internal_last_event_frame = Qmacro;
1529 #endif
1530
1531 /* Exit the macro if we are at the end.
1532 Also, some things replace the macro with t
1533 to force an early exit. */
1534 if (EQ (Vexecuting_macro, Qt)
1535 || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
1536 {
1537 XSETINT (c, -1);
1538 return c;
1539 }
1540
1541 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
1542 if (STRINGP (Vexecuting_macro)
1543 && (XINT (c) & 0x80))
1544 XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
1545
1546 executing_macro_index++;
1547
1548 goto from_macro;
1549 }
1550
1551 if (!NILP (unread_switch_frame))
1552 {
1553 c = unread_switch_frame;
1554 unread_switch_frame = Qnil;
1555
1556 /* This event should make it into this_command_keys, and get echoed
1557 again, so we go to reread_first, rather than reread. */
1558 goto reread_first;
1559 }
1560
1561 /* Don't bother updating menu bars while doing mouse tracking.
1562 We get events very rapidly then, and the menu bar won't be changing.
1563 We do update the menu bar once on entry to Ftrack_mouse. */
1564 if (commandflag > 0 && !input_pending && !detect_input_pending ())
1565 prepare_menu_bars ();
1566
1567 /* Save outer setjmp data, in case called recursively. */
1568 save_getcjmp (save_jump);
1569
1570 stop_polling ();
1571
1572 if (commandflag >= 0 && !input_pending && !detect_input_pending ())
1573 redisplay ();
1574
1575 if (_setjmp (getcjmp))
1576 {
1577 XSETINT (c, quit_char);
1578 #ifdef MULTI_FRAME
1579 XSETFRAME (internal_last_event_frame, selected_frame);
1580 Vlast_event_frame = internal_last_event_frame;
1581 #endif
1582 /* If we report the quit char as an event,
1583 don't do so more than once. */
1584 if (!NILP (Vinhibit_quit))
1585 Vquit_flag = Qnil;
1586
1587 goto non_reread;
1588 }
1589
1590 /* Message turns off echoing unless more keystrokes turn it on again. */
1591 if (echo_area_glyphs && *echo_area_glyphs && echo_area_glyphs != echobuf)
1592 cancel_echoing ();
1593 else
1594 /* If already echoing, continue. */
1595 echo_dash ();
1596
1597 /* Try reading a character via menu prompting in the minibuf.
1598 Try this before the sit-for, because the sit-for
1599 would do the wrong thing if we are supposed to do
1600 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
1601 after a mouse event so don't try a minibuf menu. */
1602 c = Qnil;
1603 if (nmaps > 0 && INTERACTIVE
1604 && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
1605 /* Don't bring up a menu if we already have another event. */
1606 && NILP (Vunread_command_events)
1607 && unread_command_char < 0
1608 && !detect_input_pending ())
1609 {
1610 c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
1611 if (! NILP (c))
1612 {
1613 key_already_recorded = 1;
1614 goto non_reread;
1615 }
1616 }
1617
1618 /* If in middle of key sequence and minibuffer not active,
1619 start echoing if enough time elapses. */
1620 if (minibuf_level == 0 && !immediate_echo && this_command_key_count > 0
1621 && ! noninteractive
1622 && echo_keystrokes > 0
1623 && (echo_area_glyphs == 0 || *echo_area_glyphs == 0))
1624 {
1625 Lisp_Object tem0;
1626
1627 /* After a mouse event, start echoing right away.
1628 This is because we are probably about to display a menu,
1629 and we don't want to delay before doing so. */
1630 if (EVENT_HAS_PARAMETERS (prev_event))
1631 echo ();
1632 else
1633 {
1634 tem0 = sit_for (echo_keystrokes, 0, 1, 1);
1635 if (EQ (tem0, Qt))
1636 echo ();
1637 }
1638 }
1639
1640 /* Maybe auto save due to number of keystrokes or idle time. */
1641
1642 if (commandflag != 0
1643 && auto_save_interval > 0
1644 && num_nonmacro_input_chars - last_auto_save > max (auto_save_interval, 20)
1645 && !detect_input_pending ())
1646 {
1647 jmp_buf temp;
1648 save_getcjmp (temp);
1649 Fdo_auto_save (Qnil, Qnil);
1650 restore_getcjmp (temp);
1651 }
1652
1653 /* Try reading using an X menu.
1654 This is never confused with reading using the minibuf
1655 because the recursive call of read_char in read_char_minibuf_menu_prompt
1656 does not pass on any keymaps. */
1657 if (nmaps > 0 && INTERACTIVE
1658 && !NILP (prev_event) && EVENT_HAS_PARAMETERS (prev_event)
1659 /* Don't bring up a menu if we already have another event. */
1660 && NILP (Vunread_command_events)
1661 && unread_command_char < 0)
1662 c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
1663
1664 /* Slow down auto saves logarithmically in size of current buffer,
1665 and garbage collect while we're at it. */
1666 if (INTERACTIVE && NILP (c))
1667 {
1668 int delay_level, buffer_size;
1669
1670 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
1671 last_non_minibuf_size = Z - BEG;
1672 buffer_size = (last_non_minibuf_size >> 8) + 1;
1673 delay_level = 0;
1674 while (buffer_size > 64)
1675 delay_level++, buffer_size -= buffer_size >> 2;
1676 if (delay_level < 4) delay_level = 4;
1677 /* delay_level is 4 for files under around 50k, 7 at 100k,
1678 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
1679
1680 /* Auto save if enough time goes by without input. */
1681 if (commandflag != 0
1682 && num_nonmacro_input_chars > last_auto_save
1683 && INTEGERP (Vauto_save_timeout)
1684 && XINT (Vauto_save_timeout) > 0)
1685 {
1686 Lisp_Object tem0;
1687 int delay = delay_level * XFASTINT (Vauto_save_timeout) / 4;
1688 tem0 = sit_for (delay, 0, 1, 1);
1689 if (EQ (tem0, Qt))
1690 {
1691 jmp_buf temp;
1692 save_getcjmp (temp);
1693 Fdo_auto_save (Qnil, Qnil);
1694 restore_getcjmp (temp);
1695
1696 /* If we have auto-saved and there is still no input
1697 available, garbage collect if there has been enough
1698 consing going on to make it worthwhile. */
1699 if (!detect_input_pending ()
1700 && consing_since_gc > gc_cons_threshold / 2)
1701 {
1702 Fgarbage_collect ();
1703 /* prepare_menu_bars isn't safe here, but it should
1704 also be unnecessary. */
1705 redisplay ();
1706 }
1707 }
1708 }
1709 }
1710
1711 /* Actually read a character, waiting if necessary. */
1712 while (NILP (c))
1713 {
1714 c = kbd_buffer_get_event ();
1715 if (!NILP (c))
1716 break;
1717 if (commandflag >= 0 && !input_pending && !detect_input_pending ())
1718 {
1719 prepare_menu_bars ();
1720 redisplay ();
1721 }
1722 }
1723
1724 /* Terminate Emacs in batch mode if at eof. */
1725 if (noninteractive && INTEGERP (c) && XINT (c) < 0)
1726 Fkill_emacs (make_number (1));
1727
1728 if (INTEGERP (c))
1729 {
1730 /* Add in any extra modifiers, where appropriate. */
1731 if ((extra_keyboard_modifiers & CHAR_CTL)
1732 || ((extra_keyboard_modifiers & 0177) < ' '
1733 && (extra_keyboard_modifiers & 0177) != 0))
1734 XSETINT (c, make_ctrl_char (XINT (c)));
1735
1736 /* Transfer any other modifier bits directly from
1737 extra_keyboard_modifiers to c. Ignore the actual character code
1738 in the low 16 bits of extra_keyboard_modifiers. */
1739 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
1740 }
1741
1742 non_reread:
1743
1744 restore_getcjmp (save_jump);
1745
1746 start_polling ();
1747
1748 /* Buffer switch events are only for internal wakeups
1749 so don't show them to the user. */
1750 if (BUFFERP (c))
1751 return c;
1752
1753 if (key_already_recorded)
1754 return c;
1755
1756 /* Wipe the echo area. */
1757 echo_area_glyphs = 0;
1758
1759 /* Handle things that only apply to characters. */
1760 if (INTEGERP (c))
1761 {
1762 /* If kbd_buffer_get_event gave us an EOF, return that. */
1763 if (XINT (c) == -1)
1764 return c;
1765
1766 if (STRINGP (Vkeyboard_translate_table)
1767 && XSTRING (Vkeyboard_translate_table)->size > XFASTINT (c))
1768 XSETINT (c, XSTRING (Vkeyboard_translate_table)->data[XFASTINT (c)]);
1769 }
1770
1771 total_keys++;
1772 XVECTOR (recent_keys)->contents[recent_keys_index] = c;
1773 if (++recent_keys_index >= NUM_RECENT_KEYS)
1774 recent_keys_index = 0;
1775
1776 /* Write c to the dribble file. If c is a lispy event, write
1777 the event's symbol to the dribble file, in <brackets>. Bleaugh.
1778 If you, dear reader, have a better idea, you've got the source. :-) */
1779 if (dribble)
1780 {
1781 if (INTEGERP (c))
1782 {
1783 if (XUINT (c) < 0x100)
1784 putc (XINT (c), dribble);
1785 else
1786 fprintf (dribble, " 0x%x", XUINT (c));
1787 }
1788 else
1789 {
1790 Lisp_Object dribblee;
1791
1792 /* If it's a structured event, take the event header. */
1793 dribblee = EVENT_HEAD (c);
1794
1795 if (SYMBOLP (dribblee))
1796 {
1797 putc ('<', dribble);
1798 fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
1799 XSYMBOL (dribblee)->name->size,
1800 dribble);
1801 putc ('>', dribble);
1802 }
1803 }
1804
1805 fflush (dribble);
1806 }
1807
1808 store_kbd_macro_char (c);
1809
1810 num_nonmacro_input_chars++;
1811
1812 from_macro:
1813 reread_first:
1814
1815 /* Don't echo mouse motion events. */
1816 if (! (EVENT_HAS_PARAMETERS (c)
1817 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
1818 echo_char (c);
1819
1820 /* Record this character as part of the current key. */
1821 add_command_key (c);
1822
1823 /* Re-reading in the middle of a command */
1824 reread:
1825 last_input_char = c;
1826 num_input_chars++;
1827
1828 /* Process the help character specially if enabled */
1829 if (EQ (c, Vhelp_char) && !NILP (Vhelp_form))
1830 {
1831 Lisp_Object tem0;
1832 count = specpdl_ptr - specpdl;
1833
1834 record_unwind_protect (Fset_window_configuration,
1835 Fcurrent_window_configuration (Qnil));
1836
1837 tem0 = Feval (Vhelp_form);
1838 if (STRINGP (tem0))
1839 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
1840
1841 cancel_echoing ();
1842 do
1843 c = read_char (0, 0, 0, Qnil, 0);
1844 while (BUFFERP (c));
1845 /* Remove the help from the frame */
1846 unbind_to (count, Qnil);
1847 prepare_menu_bars ();
1848 redisplay ();
1849 if (EQ (c, make_number (040)))
1850 {
1851 cancel_echoing ();
1852 do
1853 c = read_char (0, 0, 0, Qnil, 0);
1854 while (BUFFERP (c));
1855 }
1856 }
1857
1858 return c;
1859 }
1860
1861 Lisp_Object
1862 print_help (object)
1863 Lisp_Object object;
1864 {
1865 Fprinc (object, Qnil);
1866 return Qnil;
1867 }
1868
1869 /* Copy out or in the info on where C-g should throw to.
1870 This is used when running Lisp code from within get_char,
1871 in case get_char is called recursively.
1872 See read_process_output. */
1873
1874 save_getcjmp (temp)
1875 jmp_buf temp;
1876 {
1877 bcopy (getcjmp, temp, sizeof getcjmp);
1878 }
1879
1880 restore_getcjmp (temp)
1881 jmp_buf temp;
1882 {
1883 bcopy (temp, getcjmp, sizeof getcjmp);
1884 }
1885
1886 \f
1887 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
1888 of this function. */
1889 static Lisp_Object
1890 tracking_off (old_value)
1891 Lisp_Object old_value;
1892 {
1893 if (! XFASTINT (old_value))
1894 {
1895 do_mouse_tracking = 0;
1896
1897 /* Redisplay may have been preempted because there was input
1898 available, and it assumes it will be called again after the
1899 input has been processed. If the only input available was
1900 the sort that we have just disabled, then we need to call
1901 redisplay. */
1902 if (!readable_events ())
1903 {
1904 prepare_menu_bars ();
1905 redisplay_preserve_echo_area ();
1906 get_input_pending (&input_pending);
1907 }
1908 }
1909 }
1910
1911 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
1912 "Evaluate BODY with mouse movement events enabled.\n\
1913 Within a `track-mouse' form, mouse motion generates input events that\n\
1914 you can read with `read-event'.\n\
1915 Normally, mouse motion is ignored.")
1916 (args)
1917 Lisp_Object args;
1918 {
1919 int count = specpdl_ptr - specpdl;
1920 Lisp_Object val;
1921
1922 XSETINT (val, do_mouse_tracking);
1923 record_unwind_protect (tracking_off, val);
1924
1925 if (!input_pending && !detect_input_pending ())
1926 prepare_menu_bars ();
1927
1928 do_mouse_tracking = 1;
1929
1930 val = Fprogn (args);
1931 return unbind_to (count, val);
1932 }
1933 \f
1934 /* Low level keyboard/mouse input.
1935 kbd_buffer_store_event places events in kbd_buffer, and
1936 kbd_buffer_get_event retrieves them.
1937 mouse_moved indicates when the mouse has moved again, and
1938 *mouse_position_hook provides the mouse position. */
1939
1940 /* Return true iff there are any events in the queue that read-char
1941 would return. If this returns false, a read-char would block. */
1942 static int
1943 readable_events ()
1944 {
1945 return ! EVENT_QUEUES_EMPTY;
1946 }
1947
1948 /* Set this for debugging, to have a way to get out */
1949 int stop_character;
1950
1951 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
1952
1953 void
1954 kbd_buffer_store_event (event)
1955 register struct input_event *event;
1956 {
1957 if (event->kind == no_event)
1958 abort ();
1959
1960 if (event->kind == ascii_keystroke)
1961 {
1962 register int c = event->code & 0377;
1963
1964 if (event->modifiers & ctrl_modifier)
1965 c = make_ctrl_char (c);
1966
1967 c |= (event->modifiers
1968 & (meta_modifier | alt_modifier
1969 | hyper_modifier | super_modifier));
1970
1971 if (c == quit_char)
1972 {
1973 extern SIGTYPE interrupt_signal ();
1974
1975 #ifdef MULTI_FRAME
1976 /* If this results in a quit_char being returned to Emacs as
1977 input, set Vlast_event_frame properly. If this doesn't
1978 get returned to Emacs as an event, the next event read
1979 will set Vlast_event_frame again, so this is safe to do. */
1980 {
1981 Lisp_Object focus;
1982
1983 focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
1984 if (NILP (focus))
1985 internal_last_event_frame = event->frame_or_window;
1986 else
1987 internal_last_event_frame = focus;
1988 Vlast_event_frame = internal_last_event_frame;
1989 }
1990 #endif
1991
1992 last_event_timestamp = event->timestamp;
1993 interrupt_signal ();
1994 return;
1995 }
1996
1997 if (c && c == stop_character)
1998 {
1999 sys_suspend ();
2000 return;
2001 }
2002 }
2003
2004 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
2005 kbd_store_ptr = kbd_buffer;
2006
2007 /* Don't let the very last slot in the buffer become full,
2008 since that would make the two pointers equal,
2009 and that is indistinguishable from an empty buffer.
2010 Discard the event if it would fill the last slot. */
2011 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
2012 {
2013 kbd_store_ptr->kind = event->kind;
2014 if (event->kind == selection_request_event)
2015 {
2016 /* We must not use the ordinary copying code for this case,
2017 since `part' is an enum and copying it might not copy enough
2018 in this case. */
2019 bcopy (event, kbd_store_ptr, sizeof (*event));
2020 }
2021 else
2022 {
2023 kbd_store_ptr->code = event->code;
2024 kbd_store_ptr->part = event->part;
2025 kbd_store_ptr->frame_or_window = event->frame_or_window;
2026 kbd_store_ptr->modifiers = event->modifiers;
2027 kbd_store_ptr->x = event->x;
2028 kbd_store_ptr->y = event->y;
2029 kbd_store_ptr->timestamp = event->timestamp;
2030 }
2031 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_store_ptr
2032 - kbd_buffer]
2033 = event->frame_or_window);
2034
2035 kbd_store_ptr++;
2036 }
2037 }
2038 \f
2039 /* Read one event from the event buffer, waiting if necessary.
2040 The value is a Lisp object representing the event.
2041 The value is nil for an event that should be ignored,
2042 or that was handled here.
2043 We always read and discard one event. */
2044
2045 static Lisp_Object
2046 kbd_buffer_get_event ()
2047 {
2048 register int c;
2049 Lisp_Object obj;
2050
2051 if (noninteractive)
2052 {
2053 c = getchar ();
2054 XSETINT (obj, c);
2055 return obj;
2056 }
2057
2058 /* Wait until there is input available. */
2059 for (;;)
2060 {
2061 if (!EVENT_QUEUES_EMPTY)
2062 break;
2063
2064 /* If the quit flag is set, then read_char will return
2065 quit_char, so that counts as "available input." */
2066 if (!NILP (Vquit_flag))
2067 quit_throw_to_read_char ();
2068
2069 /* One way or another, wait until input is available; then, if
2070 interrupt handlers have not read it, read it now. */
2071
2072 #ifdef OLDVMS
2073 wait_for_kbd_input ();
2074 #else
2075 /* Note SIGIO has been undef'd if FIONREAD is missing. */
2076 #ifdef SIGIO
2077 gobble_input (0);
2078 #endif /* SIGIO */
2079 if (EVENT_QUEUES_EMPTY)
2080 {
2081 Lisp_Object minus_one;
2082
2083 XSETINT (minus_one, -1);
2084 wait_reading_process_input (0, 0, minus_one, 1);
2085
2086 if (!interrupt_input && EVENT_QUEUES_EMPTY)
2087 /* Pass 1 for EXPECT since we just waited to have input. */
2088 read_avail_input (1);
2089 }
2090 #endif /* not VMS */
2091 }
2092
2093 /* At this point, we know that there is a readable event available
2094 somewhere. If the event queue is empty, then there must be a
2095 mouse movement enabled and available. */
2096 if (kbd_fetch_ptr != kbd_store_ptr)
2097 {
2098 struct input_event *event;
2099
2100 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
2101 ? kbd_fetch_ptr
2102 : kbd_buffer);
2103
2104 last_event_timestamp = event->timestamp;
2105
2106 obj = Qnil;
2107
2108 /* These two kinds of events get special handling
2109 and don't actually appear to the command loop.
2110 We return nil for them. */
2111 if (event->kind == selection_request_event)
2112 {
2113 #ifdef HAVE_X11
2114 x_handle_selection_request (event);
2115 kbd_fetch_ptr = event + 1;
2116 #else
2117 /* We're getting selection request events, but we don't have
2118 a window system. */
2119 abort ();
2120 #endif
2121 }
2122
2123 else if (event->kind == selection_clear_event)
2124 {
2125 #ifdef HAVE_X11
2126 x_handle_selection_clear (event);
2127 kbd_fetch_ptr = event + 1;
2128 #else
2129 /* We're getting selection request events, but we don't have
2130 a window system. */
2131 abort ();
2132 #endif
2133 }
2134 #ifdef HAVE_X11
2135 else if (event->kind == delete_window_event)
2136 {
2137 /* Make an event (delete-frame (FRAME)). */
2138 obj = Fcons (event->frame_or_window, Qnil);
2139 obj = Fcons (intern ("delete-frame"), Fcons (obj, Qnil));
2140 kbd_fetch_ptr = event + 1;
2141 }
2142 #endif
2143 else if (event->kind == menu_bar_event)
2144 {
2145 /* The event value is in the frame_or_window slot. */
2146 obj = event->frame_or_window;
2147 kbd_fetch_ptr = event + 1;
2148 }
2149 else if (event->kind == buffer_switch_event)
2150 {
2151 /* The value doesn't matter here; only the type is tested. */
2152 XSETBUFFER (obj, current_buffer);
2153 kbd_fetch_ptr = event + 1;
2154 }
2155 /* Just discard these, by returning nil.
2156 (They shouldn't be found in the buffer,
2157 but on some machines it appears they do show up.) */
2158 else if (event->kind == no_event)
2159 kbd_fetch_ptr = event + 1;
2160
2161 /* If this event is on a different frame, return a switch-frame this
2162 time, and leave the event in the queue for next time. */
2163 else
2164 {
2165 #ifdef MULTI_FRAME
2166 Lisp_Object frame;
2167 Lisp_Object focus;
2168
2169 frame = event->frame_or_window;
2170 if (WINDOWP (frame))
2171 frame = WINDOW_FRAME (XWINDOW (frame));
2172
2173 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
2174 if (! NILP (focus))
2175 frame = focus;
2176
2177 if (! EQ (frame, internal_last_event_frame)
2178 && XFRAME (frame) != selected_frame)
2179 obj = make_lispy_switch_frame (frame);
2180 internal_last_event_frame = frame;
2181 #endif /* MULTI_FRAME */
2182
2183 /* If we didn't decide to make a switch-frame event, go ahead
2184 and build a real event from the queue entry. */
2185
2186 if (NILP (obj))
2187 {
2188 obj = make_lispy_event (event);
2189
2190 /* Wipe out this event, to catch bugs. */
2191 event->kind = no_event;
2192 (XVECTOR (kbd_buffer_frame_or_window)->contents[event - kbd_buffer]
2193 = Qnil);
2194
2195 kbd_fetch_ptr = event + 1;
2196 }
2197 }
2198 }
2199 /* Try generating a mouse motion event. */
2200 else if (do_mouse_tracking && mouse_moved)
2201 {
2202 FRAME_PTR f = 0;
2203 Lisp_Object bar_window;
2204 enum scroll_bar_part part;
2205 Lisp_Object x, y;
2206 unsigned long time;
2207
2208 (*mouse_position_hook) (&f, &bar_window, &part, &x, &y, &time);
2209
2210 obj = Qnil;
2211
2212 #ifdef MULTI_FRAME
2213 /* Decide if we should generate a switch-frame event. Don't
2214 generate switch-frame events for motion outside of all Emacs
2215 frames. */
2216 if (f)
2217 {
2218 Lisp_Object frame;
2219
2220 frame = FRAME_FOCUS_FRAME (f);
2221 if (NILP (frame))
2222 XSETFRAME (frame, f);
2223
2224 if (! EQ (frame, internal_last_event_frame)
2225 && XFRAME (frame) != selected_frame)
2226 obj = make_lispy_switch_frame (frame);
2227 internal_last_event_frame = frame;
2228 }
2229 #endif
2230
2231 #if defined (MULTI_FRAME) || defined (HAVE_MOUSE)
2232 /* If we didn't decide to make a switch-frame event, go ahead and
2233 return a mouse-motion event. */
2234 if (NILP (obj))
2235 obj = make_lispy_movement (f, bar_window, part, x, y, time);
2236 #endif
2237 }
2238 else
2239 /* We were promised by the above while loop that there was
2240 something for us to read! */
2241 abort ();
2242
2243 input_pending = readable_events ();
2244
2245 #ifdef MULTI_FRAME
2246 Vlast_event_frame = internal_last_event_frame;
2247 #endif
2248
2249 return (obj);
2250 }
2251 \f
2252 /* Process any events that are not user-visible,
2253 then return, without reading any user-visible events. */
2254
2255 void
2256 swallow_events ()
2257 {
2258 while (kbd_fetch_ptr != kbd_store_ptr)
2259 {
2260 struct input_event *event;
2261
2262 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
2263 ? kbd_fetch_ptr
2264 : kbd_buffer);
2265
2266 last_event_timestamp = event->timestamp;
2267
2268 /* These two kinds of events get special handling
2269 and don't actually appear to the command loop. */
2270 if (event->kind == selection_request_event)
2271 {
2272 #ifdef HAVE_X11
2273 x_handle_selection_request (event);
2274 kbd_fetch_ptr = event + 1;
2275 #else
2276 /* We're getting selection request events, but we don't have
2277 a window system. */
2278 abort ();
2279 #endif
2280 }
2281
2282 else if (event->kind == selection_clear_event)
2283 {
2284 #ifdef HAVE_X11
2285 x_handle_selection_clear (event);
2286 kbd_fetch_ptr = event + 1;
2287 #else
2288 /* We're getting selection request events, but we don't have
2289 a window system. */
2290 abort ();
2291 #endif
2292 }
2293 else
2294 break;
2295 }
2296
2297 get_input_pending (&input_pending);
2298 }
2299 \f
2300 /* Caches for modify_event_symbol. */
2301 static Lisp_Object accent_key_syms;
2302 static Lisp_Object system_key_syms;
2303 static Lisp_Object func_key_syms;
2304 static Lisp_Object mouse_syms;
2305
2306 Lisp_Object Vsystem_key_alist;
2307
2308 /* This is a list of keysym codes for special "accent" characters.
2309 It parallels lispy_accent_keys. */
2310
2311 static int lispy_accent_codes[] =
2312 {
2313 #ifdef XK_dead_circumflex
2314 XK_dead_circumflex,
2315 #else
2316 0,
2317 #endif
2318 #ifdef XK_dead_grave
2319 XK_dead_grave,
2320 #else
2321 0,
2322 #endif
2323 #ifdef XK_dead_tilde
2324 XK_dead_tilde,
2325 #else
2326 0,
2327 #endif
2328 #ifdef XK_dead_diaeresis
2329 XK_dead_diaeresis,
2330 #else
2331 0,
2332 #endif
2333 #ifdef XK_dead_macron
2334 XK_dead_macron,
2335 #else
2336 0,
2337 #endif
2338 #ifdef XK_dead_degree
2339 XK_dead_degree,
2340 #else
2341 0,
2342 #endif
2343 #ifdef XK_dead_acute
2344 XK_dead_acute,
2345 #else
2346 0,
2347 #endif
2348 #ifdef XK_dead_cedilla
2349 XK_dead_cedilla,
2350 #else
2351 0,
2352 #endif
2353 #ifdef XK_dead_breve
2354 XK_dead_breve,
2355 #else
2356 0,
2357 #endif
2358 #ifdef XK_dead_ogonek
2359 XK_dead_ogonek,
2360 #else
2361 0,
2362 #endif
2363 #ifdef XK_dead_caron
2364 XK_dead_caron,
2365 #else
2366 0,
2367 #endif
2368 #ifdef XK_dead_doubleacute
2369 XK_dead_doubleacute,
2370 #else
2371 0,
2372 #endif
2373 #ifdef XK_dead_abovedot
2374 XK_dead_abovedot,
2375 #else
2376 0,
2377 #endif
2378 };
2379
2380 /* This is a list of Lisp names for special "accent" characters.
2381 It parallels lispy_accent_codes. */
2382
2383 static char *lispy_accent_keys[] =
2384 {
2385 "dead-circumflex",
2386 "dead-grave",
2387 "dead-tilde",
2388 "dead-diaeresis",
2389 "dead-macron",
2390 "dead-degree",
2391 "dead-acute",
2392 "dead-cedilla",
2393 "dead-breve",
2394 "dead-ogonek",
2395 "dead-caron",
2396 "dead-doubleacute",
2397 "dead-abovedot",
2398 };
2399
2400 /* You'll notice that this table is arranged to be conveniently
2401 indexed by X Windows keysym values. */
2402 static char *lispy_function_keys[] =
2403 {
2404 /* X Keysym value */
2405
2406 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00 */
2407 "backspace",
2408 "tab",
2409 "linefeed",
2410 "clear",
2411 0,
2412 "return",
2413 0, 0,
2414 0, 0, 0, /* 0xff10 */
2415 "pause",
2416 0, 0, 0, 0, 0, 0, 0,
2417 "escape",
2418 0, 0, 0, 0,
2419 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff20...2f */
2420 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff30...3f */
2421 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
2422
2423 "home", /* 0xff50 */ /* IsCursorKey */
2424 "left",
2425 "up",
2426 "right",
2427 "down",
2428 "prior",
2429 "next",
2430 "end",
2431 "begin",
2432 0, /* 0xff59 */
2433 0, 0, 0, 0, 0, 0,
2434 "select", /* 0xff60 */ /* IsMiscFunctionKey */
2435 "print",
2436 "execute",
2437 "insert",
2438 0, /* 0xff64 */
2439 "undo",
2440 "redo",
2441 "menu",
2442 "find",
2443 "cancel",
2444 "help",
2445 "break", /* 0xff6b */
2446
2447 0, 0, 0, 0, 0, 0, 0, 0, "backtab", 0,
2448 0, /* 0xff76 */
2449 0, 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff7f */
2450 "kp-space", /* 0xff80 */ /* IsKeypadKey */
2451 0, 0, 0, 0, 0, 0, 0, 0,
2452 "kp-tab", /* 0xff89 */
2453 0, 0, 0,
2454 "kp-enter", /* 0xff8d */
2455 0, 0, 0,
2456 "kp-f1", /* 0xff91 */
2457 "kp-f2",
2458 "kp-f3",
2459 "kp-f4",
2460 "kp-home", /* 0xff95 */
2461 "kp-left",
2462 "kp-up",
2463 "kp-right",
2464 "kp-down",
2465 "kp-prior", /* kp-page-up */
2466 "kp-next", /* kp-page-down */
2467 "kp-end",
2468 "kp-begin",
2469 "kp-insert",
2470 "kp-delete",
2471 0, /* 0xffa0 */
2472 0, 0, 0, 0, 0, 0, 0, 0, 0,
2473 "kp-multiply", /* 0xffaa */
2474 "kp-add",
2475 "kp-separator",
2476 "kp-subtract",
2477 "kp-decimal",
2478 "kp-divide", /* 0xffaf */
2479 "kp-0", /* 0xffb0 */
2480 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
2481 0, /* 0xffba */
2482 0, 0,
2483 "kp-equal", /* 0xffbd */
2484 "f1", /* 0xffbe */ /* IsFunctionKey */
2485 "f2",
2486 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
2487 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
2488 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
2489 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
2490 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
2491 0, 0, 0, 0, 0, 0, 0, 0,
2492 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
2493 0, 0, 0, 0, 0, 0, 0, "delete"
2494 };
2495
2496 static char *lispy_mouse_names[] =
2497 {
2498 "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
2499 };
2500
2501 /* Scroll bar parts. */
2502 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
2503
2504 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
2505 Lisp_Object *scroll_bar_parts[] = {
2506 &Qabove_handle, &Qhandle, &Qbelow_handle
2507 };
2508
2509
2510 /* A vector, indexed by button number, giving the down-going location
2511 of currently depressed buttons, both scroll bar and non-scroll bar.
2512
2513 The elements have the form
2514 (BUTTON-NUMBER MODIFIER-MASK . REST)
2515 where REST is the cdr of a position as it would be reported in the event.
2516
2517 The make_lispy_event function stores positions here to tell the
2518 difference between click and drag events, and to store the starting
2519 location to be included in drag events. */
2520
2521 static Lisp_Object button_down_location;
2522
2523 /* Information about the most recent up-going button event: Which
2524 button, what location, and what time. */
2525
2526 static int last_mouse_button;
2527 static int last_mouse_x;
2528 static int last_mouse_y;
2529 static unsigned long button_down_time;
2530
2531 /* The maximum time between clicks to make a double-click,
2532 or Qnil to disable double-click detection,
2533 or Qt for no time limit. */
2534 Lisp_Object Vdouble_click_time;
2535
2536 /* The number of clicks in this multiple-click. */
2537
2538 int double_click_count;
2539
2540 #ifdef USE_X_TOOLKIT
2541 extern Lisp_Object map_event_to_object ();
2542 #endif /* USE_X_TOOLKIT */
2543
2544 /* Given a struct input_event, build the lisp event which represents
2545 it. If EVENT is 0, build a mouse movement event from the mouse
2546 movement buffer, which should have a movement event in it.
2547
2548 Note that events must be passed to this function in the order they
2549 are received; this function stores the location of button presses
2550 in order to build drag events when the button is released. */
2551
2552 static Lisp_Object
2553 make_lispy_event (event)
2554 struct input_event *event;
2555 {
2556 int i;
2557
2558 #ifdef SWITCH_ENUM_BUG
2559 switch ((int) event->kind)
2560 #else
2561 switch (event->kind)
2562 #endif
2563 {
2564 /* A simple keystroke. */
2565 case ascii_keystroke:
2566 {
2567 Lisp_Object lispy_c;
2568 int c = event->code & 0377;
2569 /* Turn ASCII characters into control characters
2570 when proper. */
2571 if (event->modifiers & ctrl_modifier)
2572 c = make_ctrl_char (c);
2573
2574 /* Add in the other modifier bits. We took care of ctrl_modifier
2575 just above, and the shift key was taken care of by the X code,
2576 and applied to control characters by make_ctrl_char. */
2577 c |= (event->modifiers
2578 & (meta_modifier | alt_modifier
2579 | hyper_modifier | super_modifier));
2580 button_down_time = 0;
2581 XSETFASTINT (lispy_c, c);
2582 return lispy_c;
2583 }
2584
2585 /* A function key. The symbol may need to have modifier prefixes
2586 tacked onto it. */
2587 case non_ascii_keystroke:
2588 button_down_time = 0;
2589
2590 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
2591 if (event->code == lispy_accent_codes[i])
2592 return modify_event_symbol (i,
2593 event->modifiers,
2594 Qfunction_key, Qnil,
2595 lispy_accent_keys, &accent_key_syms,
2596 (sizeof (lispy_accent_keys)
2597 / sizeof (lispy_accent_keys[0])));
2598
2599 /* Handle system-specific keysyms. */
2600 if (event->code & (1 << 28))
2601 {
2602 /* We need to use an alist rather than a vector as the cache
2603 since we can't make a vector long enuf. */
2604 if (NILP (system_key_syms))
2605 system_key_syms = Fcons (Qnil, Qnil);
2606 return modify_event_symbol (event->code & 0xffffff,
2607 event->modifiers,
2608 Qfunction_key, Vsystem_key_alist,
2609 0, &system_key_syms, 0xffffff);
2610 }
2611
2612 return modify_event_symbol (event->code - 0xff00,
2613 event->modifiers,
2614 Qfunction_key, Qnil,
2615 lispy_function_keys, &func_key_syms,
2616 (sizeof (lispy_function_keys)
2617 / sizeof (lispy_function_keys[0])));
2618 break;
2619
2620 #if defined (MULTI_FRAME) || defined (HAVE_MOUSE)
2621 /* A mouse click. Figure out where it is, decide whether it's
2622 a press, click or drag, and build the appropriate structure. */
2623 case mouse_click:
2624 case scroll_bar_click:
2625 {
2626 int button = event->code;
2627 int is_double;
2628 Lisp_Object position;
2629 Lisp_Object *start_pos_ptr;
2630 Lisp_Object start_pos;
2631
2632 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
2633 abort ();
2634
2635 /* Build the position as appropriate for this mouse click. */
2636 if (event->kind == mouse_click)
2637 {
2638 int part;
2639 FRAME_PTR f = XFRAME (event->frame_or_window);
2640 Lisp_Object window;
2641 Lisp_Object posn;
2642 int row, column;
2643
2644 /* Ignore mouse events that were made on frame that
2645 have been deleted. */
2646 if (! FRAME_LIVE_P (f))
2647 return Qnil;
2648
2649 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
2650 &column, &row, 0, 0);
2651
2652 #ifdef USE_X_TOOLKIT
2653 if (FRAME_EXTERNAL_MENU_BAR (f) && XINT (event->y) == -1)
2654 #else
2655 if (row < FRAME_MENU_BAR_LINES (f))
2656 #endif
2657 {
2658 Lisp_Object items, item;
2659 int hpos;
2660 int i;
2661
2662 /* Activate the menu bar on the down event. If the
2663 up event comes in before the menu code can deal with it,
2664 just ignore it. */
2665 if (! (event->modifiers & down_modifier))
2666 return Qnil;
2667
2668 #ifndef USE_X_TOOLKIT
2669 item = Qnil;
2670 items = FRAME_MENU_BAR_ITEMS (f);
2671 for (i = 0; i < XVECTOR (items)->size; i += 3)
2672 {
2673 Lisp_Object pos, string;
2674 string = XVECTOR (items)->contents[i + 1];
2675 pos = XVECTOR (items)->contents[i + 2];
2676 if (NILP (string))
2677 break;
2678 if (column >= XINT (pos)
2679 && column < XINT (pos) + XSTRING (string)->size)
2680 {
2681 item = XVECTOR (items)->contents[i];
2682 break;
2683 }
2684 }
2685 #endif /* not USE_X_TOOLKIT */
2686
2687 position
2688 = Fcons (event->frame_or_window,
2689 Fcons (Qmenu_bar,
2690 Fcons (Fcons (event->x, event->y),
2691 Fcons (make_number (event->timestamp),
2692 Qnil))));
2693
2694 return Fcons (item, Fcons (position, Qnil));
2695 }
2696
2697 window = window_from_coordinates (f, column, row, &part);
2698
2699 if (!WINDOWP (window))
2700 {
2701 window = event->frame_or_window;
2702 posn = Qnil;
2703 }
2704 else
2705 {
2706 int pixcolumn, pixrow;
2707 column -= XINT (XWINDOW (window)->left);
2708 row -= XINT (XWINDOW (window)->top);
2709 glyph_to_pixel_coords (f, column, row, &pixcolumn, &pixrow);
2710 XSETINT (event->x, pixcolumn);
2711 XSETINT (event->y, pixrow);
2712
2713 if (part == 1)
2714 posn = Qmode_line;
2715 else if (part == 2)
2716 posn = Qvertical_line;
2717 else
2718 XSETINT (posn,
2719 buffer_posn_from_coords (XWINDOW (window),
2720 column, row));
2721 }
2722
2723 position
2724 = Fcons (window,
2725 Fcons (posn,
2726 Fcons (Fcons (event->x, event->y),
2727 Fcons (make_number (event->timestamp),
2728 Qnil))));
2729 }
2730 else
2731 {
2732 Lisp_Object window;
2733 Lisp_Object portion_whole;
2734 Lisp_Object part;
2735
2736 window = event->frame_or_window;
2737 portion_whole = Fcons (event->x, event->y);
2738 part = *scroll_bar_parts[(int) event->part];
2739
2740 position =
2741 Fcons (window,
2742 Fcons (Qvertical_scroll_bar,
2743 Fcons (portion_whole,
2744 Fcons (make_number (event->timestamp),
2745 Fcons (part, Qnil)))));
2746 }
2747
2748 start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
2749
2750 start_pos = *start_pos_ptr;
2751 *start_pos_ptr = Qnil;
2752
2753 is_double = (button == last_mouse_button
2754 && XINT (event->x) == last_mouse_x
2755 && XINT (event->y) == last_mouse_y
2756 && button_down_time != 0
2757 && (EQ (Vdouble_click_time, Qt)
2758 || (INTEGERP (Vdouble_click_time)
2759 && ((int)(event->timestamp - button_down_time)
2760 < XINT (Vdouble_click_time)))));
2761 last_mouse_button = button;
2762 last_mouse_x = XINT (event->x);
2763 last_mouse_y = XINT (event->y);
2764
2765 /* If this is a button press, squirrel away the location, so
2766 we can decide later whether it was a click or a drag. */
2767 if (event->modifiers & down_modifier)
2768 {
2769 if (is_double)
2770 {
2771 double_click_count++;
2772 event->modifiers |= ((double_click_count > 2)
2773 ? triple_modifier
2774 : double_modifier);
2775 }
2776 else
2777 double_click_count = 1;
2778 button_down_time = event->timestamp;
2779 *start_pos_ptr = Fcopy_alist (position);
2780 }
2781
2782 /* Now we're releasing a button - check the co-ordinates to
2783 see if this was a click or a drag. */
2784 else if (event->modifiers & up_modifier)
2785 {
2786 /* If we did not see a down before this up,
2787 ignore the up. Probably this happened because
2788 the down event chose a menu item.
2789 It would be an annoyance to treat the release
2790 of the button that chose the menu item
2791 as a separate event. */
2792
2793 if (!CONSP (start_pos))
2794 return Qnil;
2795
2796 event->modifiers &= ~up_modifier;
2797 #if 0 /* Formerly we treated an up with no down as a click event. */
2798 if (!CONSP (start_pos))
2799 event->modifiers |= click_modifier;
2800 else
2801 #endif
2802 {
2803 /* The third element of every position should be the (x,y)
2804 pair. */
2805 Lisp_Object down;
2806
2807 down = Fnth (make_number (2), start_pos);
2808 if (EQ (event->x, XCONS (down)->car)
2809 && EQ (event->y, XCONS (down)->cdr))
2810 {
2811 if (is_double && double_click_count > 1)
2812 event->modifiers |= ((double_click_count > 2)
2813 ? triple_modifier
2814 : double_modifier);
2815 else
2816 event->modifiers |= click_modifier;
2817 }
2818 else
2819 {
2820 button_down_time = 0;
2821 event->modifiers |= drag_modifier;
2822 }
2823 }
2824 }
2825 else
2826 /* Every mouse event should either have the down_modifier or
2827 the up_modifier set. */
2828 abort ();
2829
2830 {
2831 /* Get the symbol we should use for the mouse click. */
2832 Lisp_Object head;
2833
2834 head = modify_event_symbol (button,
2835 event->modifiers,
2836 Qmouse_click, Qnil,
2837 lispy_mouse_names, &mouse_syms,
2838 (sizeof (lispy_mouse_names)
2839 / sizeof (lispy_mouse_names[0])));
2840 if (event->modifiers & drag_modifier)
2841 return Fcons (head,
2842 Fcons (start_pos,
2843 Fcons (position,
2844 Qnil)));
2845 else if (event->modifiers & (double_modifier | triple_modifier))
2846 return Fcons (head,
2847 Fcons (position,
2848 Fcons (make_number (double_click_count),
2849 Qnil)));
2850 else
2851 return Fcons (head,
2852 Fcons (position,
2853 Qnil));
2854 }
2855 }
2856 #endif /* MULTI_FRAME or HAVE_MOUSE */
2857
2858 /* The 'kind' field of the event is something we don't recognize. */
2859 default:
2860 abort ();
2861 }
2862 }
2863
2864 #if defined (MULTI_FRAME) || defined (HAVE_MOUSE)
2865
2866 static Lisp_Object
2867 make_lispy_movement (frame, bar_window, part, x, y, time)
2868 FRAME_PTR frame;
2869 Lisp_Object bar_window;
2870 enum scroll_bar_part part;
2871 Lisp_Object x, y;
2872 unsigned long time;
2873 {
2874 #ifdef MULTI_FRAME
2875 /* Is it a scroll bar movement? */
2876 if (frame && ! NILP (bar_window))
2877 {
2878 Lisp_Object part_sym;
2879
2880 part_sym = *scroll_bar_parts[(int) part];
2881 return Fcons (Qscroll_bar_movement,
2882 (Fcons (Fcons (bar_window,
2883 Fcons (Qvertical_scroll_bar,
2884 Fcons (Fcons (x, y),
2885 Fcons (make_number (time),
2886 Fcons (part_sym,
2887 Qnil))))),
2888 Qnil)));
2889 }
2890
2891 /* Or is it an ordinary mouse movement? */
2892 else
2893 #endif /* MULTI_FRAME */
2894 {
2895 int area;
2896 Lisp_Object window;
2897 Lisp_Object posn;
2898 int column, row;
2899
2900 #ifdef MULTI_FRAME
2901 if (frame)
2902 #else
2903 if (1)
2904 #endif
2905 {
2906 /* It's in a frame; which window on that frame? */
2907 pixel_to_glyph_coords (frame, XINT (x), XINT (y), &column, &row, 0, 1);
2908 window = window_from_coordinates (frame, column, row, &area);
2909 }
2910 else
2911 window = Qnil;
2912
2913 if (WINDOWP (window))
2914 {
2915 int pixcolumn, pixrow;
2916 column -= XINT (XWINDOW (window)->left);
2917 row -= XINT (XWINDOW (window)->top);
2918 glyph_to_pixel_coords (frame, column, row, &pixcolumn, &pixrow);
2919 XSETINT (x, pixcolumn);
2920 XSETINT (y, pixrow);
2921
2922 if (area == 1)
2923 posn = Qmode_line;
2924 else if (area == 2)
2925 posn = Qvertical_line;
2926 else
2927 XSETINT (posn,
2928 buffer_posn_from_coords (XWINDOW (window), column, row));
2929 }
2930 #ifdef MULTI_FRAME
2931 else if (frame != 0)
2932 {
2933 XSETFRAME (window, frame);
2934 posn = Qnil;
2935 }
2936 #endif
2937 else
2938 {
2939 window = Qnil;
2940 posn = Qnil;
2941 XSETFASTINT (x, 0);
2942 XSETFASTINT (y, 0);
2943 }
2944
2945 return Fcons (Qmouse_movement,
2946 Fcons (Fcons (window,
2947 Fcons (posn,
2948 Fcons (Fcons (x, y),
2949 Fcons (make_number (time),
2950 Qnil)))),
2951 Qnil));
2952 }
2953 }
2954
2955 #endif /* neither MULTI_FRAME nor HAVE_MOUSE */
2956
2957 /* Construct a switch frame event. */
2958 static Lisp_Object
2959 make_lispy_switch_frame (frame)
2960 Lisp_Object frame;
2961 {
2962 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
2963 }
2964 \f
2965 /* Manipulating modifiers. */
2966
2967 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
2968
2969 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
2970 SYMBOL's name of the end of the modifiers; the string from this
2971 position is the unmodified symbol name.
2972
2973 This doesn't use any caches. */
2974 static int
2975 parse_modifiers_uncached (symbol, modifier_end)
2976 Lisp_Object symbol;
2977 int *modifier_end;
2978 {
2979 struct Lisp_String *name;
2980 int i;
2981 int modifiers;
2982
2983 CHECK_SYMBOL (symbol, 1);
2984
2985 modifiers = 0;
2986 name = XSYMBOL (symbol)->name;
2987
2988
2989 for (i = 0; i+2 <= name->size; )
2990 switch (name->data[i])
2991 {
2992 #define SINGLE_LETTER_MOD(bit) \
2993 if (name->data[i+1] != '-') \
2994 goto no_more_modifiers; \
2995 modifiers |= bit; \
2996 i += 2;
2997
2998 case 'A':
2999 SINGLE_LETTER_MOD (alt_modifier);
3000 break;
3001
3002 case 'C':
3003 SINGLE_LETTER_MOD (ctrl_modifier);
3004 break;
3005
3006 case 'H':
3007 SINGLE_LETTER_MOD (hyper_modifier);
3008 break;
3009
3010 case 'M':
3011 SINGLE_LETTER_MOD (meta_modifier);
3012 break;
3013
3014 case 'S':
3015 SINGLE_LETTER_MOD (shift_modifier);
3016 break;
3017
3018 case 's':
3019 SINGLE_LETTER_MOD (super_modifier);
3020 break;
3021
3022 case 'd':
3023 if (i + 5 > name->size)
3024 goto no_more_modifiers;
3025 if (! strncmp (name->data + i, "drag-", 5))
3026 {
3027 modifiers |= drag_modifier;
3028 i += 5;
3029 }
3030 else if (! strncmp (name->data + i, "down-", 5))
3031 {
3032 modifiers |= down_modifier;
3033 i += 5;
3034 }
3035 else if (i + 7 <= name->size
3036 && ! strncmp (name->data + i, "double-", 7))
3037 {
3038 modifiers |= double_modifier;
3039 i += 7;
3040 }
3041 else
3042 goto no_more_modifiers;
3043 break;
3044
3045 case 't':
3046 if (i + 7 > name->size)
3047 goto no_more_modifiers;
3048 if (! strncmp (name->data + i, "triple-", 7))
3049 {
3050 modifiers |= triple_modifier;
3051 i += 7;
3052 }
3053 else
3054 goto no_more_modifiers;
3055 break;
3056
3057 default:
3058 goto no_more_modifiers;
3059
3060 #undef SINGLE_LETTER_MOD
3061 }
3062 no_more_modifiers:
3063
3064 /* Should we include the `click' modifier? */
3065 if (! (modifiers & (down_modifier | drag_modifier
3066 | double_modifier | triple_modifier))
3067 && i + 7 == name->size
3068 && strncmp (name->data + i, "mouse-", 6) == 0
3069 && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
3070 modifiers |= click_modifier;
3071
3072 if (modifier_end)
3073 *modifier_end = i;
3074
3075 return modifiers;
3076 }
3077
3078
3079 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
3080 prepended to the string BASE[0..BASE_LEN-1].
3081 This doesn't use any caches. */
3082 static Lisp_Object
3083 apply_modifiers_uncached (modifiers, base, base_len)
3084 int modifiers;
3085 char *base;
3086 int base_len;
3087 {
3088 /* Since BASE could contain nulls, we can't use intern here; we have
3089 to use Fintern, which expects a genuine Lisp_String, and keeps a
3090 reference to it. */
3091 char *new_mods =
3092 (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
3093 int mod_len;
3094
3095 {
3096 char *p = new_mods;
3097
3098 /* Only the event queue may use the `up' modifier; it should always
3099 be turned into a click or drag event before presented to lisp code. */
3100 if (modifiers & up_modifier)
3101 abort ();
3102
3103 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
3104 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
3105 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
3106 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
3107 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
3108 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
3109 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
3110 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
3111 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
3112 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
3113 /* The click modifier is denoted by the absence of other modifiers. */
3114
3115 *p = '\0';
3116
3117 mod_len = p - new_mods;
3118 }
3119
3120 {
3121 Lisp_Object new_name;
3122
3123 new_name = make_uninit_string (mod_len + base_len);
3124 bcopy (new_mods, XSTRING (new_name)->data, mod_len);
3125 bcopy (base, XSTRING (new_name)->data + mod_len, base_len);
3126
3127 return Fintern (new_name, Qnil);
3128 }
3129 }
3130
3131
3132 static char *modifier_names[] =
3133 {
3134 "up", "down", "drag", "click", "double", "triple", 0, 0,
3135 0, 0, 0, 0, 0, 0, 0, 0,
3136 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
3137 };
3138 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
3139
3140 static Lisp_Object modifier_symbols;
3141
3142 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
3143 static Lisp_Object
3144 lispy_modifier_list (modifiers)
3145 int modifiers;
3146 {
3147 Lisp_Object modifier_list;
3148 int i;
3149
3150 modifier_list = Qnil;
3151 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
3152 if (modifiers & (1<<i))
3153 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
3154 modifier_list);
3155
3156 return modifier_list;
3157 }
3158
3159
3160 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
3161 where UNMODIFIED is the unmodified form of SYMBOL,
3162 MASK is the set of modifiers present in SYMBOL's name.
3163 This is similar to parse_modifiers_uncached, but uses the cache in
3164 SYMBOL's Qevent_symbol_element_mask property, and maintains the
3165 Qevent_symbol_elements property. */
3166 static Lisp_Object
3167 parse_modifiers (symbol)
3168 Lisp_Object symbol;
3169 {
3170 Lisp_Object elements;
3171
3172 elements = Fget (symbol, Qevent_symbol_element_mask);
3173 if (CONSP (elements))
3174 return elements;
3175 else
3176 {
3177 int end;
3178 int modifiers = parse_modifiers_uncached (symbol, &end);
3179 Lisp_Object unmodified;
3180 Lisp_Object mask;
3181
3182 unmodified = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
3183 XSYMBOL (symbol)->name->size - end),
3184 Qnil);
3185
3186 if (modifiers & ~((1<<VALBITS) - 1))
3187 abort ();
3188 XSETFASTINT (mask, modifiers);
3189 elements = Fcons (unmodified, Fcons (mask, Qnil));
3190
3191 /* Cache the parsing results on SYMBOL. */
3192 Fput (symbol, Qevent_symbol_element_mask,
3193 elements);
3194 Fput (symbol, Qevent_symbol_elements,
3195 Fcons (unmodified, lispy_modifier_list (modifiers)));
3196
3197 /* Since we know that SYMBOL is modifiers applied to unmodified,
3198 it would be nice to put that in unmodified's cache.
3199 But we can't, since we're not sure that parse_modifiers is
3200 canonical. */
3201
3202 return elements;
3203 }
3204 }
3205
3206 /* Apply the modifiers MODIFIERS to the symbol BASE.
3207 BASE must be unmodified.
3208
3209 This is like apply_modifiers_uncached, but uses BASE's
3210 Qmodifier_cache property, if present. It also builds
3211 Qevent_symbol_elements properties, since it has that info anyway.
3212
3213 apply_modifiers copies the value of BASE's Qevent_kind property to
3214 the modified symbol. */
3215 static Lisp_Object
3216 apply_modifiers (modifiers, base)
3217 int modifiers;
3218 Lisp_Object base;
3219 {
3220 Lisp_Object cache, index, entry, new_symbol;
3221
3222 /* Mask out upper bits. We don't know where this value's been. */
3223 modifiers &= (1<<VALBITS) - 1;
3224
3225 /* The click modifier never figures into cache indices. */
3226 cache = Fget (base, Qmodifier_cache);
3227 XSETFASTINT (index, (modifiers & ~click_modifier));
3228 entry = assq_no_quit (index, cache);
3229
3230 if (CONSP (entry))
3231 new_symbol = XCONS (entry)->cdr;
3232 else
3233 {
3234 /* We have to create the symbol ourselves. */
3235 new_symbol = apply_modifiers_uncached (modifiers,
3236 XSYMBOL (base)->name->data,
3237 XSYMBOL (base)->name->size);
3238
3239 /* Add the new symbol to the base's cache. */
3240 entry = Fcons (index, new_symbol);
3241 Fput (base, Qmodifier_cache, Fcons (entry, cache));
3242
3243 /* We have the parsing info now for free, so add it to the caches. */
3244 XSETFASTINT (index, modifiers);
3245 Fput (new_symbol, Qevent_symbol_element_mask,
3246 Fcons (base, Fcons (index, Qnil)));
3247 Fput (new_symbol, Qevent_symbol_elements,
3248 Fcons (base, lispy_modifier_list (modifiers)));
3249 }
3250
3251 /* Make sure this symbol is of the same kind as BASE.
3252
3253 You'd think we could just set this once and for all when we
3254 intern the symbol above, but reorder_modifiers may call us when
3255 BASE's property isn't set right; we can't assume that just
3256 because it has a Qmodifier_cache property it must have its
3257 Qevent_kind set right as well. */
3258 if (NILP (Fget (new_symbol, Qevent_kind)))
3259 {
3260 Lisp_Object kind;
3261
3262 kind = Fget (base, Qevent_kind);
3263 if (! NILP (kind))
3264 Fput (new_symbol, Qevent_kind, kind);
3265 }
3266
3267 return new_symbol;
3268 }
3269
3270
3271 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
3272 return a symbol with the modifiers placed in the canonical order.
3273 Canonical order is alphabetical, except for down and drag, which
3274 always come last. The 'click' modifier is never written out.
3275
3276 Fdefine_key calls this to make sure that (for example) C-M-foo
3277 and M-C-foo end up being equivalent in the keymap. */
3278
3279 Lisp_Object
3280 reorder_modifiers (symbol)
3281 Lisp_Object symbol;
3282 {
3283 /* It's hopefully okay to write the code this way, since everything
3284 will soon be in caches, and no consing will be done at all. */
3285 Lisp_Object parsed;
3286
3287 parsed = parse_modifiers (symbol);
3288 return apply_modifiers (XCONS (XCONS (parsed)->cdr)->car,
3289 XCONS (parsed)->car);
3290 }
3291
3292
3293 /* For handling events, we often want to produce a symbol whose name
3294 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
3295 to some base, like the name of a function key or mouse button.
3296 modify_event_symbol produces symbols of this sort.
3297
3298 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
3299 is the name of the i'th symbol. TABLE_SIZE is the number of elements
3300 in the table.
3301
3302 Alternatively, NAME_ALIST is an alist mapping codes into symbol names.
3303 NAME_ALIST is used if it is non-nil; otherwise NAME_TABLE is used.
3304
3305 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
3306 persist between calls to modify_event_symbol that it can use to
3307 store a cache of the symbols it's generated for this NAME_TABLE
3308 before. The object stored there may be a vector or an alist.
3309
3310 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
3311
3312 MODIFIERS is a set of modifier bits (as given in struct input_events)
3313 whose prefixes should be applied to the symbol name.
3314
3315 SYMBOL_KIND is the value to be placed in the event_kind property of
3316 the returned symbol.
3317
3318 The symbols we create are supposed to have an
3319 `event-symbol-elements' property, which lists the modifiers present
3320 in the symbol's name. */
3321
3322 static Lisp_Object
3323 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist,
3324 name_table, symbol_table, table_size)
3325 int symbol_num;
3326 unsigned modifiers;
3327 Lisp_Object symbol_kind;
3328 Lisp_Object name_alist;
3329 char **name_table;
3330 Lisp_Object *symbol_table;
3331 int table_size;
3332 {
3333 Lisp_Object value;
3334 Lisp_Object symbol_int;
3335
3336 XSETINT (symbol_int, symbol_num);
3337
3338 /* Is this a request for a valid symbol? */
3339 if (symbol_num < 0 || symbol_num >= table_size)
3340 return Qnil;
3341
3342 if (CONSP (*symbol_table))
3343 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
3344
3345 /* If *symbol_table doesn't seem to be initialized properly, fix that.
3346 *symbol_table should be a lisp vector TABLE_SIZE elements long,
3347 where the Nth element is the symbol for NAME_TABLE[N], or nil if
3348 we've never used that symbol before. */
3349 else
3350 {
3351 if (! VECTORP (*symbol_table)
3352 || XVECTOR (*symbol_table)->size != table_size)
3353 {
3354 Lisp_Object size;
3355
3356 XSETFASTINT (size, table_size);
3357 *symbol_table = Fmake_vector (size, Qnil);
3358 }
3359
3360 value = XVECTOR (*symbol_table)->contents[symbol_num];
3361 }
3362
3363 /* Have we already used this symbol before? */
3364 if (NILP (value))
3365 {
3366 /* No; let's create it. */
3367 if (!NILP (name_alist))
3368 value = Fcdr_safe (Fassq (symbol_int, name_alist));
3369 else if (name_table[symbol_num])
3370 value = intern (name_table[symbol_num]);
3371
3372 if (NILP (value))
3373 {
3374 char buf[20];
3375 sprintf (buf, "key-%d", symbol_num);
3376 value = intern (buf);
3377 }
3378
3379 if (CONSP (*symbol_table))
3380 *symbol_table = Fcons (value, *symbol_table);
3381 else
3382 XVECTOR (*symbol_table)->contents[symbol_num] = value;
3383
3384 /* Fill in the cache entries for this symbol; this also
3385 builds the Qevent_symbol_elements property, which the user
3386 cares about. */
3387 apply_modifiers (modifiers & click_modifier, value);
3388 Fput (value, Qevent_kind, symbol_kind);
3389 }
3390
3391 /* Apply modifiers to that symbol. */
3392 return apply_modifiers (modifiers, value);
3393 }
3394
3395 \f
3396 /* Store into *addr a value nonzero if terminal input chars are available.
3397 Serves the purpose of ioctl (0, FIONREAD, addr)
3398 but works even if FIONREAD does not exist.
3399 (In fact, this may actually read some input.) */
3400
3401 static void
3402 get_input_pending (addr)
3403 int *addr;
3404 {
3405 /* First of all, have we already counted some input? */
3406 *addr = !NILP (Vquit_flag) || readable_events ();
3407
3408 /* If input is being read as it arrives, and we have none, there is none. */
3409 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
3410 return;
3411
3412 /* Try to read some input and see how much we get. */
3413 gobble_input (0);
3414 *addr = !NILP (Vquit_flag) || readable_events ();
3415 }
3416
3417 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
3418
3419 int
3420 gobble_input (expected)
3421 int expected;
3422 {
3423 #ifndef VMS
3424 #ifdef SIGIO
3425 if (interrupt_input)
3426 {
3427 SIGMASKTYPE mask;
3428 mask = sigblockx (SIGIO);
3429 read_avail_input (expected);
3430 sigsetmask (mask);
3431 }
3432 else
3433 #ifdef POLL_FOR_INPUT
3434 if (read_socket_hook && !interrupt_input && poll_suppress_count == 0)
3435 {
3436 SIGMASKTYPE mask;
3437 mask = sigblockx (SIGALRM);
3438 read_avail_input (expected);
3439 sigsetmask (mask);
3440 }
3441 else
3442 #endif
3443 #endif
3444 read_avail_input (expected);
3445 #endif
3446 }
3447
3448 /* Put a buffer_switch_event in the buffer
3449 so that read_key_sequence will notice the new current buffer. */
3450
3451 record_asynch_buffer_change ()
3452 {
3453 struct input_event event;
3454 Lisp_Object tem;
3455
3456 event.kind = buffer_switch_event;
3457 event.frame_or_window = Qnil;
3458
3459 #ifdef subprocesses
3460 /* We don't need a buffer-switch event unless Emacs is waiting for input.
3461 The purpose of the event is to make read_key_sequence look up the
3462 keymaps again. If we aren't in read_key_sequence, we don't need one,
3463 and the event could cause trouble by messing up (input-pending-p). */
3464 tem = Fwaiting_for_user_input_p ();
3465 if (NILP (tem))
3466 return;
3467 #else
3468 /* We never need these events if we have no asynchronous subprocesses. */
3469 return;
3470 #endif
3471
3472 /* Make sure no interrupt happens while storing the event. */
3473 #ifdef SIGIO
3474 if (interrupt_input)
3475 {
3476 SIGMASKTYPE mask;
3477 mask = sigblockx (SIGIO);
3478 kbd_buffer_store_event (&event);
3479 sigsetmask (mask);
3480 }
3481 else
3482 #endif
3483 {
3484 stop_polling ();
3485 kbd_buffer_store_event (&event);
3486 start_polling ();
3487 }
3488 }
3489 \f
3490 #ifndef VMS
3491
3492 /* Read any terminal input already buffered up by the system
3493 into the kbd_buffer, but do not wait.
3494
3495 EXPECTED should be nonzero if the caller knows there is some input.
3496
3497 Except on VMS, all input is read by this function.
3498 If interrupt_input is nonzero, this function MUST be called
3499 only when SIGIO is blocked.
3500
3501 Returns the number of keyboard chars read, or -1 meaning
3502 this is a bad time to try to read input. */
3503
3504 static int
3505 read_avail_input (expected)
3506 int expected;
3507 {
3508 struct input_event buf[KBD_BUFFER_SIZE];
3509 register int i;
3510 int nread;
3511
3512 if (read_socket_hook)
3513 /* No need for FIONREAD or fcntl; just say don't wait. */
3514 nread = (*read_socket_hook) (0, buf, KBD_BUFFER_SIZE, expected, expected);
3515 else
3516 {
3517 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
3518 the kbd_buffer can really hold. That may prevent loss
3519 of characters on some systems when input is stuffed at us. */
3520 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
3521 int n_to_read;
3522
3523 /* Determine how many characters we should *try* to read. */
3524 #ifdef MSDOS
3525 n_to_read = dos_keysns ();
3526 if (n_to_read == 0)
3527 return 0;
3528 #else /* not MSDOS */
3529 #ifdef FIONREAD
3530 /* Find out how much input is available. */
3531 if (ioctl (0, FIONREAD, &n_to_read) < 0)
3532 /* Formerly simply reported no input, but that sometimes led to
3533 a failure of Emacs to terminate.
3534 SIGHUP seems appropriate if we can't reach the terminal. */
3535 /* ??? Is it really right to send the signal just to this process
3536 rather than to the whole process group?
3537 Perhaps on systems with FIONREAD Emacs is alone in its group. */
3538 kill (getpid (), SIGHUP);
3539 if (n_to_read == 0)
3540 return 0;
3541 if (n_to_read > sizeof cbuf)
3542 n_to_read = sizeof cbuf;
3543 #else /* no FIONREAD */
3544 #if defined(USG) || defined(DGUX)
3545 /* Read some input if available, but don't wait. */
3546 n_to_read = sizeof cbuf;
3547 fcntl (fileno (stdin), F_SETFL, O_NDELAY);
3548 #else
3549 you lose;
3550 #endif
3551 #endif
3552 #endif /* not MSDOS */
3553
3554 /* Now read; for one reason or another, this will not block.
3555 NREAD is set to the number of chars read. */
3556 do
3557 {
3558 #ifdef MSDOS
3559 cbuf[0] = dos_keyread();
3560 nread = 1;
3561 #else
3562 nread = read (fileno (stdin), cbuf, n_to_read);
3563 #endif
3564 #if defined (AIX) && (! defined (aix386) && defined (_BSD))
3565 /* The kernel sometimes fails to deliver SIGHUP for ptys.
3566 This looks incorrect, but it isn't, because _BSD causes
3567 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
3568 and that causes a value other than 0 when there is no input. */
3569 if (nread == 0)
3570 kill (0, SIGHUP);
3571 #endif
3572 }
3573 while (
3574 /* We used to retry the read if it was interrupted.
3575 But this does the wrong thing when O_NDELAY causes
3576 an EAGAIN error. Does anybody know of a situation
3577 where a retry is actually needed? */
3578 #if 0
3579 nread < 0 && (errno == EAGAIN
3580 #ifdef EFAULT
3581 || errno == EFAULT
3582 #endif
3583 #ifdef EBADSLT
3584 || errno == EBADSLT
3585 #endif
3586 )
3587 #else
3588 0
3589 #endif
3590 );
3591
3592 #ifndef FIONREAD
3593 #if defined (USG) || defined (DGUX)
3594 fcntl (fileno (stdin), F_SETFL, 0);
3595 #endif /* USG or DGUX */
3596 #endif /* no FIONREAD */
3597 for (i = 0; i < nread; i++)
3598 {
3599 buf[i].kind = ascii_keystroke;
3600 buf[i].modifiers = 0;
3601 if (meta_key == 1 && (cbuf[i] & 0x80))
3602 buf[i].modifiers = meta_modifier;
3603 if (meta_key != 2)
3604 cbuf[i] &= ~0x80;
3605
3606 XSETINT (buf[i].code, cbuf[i]);
3607 #ifdef MULTI_FRAME
3608 XSETFRAME (buf[i].frame_or_window, selected_frame);
3609 #else
3610 buf[i].frame_or_window = Qnil;
3611 #endif
3612 }
3613 }
3614
3615 /* Scan the chars for C-g and store them in kbd_buffer. */
3616 for (i = 0; i < nread; i++)
3617 {
3618 kbd_buffer_store_event (&buf[i]);
3619 /* Don't look at input that follows a C-g too closely.
3620 This reduces lossage due to autorepeat on C-g. */
3621 if (buf[i].kind == ascii_keystroke
3622 && buf[i].code == quit_char)
3623 break;
3624 }
3625
3626 return nread;
3627 }
3628 #endif /* not VMS */
3629 \f
3630 #ifdef SIGIO /* for entire page */
3631 /* Note SIGIO has been undef'd if FIONREAD is missing. */
3632
3633 SIGTYPE
3634 input_available_signal (signo)
3635 int signo;
3636 {
3637 /* Must preserve main program's value of errno. */
3638 int old_errno = errno;
3639 #ifdef BSD4_1
3640 extern int select_alarmed;
3641 #endif
3642
3643 #ifdef USG
3644 /* USG systems forget handlers when they are used;
3645 must reestablish each time */
3646 signal (signo, input_available_signal);
3647 #endif /* USG */
3648
3649 #ifdef BSD4_1
3650 sigisheld (SIGIO);
3651 #endif
3652
3653 if (input_available_clear_time)
3654 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
3655
3656 while (1)
3657 {
3658 int nread;
3659 nread = read_avail_input (1);
3660 /* -1 means it's not ok to read the input now.
3661 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
3662 0 means there was no keyboard input available. */
3663 if (nread <= 0)
3664 break;
3665
3666 #ifdef BSD4_1
3667 select_alarmed = 1; /* Force the select emulator back to life */
3668 #endif
3669 }
3670
3671 #ifdef BSD4_1
3672 sigfree ();
3673 #endif
3674 errno = old_errno;
3675 }
3676 #endif /* SIGIO */
3677
3678 /* Send ourselves a SIGIO.
3679
3680 This function exists so that the UNBLOCK_INPUT macro in
3681 blockinput.h can have some way to take care of input we put off
3682 dealing with, without assuming that every file which uses
3683 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
3684 void
3685 reinvoke_input_signal ()
3686 {
3687 #ifdef SIGIO
3688 kill (0, SIGIO);
3689 #endif
3690 }
3691
3692
3693 \f
3694 /* Return the prompt-string of a sparse keymap.
3695 This is the first element which is a string.
3696 Return nil if there is none. */
3697
3698 Lisp_Object
3699 map_prompt (map)
3700 Lisp_Object map;
3701 {
3702 while (CONSP (map))
3703 {
3704 register Lisp_Object tem;
3705 tem = Fcar (map);
3706 if (STRINGP (tem))
3707 return tem;
3708 map = Fcdr (map);
3709 }
3710 return Qnil;
3711 }
3712
3713 static void menu_bar_item ();
3714 static void menu_bar_one_keymap ();
3715
3716 /* These variables hold the vector under construction within
3717 menu_bar_items and its subroutines, and the current index
3718 for storing into that vector. */
3719 static Lisp_Object menu_bar_items_vector;
3720 static int menu_bar_items_index;
3721
3722 /* Return a vector of menu items for a menu bar, appropriate
3723 to the current buffer. Each item has three elements in the vector:
3724 KEY STRING MAPLIST.
3725
3726 OLD is an old vector we can optionally reuse, or nil. */
3727
3728 Lisp_Object
3729 menu_bar_items (old)
3730 Lisp_Object old;
3731 {
3732 /* The number of keymaps we're scanning right now, and the number of
3733 keymaps we have allocated space for. */
3734 int nmaps;
3735
3736 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
3737 in the current keymaps, or nil where it is not a prefix. */
3738 Lisp_Object *maps;
3739
3740 Lisp_Object def, tem, tail;
3741
3742 Lisp_Object result;
3743
3744 int mapno;
3745 Lisp_Object oquit;
3746
3747 int i;
3748
3749 struct gcpro gcpro1;
3750
3751 /* In order to build the menus, we need to call the keymap
3752 accessors. They all call QUIT. But this function is called
3753 during redisplay, during which a quit is fatal. So inhibit
3754 quitting while building the menus.
3755 We do this instead of specbind because (1) errors will clear it anyway
3756 and (2) this avoids risk of specpdl overflow. */
3757 oquit = Vinhibit_quit;
3758 Vinhibit_quit = Qt;
3759
3760 if (!NILP (old))
3761 menu_bar_items_vector = old;
3762 else
3763 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
3764 menu_bar_items_index = 0;
3765
3766 GCPRO1 (menu_bar_items_vector);
3767
3768 /* Build our list of keymaps.
3769 If we recognize a function key and replace its escape sequence in
3770 keybuf with its symbol, or if the sequence starts with a mouse
3771 click and we need to switch buffers, we jump back here to rebuild
3772 the initial keymaps from the current buffer. */
3773 {
3774 Lisp_Object *tmaps;
3775
3776 if (!NILP (Voverriding_local_map))
3777 {
3778 nmaps = 2;
3779 maps = (Lisp_Object *) alloca (nmaps * sizeof (maps[0]));
3780 maps[0] = Voverriding_local_map;
3781 }
3782 else
3783 {
3784 nmaps = current_minor_maps (0, &tmaps) + 2;
3785 maps = (Lisp_Object *) alloca (nmaps * sizeof (maps[0]));
3786 bcopy (tmaps, maps, (nmaps - 2) * sizeof (maps[0]));
3787 #ifdef USE_TEXT_PROPERTIES
3788 maps[nmaps-2] = get_local_map (PT, current_buffer);
3789 #else
3790 maps[nmaps-2] = current_buffer->keymap;
3791 #endif
3792 }
3793 maps[nmaps-1] = current_global_map;
3794 }
3795
3796 /* Look up in each map the dummy prefix key `menu-bar'. */
3797
3798 result = Qnil;
3799
3800 for (mapno = nmaps - 1; mapno >= 0; mapno--)
3801 {
3802 if (! NILP (maps[mapno]))
3803 def = get_keyelt (access_keymap (maps[mapno], Qmenu_bar, 1, 0));
3804 else
3805 def = Qnil;
3806
3807 tem = Fkeymapp (def);
3808 if (!NILP (tem))
3809 menu_bar_one_keymap (def);
3810 }
3811
3812 /* Move to the end those items that should be at the end. */
3813
3814 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCONS (tail)->cdr)
3815 {
3816 int i;
3817 int end = menu_bar_items_index;
3818
3819 for (i = 0; i < end; i += 3)
3820 if (EQ (XCONS (tail)->car, XVECTOR (menu_bar_items_vector)->contents[i]))
3821 {
3822 Lisp_Object tem0, tem1, tem2;
3823 /* Move the item at index I to the end,
3824 shifting all the others forward. */
3825 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
3826 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
3827 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
3828 if (end > i + 3)
3829 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 3],
3830 &XVECTOR (menu_bar_items_vector)->contents[i],
3831 (end - i - 3) * sizeof (Lisp_Object));
3832 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem0;
3833 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem1;
3834 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem2;
3835 break;
3836 }
3837 }
3838
3839 /* Add nil, nil, nil at the end. */
3840 i = menu_bar_items_index;
3841 if (i + 3 > XVECTOR (menu_bar_items_vector)->size)
3842 {
3843 Lisp_Object tem;
3844 int newsize = 2 * i;
3845 tem = Fmake_vector (make_number (2 * i), Qnil);
3846 bcopy (XVECTOR (menu_bar_items_vector)->contents,
3847 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
3848 menu_bar_items_vector = tem;
3849 }
3850 /* Add this item. */
3851 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
3852 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
3853 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
3854 menu_bar_items_index = i;
3855
3856 Vinhibit_quit = oquit;
3857 UNGCPRO;
3858 return menu_bar_items_vector;
3859 }
3860 \f
3861 /* Scan one map KEYMAP, accumulating any menu items it defines
3862 in menu_bar_items_vector. */
3863
3864 static void
3865 menu_bar_one_keymap (keymap)
3866 Lisp_Object keymap;
3867 {
3868 Lisp_Object tail, item, key, binding, item_string, table;
3869
3870 /* Loop over all keymap entries that have menu strings. */
3871 for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
3872 {
3873 item = XCONS (tail)->car;
3874 if (CONSP (item))
3875 {
3876 key = XCONS (item)->car;
3877 binding = XCONS (item)->cdr;
3878 if (CONSP (binding))
3879 {
3880 item_string = XCONS (binding)->car;
3881 if (STRINGP (item_string))
3882 menu_bar_item (key, item_string, Fcdr (binding));
3883 }
3884 else if (EQ (binding, Qundefined))
3885 menu_bar_item (key, Qnil, binding);
3886 }
3887 else if (VECTORP (item))
3888 {
3889 /* Loop over the char values represented in the vector. */
3890 int len = XVECTOR (item)->size;
3891 int c;
3892 for (c = 0; c < len; c++)
3893 {
3894 Lisp_Object character;
3895 XSETFASTINT (character, c);
3896 binding = XVECTOR (item)->contents[c];
3897 if (CONSP (binding))
3898 {
3899 item_string = XCONS (binding)->car;
3900 if (STRINGP (item_string))
3901 menu_bar_item (key, item_string, Fcdr (binding));
3902 }
3903 else if (EQ (binding, Qundefined))
3904 menu_bar_item (key, Qnil, binding);
3905 }
3906 }
3907 }
3908 }
3909
3910 /* This is used as the handler when calling internal_condition_case_1. */
3911
3912 static Lisp_Object
3913 menu_bar_item_1 (arg)
3914 Lisp_Object arg;
3915 {
3916 return Qnil;
3917 }
3918
3919 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
3920 If there's already an item for KEY, add this DEF to it. */
3921
3922 static void
3923 menu_bar_item (key, item_string, def)
3924 Lisp_Object key, item_string, def;
3925 {
3926 Lisp_Object tem;
3927 Lisp_Object enabled;
3928 int i;
3929
3930 if (EQ (def, Qundefined))
3931 {
3932 /* If a map has an explicit `undefined' as definition,
3933 discard any previously made menu bar item. */
3934
3935 for (i = 0; i < menu_bar_items_index; i += 3)
3936 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
3937 {
3938 if (menu_bar_items_index > i + 3)
3939 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 3],
3940 &XVECTOR (menu_bar_items_vector)->contents[i],
3941 (menu_bar_items_index - i - 3) * sizeof (Lisp_Object));
3942 menu_bar_items_index -= 3;
3943 return;
3944 }
3945
3946 /* If there's no definition for this key yet,
3947 just ignore `undefined'. */
3948 return;
3949 }
3950
3951 /* See if this entry is enabled. */
3952 enabled = Qt;
3953
3954 if (SYMBOLP (def))
3955 {
3956 /* No property, or nil, means enable.
3957 Otherwise, enable if value is not nil. */
3958 tem = Fget (def, Qmenu_enable);
3959 if (!NILP (tem))
3960 /* (condition-case nil (eval tem)
3961 (error nil)) */
3962 enabled = internal_condition_case_1 (Feval, tem, Qerror,
3963 menu_bar_item_1);
3964 }
3965
3966 /* Ignore this item if it's not enabled. */
3967 if (NILP (enabled))
3968 return;
3969
3970 /* Find any existing item for this KEY. */
3971 for (i = 0; i < menu_bar_items_index; i += 3)
3972 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
3973 break;
3974
3975 /* If we did not find this KEY, add it at the end. */
3976 if (i == menu_bar_items_index)
3977 {
3978 /* If vector is too small, get a bigger one. */
3979 if (i + 3 > XVECTOR (menu_bar_items_vector)->size)
3980 {
3981 Lisp_Object tem;
3982 int newsize = 2 * i;
3983 tem = Fmake_vector (make_number (2 * i), Qnil);
3984 bcopy (XVECTOR (menu_bar_items_vector)->contents,
3985 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
3986 menu_bar_items_vector = tem;
3987 }
3988 /* Add this item. */
3989 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
3990 XVECTOR (menu_bar_items_vector)->contents[i++] = item_string;
3991 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (def, Qnil);
3992 menu_bar_items_index = i;
3993 }
3994 /* We did find an item for this KEY. Add DEF to its list of maps. */
3995 else
3996 {
3997 Lisp_Object old;
3998 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
3999 XVECTOR (menu_bar_items_vector)->contents[i + 2] = Fcons (def, old);
4000 }
4001 }
4002 \f
4003 /* Read a character using menus based on maps in the array MAPS.
4004 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
4005 Return t if we displayed a menu but the user rejected it.
4006
4007 PREV_EVENT is the previous input event, or nil if we are reading
4008 the first event of a key sequence.
4009
4010 If USED_MOUSE_MENU is non-zero, then we set *USED_MOUSE_MENU to 1
4011 if we used a mouse menu to read the input, or zero otherwise. If
4012 USED_MOUSE_MENU is zero, *USED_MOUSE_MENU is left alone.
4013
4014 The prompting is done based on the prompt-string of the map
4015 and the strings associated with various map elements.
4016
4017 This can be done with X menus or with menus put in the minibuf.
4018 These are done in different ways, depending on how the input will be read.
4019 Menus using X are done after auto-saving in read-char, getting the input
4020 event from Fx_popup_menu; menus using the minibuf use read_char recursively
4021 and do auto-saving in the inner call of read_char. */
4022
4023 static Lisp_Object
4024 read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
4025 int nmaps;
4026 Lisp_Object *maps;
4027 Lisp_Object prev_event;
4028 int *used_mouse_menu;
4029 {
4030 int mapno;
4031 register Lisp_Object name;
4032 Lisp_Object rest, vector;
4033
4034 if (used_mouse_menu)
4035 *used_mouse_menu = 0;
4036
4037 /* Use local over global Menu maps */
4038
4039 if (! menu_prompting)
4040 return Qnil;
4041
4042 /* Optionally disregard all but the global map. */
4043 if (inhibit_local_menu_bar_menus)
4044 {
4045 maps += (nmaps - 1);
4046 nmaps = 1;
4047 }
4048
4049 /* Get the menu name from the first map that has one (a prompt string). */
4050 for (mapno = 0; mapno < nmaps; mapno++)
4051 {
4052 name = map_prompt (maps[mapno]);
4053 if (!NILP (name))
4054 break;
4055 }
4056
4057 /* If we don't have any menus, just read a character normally. */
4058 if (mapno >= nmaps)
4059 return Qnil;
4060
4061 #if (defined (HAVE_X_WINDOWS) && defined (HAVE_X_MENU)) || defined (MSDOS)
4062 /* If we got to this point via a mouse click,
4063 use a real menu for mouse selection. */
4064 if (EVENT_HAS_PARAMETERS (prev_event))
4065 {
4066 /* Display the menu and get the selection. */
4067 Lisp_Object *realmaps
4068 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
4069 Lisp_Object value;
4070 int nmaps1 = 0;
4071
4072 /* Use the maps that are not nil. */
4073 for (mapno = 0; mapno < nmaps; mapno++)
4074 if (!NILP (maps[mapno]))
4075 realmaps[nmaps1++] = maps[mapno];
4076
4077 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
4078 if (CONSP (value))
4079 {
4080 /* If we got more than one event, put all but the first
4081 onto this list to be read later.
4082 Return just the first event now. */
4083 Vunread_command_events
4084 = nconc2 (XCONS (value)->cdr, Vunread_command_events);
4085 value = XCONS (value)->car;
4086 }
4087 else if (NILP (value))
4088 value = Qt;
4089 if (used_mouse_menu)
4090 *used_mouse_menu = 1;
4091 return value;
4092 }
4093 #endif /* (HAVE_X_WINDOWS && HAVE_X_MENU) || MSDOS */
4094 return Qnil ;
4095 }
4096
4097 static Lisp_Object
4098 read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
4099 int commandflag ;
4100 int nmaps;
4101 Lisp_Object *maps;
4102 {
4103 int mapno;
4104 register Lisp_Object name;
4105 int nlength;
4106 int width = FRAME_WIDTH (selected_frame) - 4;
4107 char *menu = (char *) alloca (width + 4);
4108 int idx = -1;
4109 int nobindings = 1;
4110 Lisp_Object rest, vector;
4111
4112 if (! menu_prompting)
4113 return Qnil;
4114
4115 /* Get the menu name from the first map that has one (a prompt string). */
4116 for (mapno = 0; mapno < nmaps; mapno++)
4117 {
4118 name = map_prompt (maps[mapno]);
4119 if (!NILP (name))
4120 break;
4121 }
4122
4123 /* If we don't have any menus, just read a character normally. */
4124 if (mapno >= nmaps)
4125 return Qnil;
4126
4127 /* Prompt string always starts with map's prompt, and a space. */
4128 strcpy (menu, XSTRING (name)->data);
4129 nlength = XSTRING (name)->size;
4130 menu[nlength++] = ':';
4131 menu[nlength++] = ' ';
4132 menu[nlength] = 0;
4133
4134 /* Start prompting at start of first map. */
4135 mapno = 0;
4136 rest = maps[mapno];
4137
4138 /* Present the documented bindings, a line at a time. */
4139 while (1)
4140 {
4141 int notfirst = 0;
4142 int i = nlength;
4143 Lisp_Object obj;
4144 int ch;
4145 int orig_defn_macro ;
4146
4147 /* Loop over elements of map. */
4148 while (i < width)
4149 {
4150 Lisp_Object s, elt;
4151
4152 /* If reached end of map, start at beginning of next map. */
4153 if (NILP (rest))
4154 {
4155 mapno++;
4156 /* At end of last map, wrap around to first map if just starting,
4157 or end this line if already have something on it. */
4158 if (mapno == nmaps)
4159 {
4160 mapno = 0;
4161 if (notfirst || nobindings) break;
4162 }
4163 rest = maps[mapno];
4164 }
4165
4166 /* Look at the next element of the map. */
4167 if (idx >= 0)
4168 elt = XVECTOR (vector)->contents[idx];
4169 else
4170 elt = Fcar_safe (rest);
4171
4172 if (idx < 0 && VECTORP (elt))
4173 {
4174 /* If we found a dense table in the keymap,
4175 advanced past it, but start scanning its contents. */
4176 rest = Fcdr_safe (rest);
4177 vector = elt;
4178 idx = 0;
4179 }
4180 else
4181 {
4182 /* An ordinary element. */
4183 if ( idx < 0 )
4184 s = Fcar_safe (Fcdr_safe (elt)); /* alist */
4185 else
4186 s = Fcar_safe(elt); /* vector */
4187 if (!STRINGP (s))
4188 /* Ignore the element if it has no prompt string. */
4189 ;
4190 /* If we have room for the prompt string, add it to this line.
4191 If this is the first on the line, always add it. */
4192 else if (XSTRING (s)->size + i + 2 < width
4193 || !notfirst)
4194 {
4195 int thiswidth;
4196
4197 /* Punctuate between strings. */
4198 if (notfirst)
4199 {
4200 strcpy (menu + i, ", ");
4201 i += 2;
4202 }
4203 notfirst = 1;
4204 nobindings = 0 ;
4205
4206 /* Add as much of string as fits. */
4207 thiswidth = XSTRING (s)->size;
4208 if (thiswidth + i > width)
4209 thiswidth = width - i;
4210 bcopy (XSTRING (s)->data, menu + i, thiswidth);
4211 i += thiswidth;
4212 menu[i] = 0;
4213 }
4214 else
4215 {
4216 /* If this element does not fit, end the line now,
4217 and save the element for the next line. */
4218 strcpy (menu + i, "...");
4219 break;
4220 }
4221
4222 /* Move past this element. */
4223 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
4224 /* Handle reaching end of dense table. */
4225 idx = -1;
4226 if (idx >= 0)
4227 idx++;
4228 else
4229 rest = Fcdr_safe (rest);
4230 }
4231 }
4232
4233 /* Prompt with that and read response. */
4234 message1 (menu);
4235
4236 /* Make believe its not a keyboard macro in case the help char
4237 is pressed. Help characters are not recorded because menu prompting
4238 is not used on replay.
4239 */
4240 orig_defn_macro = defining_kbd_macro ;
4241 defining_kbd_macro = 0 ;
4242 do
4243 obj = read_char (commandflag, 0, 0, Qnil, 0);
4244 while (BUFFERP (obj));
4245 defining_kbd_macro = orig_defn_macro ;
4246
4247 if (!INTEGERP (obj))
4248 return obj;
4249 else
4250 ch = XINT (obj);
4251
4252 if (! EQ (obj, menu_prompt_more_char)
4253 && (!INTEGERP (menu_prompt_more_char)
4254 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
4255 {
4256 if ( defining_kbd_macro )
4257 store_kbd_macro_char(obj) ;
4258 return obj;
4259 }
4260 /* Help char - go round again */
4261 }
4262 }
4263 \f
4264 /* Reading key sequences. */
4265
4266 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
4267 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
4268 keymap, or nil otherwise. Return the index of the first keymap in
4269 which KEY has any binding, or NMAPS if no map has a binding.
4270
4271 If KEY is a meta ASCII character, treat it like meta-prefix-char
4272 followed by the corresponding non-meta character. Keymaps in
4273 CURRENT with non-prefix bindings for meta-prefix-char become nil in
4274 NEXT.
4275
4276 If KEY has no bindings in any of the CURRENT maps, NEXT is left
4277 unmodified.
4278
4279 NEXT may == CURRENT. */
4280
4281 static int
4282 follow_key (key, nmaps, current, defs, next)
4283 Lisp_Object key;
4284 Lisp_Object *current, *defs, *next;
4285 int nmaps;
4286 {
4287 int i, first_binding;
4288
4289 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
4290 followed by the corresponding non-meta character. */
4291 if (INTEGERP (key) && (XINT (key) & CHAR_META))
4292 {
4293 for (i = 0; i < nmaps; i++)
4294 if (! NILP (current[i]))
4295 {
4296 next[i] =
4297 get_keyelt (access_keymap (current[i], meta_prefix_char, 1, 0));
4298
4299 /* Note that since we pass the resulting bindings through
4300 get_keymap_1, non-prefix bindings for meta-prefix-char
4301 disappear. */
4302 next[i] = get_keymap_1 (next[i], 0, 1);
4303 }
4304 else
4305 next[i] = Qnil;
4306
4307 current = next;
4308 XSETINT (key, XFASTINT (key) & ~CHAR_META);
4309 }
4310
4311 first_binding = nmaps;
4312 for (i = nmaps - 1; i >= 0; i--)
4313 {
4314 if (! NILP (current[i]))
4315 {
4316 defs[i] = get_keyelt (access_keymap (current[i], key, 1, 0));
4317 if (! NILP (defs[i]))
4318 first_binding = i;
4319 }
4320 else
4321 defs[i] = Qnil;
4322 }
4323
4324 /* Given the set of bindings we've found, produce the next set of maps. */
4325 if (first_binding < nmaps)
4326 for (i = 0; i < nmaps; i++)
4327 next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0, 1);
4328
4329 return first_binding;
4330 }
4331
4332 /* Read a sequence of keys that ends with a non prefix character,
4333 storing it in KEYBUF, a buffer of size BUFSIZE.
4334 Prompt with PROMPT.
4335 Return the length of the key sequence stored.
4336 Return -1 if the user rejected a command menu.
4337
4338 Echo starting immediately unless `prompt' is 0.
4339
4340 Where a key sequence ends depends on the currently active keymaps.
4341 These include any minor mode keymaps active in the current buffer,
4342 the current buffer's local map, and the global map.
4343
4344 If a key sequence has no other bindings, we check Vfunction_key_map
4345 to see if some trailing subsequence might be the beginning of a
4346 function key's sequence. If so, we try to read the whole function
4347 key, and substitute its symbolic name into the key sequence.
4348
4349 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
4350 `double-' events into similar click events, if that would make them
4351 bound. We try to turn `triple-' events first into `double-' events,
4352 then into clicks.
4353
4354 If we get a mouse click in a mode line, vertical divider, or other
4355 non-text area, we treat the click as if it were prefixed by the
4356 symbol denoting that area - `mode-line', `vertical-line', or
4357 whatever.
4358
4359 If the sequence starts with a mouse click, we read the key sequence
4360 with respect to the buffer clicked on, not the current buffer.
4361
4362 If the user switches frames in the midst of a key sequence, we put
4363 off the switch-frame event until later; the next call to
4364 read_char will return it. */
4365
4366 static int
4367 read_key_sequence (keybuf, bufsize, prompt)
4368 Lisp_Object *keybuf;
4369 int bufsize;
4370 Lisp_Object prompt;
4371 {
4372 int count = specpdl_ptr - specpdl;
4373
4374 /* How many keys there are in the current key sequence. */
4375 int t;
4376
4377 /* The length of the echo buffer when we started reading, and
4378 the length of this_command_keys when we started reading. */
4379 int echo_start;
4380 int keys_start;
4381
4382 /* The number of keymaps we're scanning right now, and the number of
4383 keymaps we have allocated space for. */
4384 int nmaps;
4385 int nmaps_allocated = 0;
4386
4387 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
4388 the current keymaps. */
4389 Lisp_Object *defs;
4390
4391 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
4392 in the current keymaps, or nil where it is not a prefix. */
4393 Lisp_Object *submaps;
4394
4395 /* The index in defs[] of the first keymap that has a binding for
4396 this key sequence. In other words, the lowest i such that
4397 defs[i] is non-nil. */
4398 int first_binding;
4399
4400 /* If t < mock_input, then KEYBUF[t] should be read as the next
4401 input key.
4402
4403 We use this to recover after recognizing a function key. Once we
4404 realize that a suffix of the current key sequence is actually a
4405 function key's escape sequence, we replace the suffix with the
4406 function key's binding from Vfunction_key_map. Now keybuf
4407 contains a new and different key sequence, so the echo area,
4408 this_command_keys, and the submaps and defs arrays are wrong. In
4409 this situation, we set mock_input to t, set t to 0, and jump to
4410 restart_sequence; the loop will read keys from keybuf up until
4411 mock_input, thus rebuilding the state; and then it will resume
4412 reading characters from the keyboard. */
4413 int mock_input = 0;
4414
4415 /* If the sequence is unbound in submaps[], then
4416 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
4417 and fkey_map is its binding.
4418
4419 These might be > t, indicating that all function key scanning
4420 should hold off until t reaches them. We do this when we've just
4421 recognized a function key, to avoid searching for the function
4422 key's again in Vfunction_key_map. */
4423 int fkey_start = 0, fkey_end = 0;
4424 Lisp_Object fkey_map;
4425
4426 /* Likewise, for key_translation_map. */
4427 int keytran_start = 0, keytran_end = 0;
4428 Lisp_Object keytran_map;
4429
4430 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
4431 we put it off for later. While we're reading, we keep the event here. */
4432 Lisp_Object delayed_switch_frame;
4433
4434 /* See the comment below... */
4435 #if defined (GOBBLE_FIRST_EVENT)
4436 Lisp_Object first_event;
4437 #endif
4438
4439 struct buffer *starting_buffer;
4440
4441 /* Nonzero if we seem to have got the beginning of a binding
4442 in function_key_map. */
4443 int function_key_possible = 0;
4444 int key_translation_possible = 0;
4445
4446 int junk;
4447
4448 last_nonmenu_event = Qnil;
4449
4450 delayed_switch_frame = Qnil;
4451 fkey_map = Vfunction_key_map;
4452 keytran_map = Vkey_translation_map;
4453
4454 /* If there is no function-key-map, turn off function key scanning. */
4455 if (NILP (Fkeymapp (Vfunction_key_map)))
4456 fkey_start = fkey_end = bufsize + 1;
4457
4458 /* If there is no key-translation-map, turn off scanning. */
4459 if (NILP (Fkeymapp (Vkey_translation_map)))
4460 keytran_start = keytran_end = bufsize + 1;
4461
4462 if (INTERACTIVE)
4463 {
4464 if (!NILP (prompt))
4465 echo_prompt (XSTRING (prompt)->data);
4466 else if (cursor_in_echo_area && echo_keystrokes)
4467 /* This doesn't put in a dash if the echo buffer is empty, so
4468 you don't always see a dash hanging out in the minibuffer. */
4469 echo_dash ();
4470 }
4471
4472 /* Record the initial state of the echo area and this_command_keys;
4473 we will need to restore them if we replay a key sequence. */
4474 if (INTERACTIVE)
4475 echo_start = echo_length ();
4476 keys_start = this_command_key_count;
4477
4478 #if defined (GOBBLE_FIRST_EVENT)
4479 /* This doesn't quite work, because some of the things that read_char
4480 does cannot safely be bypassed. It seems too risky to try to make
4481 this work right. */
4482
4483 /* Read the first char of the sequence specially, before setting
4484 up any keymaps, in case a filter runs and switches buffers on us. */
4485 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
4486 &junk);
4487 #endif /* GOBBLE_FIRST_EVENT */
4488
4489 /* We jump here when the key sequence has been thoroughly changed, and
4490 we need to rescan it starting from the beginning. When we jump here,
4491 keybuf[0..mock_input] holds the sequence we should reread. */
4492 replay_sequence:
4493
4494 starting_buffer = current_buffer;
4495 function_key_possible = 0;
4496 key_translation_possible = 0;
4497
4498 /* Build our list of keymaps.
4499 If we recognize a function key and replace its escape sequence in
4500 keybuf with its symbol, or if the sequence starts with a mouse
4501 click and we need to switch buffers, we jump back here to rebuild
4502 the initial keymaps from the current buffer. */
4503 {
4504 Lisp_Object *maps;
4505
4506 if (!NILP (Voverriding_local_map))
4507 {
4508 nmaps = 2;
4509 if (nmaps > nmaps_allocated)
4510 {
4511 submaps = (Lisp_Object *) alloca (nmaps * sizeof (submaps[0]));
4512 defs = (Lisp_Object *) alloca (nmaps * sizeof (defs[0]));
4513 nmaps_allocated = nmaps;
4514 }
4515 submaps[0] = Voverriding_local_map;
4516 }
4517 else
4518 {
4519 nmaps = current_minor_maps (0, &maps) + 2;
4520 if (nmaps > nmaps_allocated)
4521 {
4522 submaps = (Lisp_Object *) alloca (nmaps * sizeof (submaps[0]));
4523 defs = (Lisp_Object *) alloca (nmaps * sizeof (defs[0]));
4524 nmaps_allocated = nmaps;
4525 }
4526 bcopy (maps, submaps, (nmaps - 2) * sizeof (submaps[0]));
4527 #ifdef USE_TEXT_PROPERTIES
4528 submaps[nmaps-2] = get_local_map (PT, current_buffer);
4529 #else
4530 submaps[nmaps-2] = current_buffer->keymap;
4531 #endif
4532 }
4533 submaps[nmaps-1] = current_global_map;
4534 }
4535
4536 /* Find an accurate initial value for first_binding. */
4537 for (first_binding = 0; first_binding < nmaps; first_binding++)
4538 if (! NILP (submaps[first_binding]))
4539 break;
4540
4541 /* Start from the beginning in keybuf. */
4542 t = 0;
4543
4544 /* These are no-ops the first time through, but if we restart, they
4545 revert the echo area and this_command_keys to their original state. */
4546 this_command_key_count = keys_start;
4547 if (INTERACTIVE && t < mock_input)
4548 echo_truncate (echo_start);
4549
4550 /* If the best binding for the current key sequence is a keymap, or
4551 we may be looking at a function key's escape sequence, keep on
4552 reading. */
4553 while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
4554 || (first_binding >= nmaps
4555 && fkey_start < t
4556 /* mock input is never part of a function key's sequence. */
4557 && mock_input <= fkey_start)
4558 || (keytran_start < t && key_translation_possible)
4559 /* Don't return in the middle of a possible function key sequence,
4560 if the only bindings we found were via case conversion.
4561 Thus, if ESC O a has a function-key-map translation
4562 and ESC o has a binding, don't return after ESC O,
4563 so that we can translate ESC O plus the next character. */
4564 )
4565 {
4566 Lisp_Object key;
4567 int used_mouse_menu = 0;
4568
4569 /* Where the last real key started. If we need to throw away a
4570 key that has expanded into more than one element of keybuf
4571 (say, a mouse click on the mode line which is being treated
4572 as [mode-line (mouse-...)], then we backtrack to this point
4573 of keybuf. */
4574 int last_real_key_start;
4575
4576 /* These variables are analogous to echo_start and keys_start;
4577 while those allow us to restart the entire key sequence,
4578 echo_local_start and keys_local_start allow us to throw away
4579 just one key. */
4580 int echo_local_start, keys_local_start, local_first_binding;
4581
4582 if (t >= bufsize)
4583 error ("key sequence too long");
4584
4585 if (INTERACTIVE)
4586 echo_local_start = echo_length ();
4587 keys_local_start = this_command_key_count;
4588 local_first_binding = first_binding;
4589
4590 replay_key:
4591 /* These are no-ops, unless we throw away a keystroke below and
4592 jumped back up to replay_key; in that case, these restore the
4593 variables to their original state, allowing us to replay the
4594 loop. */
4595 if (INTERACTIVE && t < mock_input)
4596 echo_truncate (echo_local_start);
4597 this_command_key_count = keys_local_start;
4598 first_binding = local_first_binding;
4599
4600 /* By default, assume each event is "real". */
4601 last_real_key_start = t;
4602
4603 /* Does mock_input indicate that we are re-reading a key sequence? */
4604 if (t < mock_input)
4605 {
4606 key = keybuf[t];
4607 add_command_key (key);
4608 if (echo_keystrokes)
4609 echo_char (key);
4610 }
4611
4612 /* If not, we should actually read a character. */
4613 else
4614 {
4615 struct buffer *buf = current_buffer;
4616
4617 key = read_char (NILP (prompt), nmaps, submaps, last_nonmenu_event,
4618 &used_mouse_menu);
4619
4620 /* read_char returns t when it shows a menu and the user rejects it.
4621 Just return -1. */
4622 if (EQ (key, Qt))
4623 return -1;
4624
4625 /* read_char returns -1 at the end of a macro.
4626 Emacs 18 handles this by returning immediately with a
4627 zero, so that's what we'll do. */
4628 if (INTEGERP (key) && XINT (key) == -1)
4629 {
4630 t = 0;
4631 goto done;
4632 }
4633
4634 /* If the current buffer has been changed from under us, the
4635 keymap may have changed, so replay the sequence. */
4636 if (BUFFERP (key))
4637 {
4638 mock_input = t;
4639 goto replay_sequence;
4640 }
4641
4642 /* If we have a quit that was typed in another frame, and
4643 quit_throw_to_read_char switched buffers,
4644 replay to get the right keymap. */
4645 if (XINT (key) == quit_char && current_buffer != starting_buffer)
4646 {
4647 keybuf[t++] = key;
4648 mock_input = t;
4649 Vquit_flag = Qnil;
4650 goto replay_sequence;
4651 }
4652
4653 Vquit_flag = Qnil;
4654 }
4655
4656 /* Clicks in non-text areas get prefixed by the symbol
4657 in their CHAR-ADDRESS field. For example, a click on
4658 the mode line is prefixed by the symbol `mode-line'.
4659
4660 Furthermore, key sequences beginning with mouse clicks
4661 are read using the keymaps of the buffer clicked on, not
4662 the current buffer. So we may have to switch the buffer
4663 here.
4664
4665 When we turn one event into two events, we must make sure
4666 that neither of the two looks like the original--so that,
4667 if we replay the events, they won't be expanded again.
4668 If not for this, such reexpansion could happen either here
4669 or when user programs play with this-command-keys. */
4670 if (EVENT_HAS_PARAMETERS (key))
4671 {
4672 Lisp_Object kind;
4673
4674 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
4675 if (EQ (kind, Qmouse_click))
4676 {
4677 Lisp_Object window, posn;
4678
4679 window = POSN_WINDOW (EVENT_START (key));
4680 posn = POSN_BUFFER_POSN (EVENT_START (key));
4681 if (CONSP (posn))
4682 {
4683 /* We're looking at the second event of a
4684 sequence which we expanded before. Set
4685 last_real_key_start appropriately. */
4686 if (t > 0)
4687 last_real_key_start = t - 1;
4688 }
4689
4690 /* Key sequences beginning with mouse clicks are
4691 read using the keymaps in the buffer clicked on,
4692 not the current buffer. If we're at the
4693 beginning of a key sequence, switch buffers. */
4694 if (last_real_key_start == 0
4695 && WINDOWP (window)
4696 && BUFFERP (XWINDOW (window)->buffer)
4697 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
4698 {
4699 keybuf[t] = key;
4700 mock_input = t + 1;
4701
4702 /* Arrange to go back to the original buffer once we're
4703 done reading the key sequence. Note that we can't
4704 use save_excursion_{save,restore} here, because they
4705 save point as well as the current buffer; we don't
4706 want to save point, because redisplay may change it,
4707 to accommodate a Fset_window_start or something. We
4708 don't want to do this at the top of the function,
4709 because we may get input from a subprocess which
4710 wants to change the selected window and stuff (say,
4711 emacsclient). */
4712 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
4713
4714 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
4715 goto replay_sequence;
4716 }
4717 else if (SYMBOLP (posn))
4718 {
4719 /* Expand mode-line and scroll-bar events into two events:
4720 use posn as a fake prefix key. */
4721
4722 if (t + 1 >= bufsize)
4723 error ("key sequence too long");
4724 keybuf[t] = posn;
4725 keybuf[t+1] = key;
4726 mock_input = t + 2;
4727
4728 /* Zap the position in key, so we know that we've
4729 expanded it, and don't try to do so again. */
4730 POSN_BUFFER_POSN (EVENT_START (key))
4731 = Fcons (posn, Qnil);
4732 goto replay_key;
4733 }
4734 }
4735 else if (EQ (kind, Qswitch_frame))
4736 {
4737 /* If we're at the beginning of a key sequence, go
4738 ahead and return this event. If we're in the
4739 midst of a key sequence, delay it until the end. */
4740 if (t > 0)
4741 {
4742 delayed_switch_frame = key;
4743 goto replay_key;
4744 }
4745 }
4746 else if (CONSP (XCONS (key)->cdr)
4747 && CONSP (EVENT_START (key))
4748 && CONSP (XCONS (EVENT_START (key))->cdr))
4749 {
4750 Lisp_Object posn;
4751
4752 posn = POSN_BUFFER_POSN (EVENT_START (key));
4753 /* Handle menu-bar events:
4754 insert the dummy prefix event `menu-bar'. */
4755 if (EQ (posn, Qmenu_bar))
4756 {
4757 if (t + 1 >= bufsize)
4758 error ("key sequence too long");
4759 /* Run the Lucid hook. */
4760 if (!NILP (Vrun_hooks))
4761 call1 (Vrun_hooks, Qactivate_menubar_hook);
4762 /* If it has changed current-menubar from previous value,
4763 really recompute the menubar from the value. */
4764 if (! NILP (Vlucid_menu_bar_dirty_flag))
4765 call0 (Qrecompute_lucid_menubar);
4766 keybuf[t] = posn;
4767 keybuf[t+1] = key;
4768
4769 /* Zap the position in key, so we know that we've
4770 expanded it, and don't try to do so again. */
4771 POSN_BUFFER_POSN (EVENT_START (key))
4772 = Fcons (posn, Qnil);
4773
4774 mock_input = t + 2;
4775 goto replay_sequence;
4776 }
4777 else if (CONSP (posn))
4778 {
4779 /* We're looking at the second event of a
4780 sequence which we expanded before. Set
4781 last_real_key_start appropriately. */
4782 if (last_real_key_start == t && t > 0)
4783 last_real_key_start = t - 1;
4784 }
4785 }
4786 }
4787
4788 /* We have finally decided that KEY is something we might want
4789 to look up. */
4790 first_binding = (follow_key (key,
4791 nmaps - first_binding,
4792 submaps + first_binding,
4793 defs + first_binding,
4794 submaps + first_binding)
4795 + first_binding);
4796
4797 /* If KEY wasn't bound, we'll try some fallbacks. */
4798 if (first_binding >= nmaps)
4799 {
4800 Lisp_Object head;
4801
4802 head = EVENT_HEAD (key);
4803 if (EQ (head, Vhelp_char))
4804 {
4805 read_key_sequence_cmd = Vprefix_help_command;
4806 keybuf[t++] = key;
4807 last_nonmenu_event = key;
4808 goto done;
4809 }
4810
4811 if (SYMBOLP (head))
4812 {
4813 Lisp_Object breakdown;
4814 int modifiers;
4815
4816 breakdown = parse_modifiers (head);
4817 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
4818 /* Attempt to reduce an unbound mouse event to a simpler
4819 event that is bound:
4820 Drags reduce to clicks.
4821 Double-clicks reduce to clicks.
4822 Triple-clicks reduce to double-clicks, then to clicks.
4823 Down-clicks are eliminated.
4824 Double-downs reduce to downs, then are eliminated.
4825 Triple-downs reduce to double-downs, then to downs,
4826 then are eliminated. */
4827 if (modifiers & (down_modifier | drag_modifier
4828 | double_modifier | triple_modifier))
4829 {
4830 while (modifiers & (down_modifier | drag_modifier
4831 | double_modifier | triple_modifier))
4832 {
4833 Lisp_Object new_head, new_click;
4834 if (modifiers & triple_modifier)
4835 modifiers ^= (double_modifier | triple_modifier);
4836 else if (modifiers & (drag_modifier | double_modifier))
4837 modifiers &= ~(drag_modifier | double_modifier);
4838 else
4839 {
4840 /* Dispose of this `down' event by simply jumping
4841 back to replay_key, to get another event.
4842
4843 Note that if this event came from mock input,
4844 then just jumping back to replay_key will just
4845 hand it to us again. So we have to wipe out any
4846 mock input.
4847
4848 We could delete keybuf[t] and shift everything
4849 after that to the left by one spot, but we'd also
4850 have to fix up any variable that points into
4851 keybuf, and shifting isn't really necessary
4852 anyway.
4853
4854 Adding prefixes for non-textual mouse clicks
4855 creates two characters of mock input, and both
4856 must be thrown away. If we're only looking at
4857 the prefix now, we can just jump back to
4858 replay_key. On the other hand, if we've already
4859 processed the prefix, and now the actual click
4860 itself is giving us trouble, then we've lost the
4861 state of the keymaps we want to backtrack to, and
4862 we need to replay the whole sequence to rebuild
4863 it.
4864
4865 Beyond that, only function key expansion could
4866 create more than two keys, but that should never
4867 generate mouse events, so it's okay to zero
4868 mock_input in that case too.
4869
4870 Isn't this just the most wonderful code ever? */
4871 if (t == last_real_key_start)
4872 {
4873 mock_input = 0;
4874 goto replay_key;
4875 }
4876 else
4877 {
4878 mock_input = last_real_key_start;
4879 goto replay_sequence;
4880 }
4881 }
4882
4883 new_head
4884 = apply_modifiers (modifiers, XCONS (breakdown)->car);
4885 new_click
4886 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
4887
4888 /* Look for a binding for this new key. follow_key
4889 promises that it didn't munge submaps the
4890 last time we called it, since key was unbound. */
4891 first_binding
4892 = (follow_key (new_click,
4893 nmaps - local_first_binding,
4894 submaps + local_first_binding,
4895 defs + local_first_binding,
4896 submaps + local_first_binding)
4897 + local_first_binding);
4898
4899 /* If that click is bound, go for it. */
4900 if (first_binding < nmaps)
4901 {
4902 key = new_click;
4903 break;
4904 }
4905 /* Otherwise, we'll leave key set to the drag event. */
4906 }
4907 }
4908 }
4909 }
4910
4911 keybuf[t++] = key;
4912 /* Normally, last_nonmenu_event gets the previous key we read.
4913 But when a mouse popup menu is being used,
4914 we don't update last_nonmenu_event; it continues to hold the mouse
4915 event that preceded the first level of menu. */
4916 if (!used_mouse_menu)
4917 last_nonmenu_event = key;
4918
4919 /* If the sequence is unbound, see if we can hang a function key
4920 off the end of it. We only want to scan real keyboard input
4921 for function key sequences, so if mock_input says that we're
4922 re-reading old events, don't examine it. */
4923 if (first_binding >= nmaps
4924 && t >= mock_input)
4925 {
4926 Lisp_Object fkey_next;
4927
4928 /* Continue scan from fkey_end until we find a bound suffix.
4929 If we fail, increment fkey_start
4930 and start fkey_end from there. */
4931 while (fkey_end < t)
4932 {
4933 Lisp_Object key;
4934
4935 key = keybuf[fkey_end++];
4936 /* Look up meta-characters by prefixing them
4937 with meta_prefix_char. I hate this. */
4938 if (INTEGERP (key) && XINT (key) & meta_modifier)
4939 {
4940 fkey_next
4941 = get_keymap_1
4942 (get_keyelt
4943 (access_keymap (fkey_map, meta_prefix_char, 1, 0)),
4944 0, 1);
4945 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
4946 }
4947 else
4948 fkey_next = fkey_map;
4949
4950 fkey_next
4951 = get_keyelt (access_keymap (fkey_next, key, 1, 0));
4952
4953 #if 0 /* I didn't turn this on, because it might cause trouble
4954 for the mapping of return into C-m and tab into C-i. */
4955 /* Optionally don't map function keys into other things.
4956 This enables the user to redefine kp- keys easily. */
4957 if (SYMBOLP (key) && !NILP (Vinhibit_function_key_mapping))
4958 fkey_next = Qnil;
4959 #endif
4960
4961 /* If the function key map gives a function, not an
4962 array, then call the function with no args and use
4963 its value instead. */
4964 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
4965 && fkey_end == t)
4966 {
4967 struct gcpro gcpro1, gcpro2, gcpro3;
4968 Lisp_Object tem;
4969 tem = fkey_next;
4970
4971 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
4972 fkey_next = call1 (fkey_next, prompt);
4973 UNGCPRO;
4974 /* If the function returned something invalid,
4975 barf--don't ignore it.
4976 (To ignore it safely, we would need to gcpro a bunch of
4977 other variables.) */
4978 if (! (VECTORP (fkey_next) || STRINGP (fkey_next)))
4979 error ("Function in function-key-map returns invalid key sequence");
4980 }
4981
4982 function_key_possible = ! NILP (fkey_next);
4983
4984 /* If keybuf[fkey_start..fkey_end] is bound in the
4985 function key map and it's a suffix of the current
4986 sequence (i.e. fkey_end == t), replace it with
4987 the binding and restart with fkey_start at the end. */
4988 if ((VECTORP (fkey_next) || STRINGP (fkey_next))
4989 && fkey_end == t)
4990 {
4991 int len = XFASTINT (Flength (fkey_next));
4992
4993 t = fkey_start + len;
4994 if (t >= bufsize)
4995 error ("key sequence too long");
4996
4997 if (VECTORP (fkey_next))
4998 bcopy (XVECTOR (fkey_next)->contents,
4999 keybuf + fkey_start,
5000 (t - fkey_start) * sizeof (keybuf[0]));
5001 else if (STRINGP (fkey_next))
5002 {
5003 int i;
5004
5005 for (i = 0; i < len; i++)
5006 XSETFASTINT (keybuf[fkey_start + i],
5007 XSTRING (fkey_next)->data[i]);
5008 }
5009
5010 mock_input = t;
5011 fkey_start = fkey_end = t;
5012 fkey_map = Vfunction_key_map;
5013
5014 /* Do pass the results through key-translation-map. */
5015 keytran_start = keytran_end = 0;
5016 keytran_map = Vkey_translation_map;
5017
5018 goto replay_sequence;
5019 }
5020
5021 fkey_map = get_keymap_1 (fkey_next, 0, 1);
5022
5023 /* If we no longer have a bound suffix, try a new positions for
5024 fkey_start. */
5025 if (NILP (fkey_map))
5026 {
5027 fkey_end = ++fkey_start;
5028 fkey_map = Vfunction_key_map;
5029 function_key_possible = 0;
5030 }
5031 }
5032 }
5033
5034 /* Look for this sequence in key-translation-map. */
5035 {
5036 Lisp_Object keytran_next;
5037
5038 /* Scan from keytran_end until we find a bound suffix. */
5039 while (keytran_end < t)
5040 {
5041 Lisp_Object key;
5042
5043 key = keybuf[keytran_end++];
5044 /* Look up meta-characters by prefixing them
5045 with meta_prefix_char. I hate this. */
5046 if (INTEGERP (key) && XINT (key) & meta_modifier)
5047 {
5048 keytran_next
5049 = get_keymap_1
5050 (get_keyelt
5051 (access_keymap (keytran_map, meta_prefix_char, 1, 0)),
5052 0, 1);
5053 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
5054 }
5055 else
5056 keytran_next = keytran_map;
5057
5058 keytran_next
5059 = get_keyelt (access_keymap (keytran_next, key, 1, 0));
5060
5061 /* If the key translation map gives a function, not an
5062 array, then call the function with no args and use
5063 its value instead. */
5064 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
5065 && keytran_end == t)
5066 {
5067 struct gcpro gcpro1, gcpro2, gcpro3;
5068 Lisp_Object tem;
5069 tem = keytran_next;
5070
5071 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
5072 keytran_next = call1 (keytran_next, prompt);
5073 UNGCPRO;
5074 /* If the function returned something invalid,
5075 barf--don't ignore it.
5076 (To ignore it safely, we would need to gcpro a bunch of
5077 other variables.) */
5078 if (! (VECTORP (keytran_next) || STRINGP (keytran_next)))
5079 error ("Function in key-translation-map returns invalid key sequence");
5080 }
5081
5082 key_translation_possible = ! NILP (keytran_next);
5083
5084 /* If keybuf[keytran_start..keytran_end] is bound in the
5085 key translation map and it's a suffix of the current
5086 sequence (i.e. keytran_end == t), replace it with
5087 the binding and restart with keytran_start at the end. */
5088 if ((VECTORP (keytran_next) || STRINGP (keytran_next))
5089 && keytran_end == t)
5090 {
5091 int len = XFASTINT (Flength (keytran_next));
5092
5093 t = keytran_start + len;
5094 if (t >= bufsize)
5095 error ("key sequence too long");
5096
5097 if (VECTORP (keytran_next))
5098 bcopy (XVECTOR (keytran_next)->contents,
5099 keybuf + keytran_start,
5100 (t - keytran_start) * sizeof (keybuf[0]));
5101 else if (STRINGP (keytran_next))
5102 {
5103 int i;
5104
5105 for (i = 0; i < len; i++)
5106 XSETFASTINT (keybuf[keytran_start + i],
5107 XSTRING (keytran_next)->data[i]);
5108 }
5109
5110 mock_input = t;
5111 keytran_start = keytran_end = t;
5112 keytran_map = Vkey_translation_map;
5113
5114 /* Don't pass the results of key-translation-map
5115 through function-key-map. */
5116 fkey_start = fkey_end = t;
5117 fkey_map = Vkey_translation_map;
5118
5119 goto replay_sequence;
5120 }
5121
5122 keytran_map = get_keymap_1 (keytran_next, 0, 1);
5123
5124 /* If we no longer have a bound suffix, try a new positions for
5125 keytran_start. */
5126 if (NILP (keytran_map))
5127 {
5128 keytran_end = ++keytran_start;
5129 keytran_map = Vkey_translation_map;
5130 key_translation_possible = 0;
5131 }
5132 }
5133 }
5134
5135 /* If KEY is not defined in any of the keymaps,
5136 and cannot be part of a function key or translation,
5137 and is an upper case letter
5138 use the corresponding lower-case letter instead. */
5139 if (first_binding == nmaps && ! function_key_possible
5140 && ! key_translation_possible
5141 && INTEGERP (key)
5142 && ((((XINT (key) & 0x3ffff)
5143 < XSTRING (current_buffer->downcase_table)->size)
5144 && UPPERCASEP (XINT (key) & 0x3ffff))
5145 || (XINT (key) & shift_modifier)))
5146 {
5147 if (XINT (key) & shift_modifier)
5148 XSETINT (key, XINT (key) & ~shift_modifier);
5149 else
5150 XSETINT (key, (DOWNCASE (XINT (key) & 0x3ffff)
5151 | (XINT (key) & ~0x3ffff)));
5152
5153 keybuf[t - 1] = key;
5154 mock_input = t;
5155 goto replay_sequence;
5156 }
5157 }
5158
5159 read_key_sequence_cmd = (first_binding < nmaps
5160 ? defs[first_binding]
5161 : Qnil);
5162
5163 done:
5164 unread_switch_frame = delayed_switch_frame;
5165 unbind_to (count, Qnil);
5166
5167 /* Occasionally we fabricate events, perhaps by expanding something
5168 according to function-key-map, or by adding a prefix symbol to a
5169 mouse click in the scroll bar or modeline. In this cases, return
5170 the entire generated key sequence, even if we hit an unbound
5171 prefix or a definition before the end. This means that you will
5172 be able to push back the event properly, and also means that
5173 read-key-sequence will always return a logical unit.
5174
5175 Better ideas? */
5176 for (; t < mock_input; t++)
5177 {
5178 if (echo_keystrokes)
5179 echo_char (keybuf[t]);
5180 add_command_key (keybuf[t]);
5181 }
5182
5183 return t;
5184 }
5185
5186 #if 0 /* This doc string is too long for some compilers.
5187 This commented-out definition serves for DOC. */
5188 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 2, 0,
5189 "Read a sequence of keystrokes and return as a string or vector.\n\
5190 The sequence is sufficient to specify a non-prefix command in the\n\
5191 current local and global maps.\n\
5192 \n\
5193 First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
5194 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
5195 as a continuation of the previous key.\n\
5196 \n\
5197 A C-g typed while in this function is treated like any other character,\n\
5198 and `quit-flag' is not set.\n\
5199 \n\
5200 If the key sequence starts with a mouse click, then the sequence is read\n\
5201 using the keymaps of the buffer of the window clicked in, not the buffer\n\
5202 of the selected window as normal.\n\
5203 ""\n\
5204 `read-key-sequence' drops unbound button-down events, since you normally\n\
5205 only care about the click or drag events which follow them. If a drag\n\
5206 or multi-click event is unbound, but the corresponding click event would\n\
5207 be bound, `read-key-sequence' turns the event into a click event at the\n\
5208 drag's starting position. This means that you don't have to distinguish\n\
5209 between click and drag, double, or triple events unless you want to.\n\
5210 \n\
5211 `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
5212 lines separating windows, and scroll bars with imaginary keys\n\
5213 `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
5214 \n\
5215 If the user switches frames in the middle of a key sequence, the\n\
5216 frame-switch event is put off until after the current key sequence.\n\
5217 \n\
5218 `read-key-sequence' checks `function-key-map' for function key\n\
5219 sequences, where they wouldn't conflict with ordinary bindings. See\n\
5220 `function-key-map' for more details.")
5221 (prompt, continue_echo)
5222 #endif
5223
5224 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 2, 0,
5225 0)
5226 (prompt, continue_echo)
5227 Lisp_Object prompt, continue_echo;
5228 {
5229 Lisp_Object keybuf[30];
5230 register int i;
5231 struct gcpro gcpro1, gcpro2;
5232
5233 if (!NILP (prompt))
5234 CHECK_STRING (prompt, 0);
5235 QUIT;
5236
5237 bzero (keybuf, sizeof keybuf);
5238 GCPRO1 (keybuf[0]);
5239 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
5240
5241 if (NILP (continue_echo))
5242 this_command_key_count = 0;
5243
5244 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])), prompt);
5245
5246 if (i == -1)
5247 {
5248 Vquit_flag = Qt;
5249 QUIT;
5250 }
5251 UNGCPRO;
5252 return make_event_array (i, keybuf);
5253 }
5254 \f
5255 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 2, 0,
5256 "Execute CMD as an editor command.\n\
5257 CMD must be a symbol that satisfies the `commandp' predicate.\n\
5258 Optional second arg RECORD-FLAG non-nil\n\
5259 means unconditionally put this command in `command-history'.\n\
5260 Otherwise, that is done only if an arg is read using the minibuffer.")
5261 (cmd, record)
5262 Lisp_Object cmd, record;
5263 {
5264 register Lisp_Object final;
5265 register Lisp_Object tem;
5266 Lisp_Object prefixarg;
5267 struct backtrace backtrace;
5268 extern int debug_on_next_call;
5269
5270 prefixarg = Vprefix_arg, Vprefix_arg = Qnil;
5271 Vcurrent_prefix_arg = prefixarg;
5272 debug_on_next_call = 0;
5273
5274 if (SYMBOLP (cmd))
5275 {
5276 tem = Fget (cmd, Qdisabled);
5277 if (!NILP (tem) && !NILP (Vrun_hooks))
5278 return call1 (Vrun_hooks, Qdisabled_command_hook);
5279 }
5280
5281 while (1)
5282 {
5283 final = Findirect_function (cmd);
5284
5285 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
5286 do_autoload (final, cmd);
5287 else
5288 break;
5289 }
5290
5291 if (STRINGP (final) || VECTORP (final))
5292 {
5293 /* If requested, place the macro in the command history. For
5294 other sorts of commands, call-interactively takes care of
5295 this. */
5296 if (!NILP (record))
5297 Vcommand_history
5298 = Fcons (Fcons (Qexecute_kbd_macro,
5299 Fcons (final, Fcons (prefixarg, Qnil))),
5300 Vcommand_history);
5301
5302 return Fexecute_kbd_macro (final, prefixarg);
5303 }
5304 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
5305 {
5306 backtrace.next = backtrace_list;
5307 backtrace_list = &backtrace;
5308 backtrace.function = &Qcall_interactively;
5309 backtrace.args = &cmd;
5310 backtrace.nargs = 1;
5311 backtrace.evalargs = 0;
5312
5313 tem = Fcall_interactively (cmd, record);
5314
5315 backtrace_list = backtrace.next;
5316 return tem;
5317 }
5318 return Qnil;
5319 }
5320 \f
5321 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
5322 1, 1, "P",
5323 "Read function name, then read its arguments and call it.")
5324 (prefixarg)
5325 Lisp_Object prefixarg;
5326 {
5327 Lisp_Object function;
5328 char buf[40];
5329 Lisp_Object saved_keys;
5330 struct gcpro gcpro1;
5331
5332 saved_keys = Fvector (this_command_key_count,
5333 XVECTOR (this_command_keys)->contents);
5334 buf[0] = 0;
5335 GCPRO1 (saved_keys);
5336
5337 if (EQ (prefixarg, Qminus))
5338 strcpy (buf, "- ");
5339 else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
5340 strcpy (buf, "C-u ");
5341 else if (CONSP (prefixarg) && INTEGERP (XCONS (prefixarg)->car))
5342 sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
5343 else if (INTEGERP (prefixarg))
5344 sprintf (buf, "%d ", XINT (prefixarg));
5345
5346 /* This isn't strictly correct if execute-extended-command
5347 is bound to anything else. Perhaps it should use
5348 this_command_keys? */
5349 strcat (buf, "M-x ");
5350
5351 /* Prompt with buf, and then read a string, completing from and
5352 restricting to the set of all defined commands. Don't provide
5353 any initial input. Save the command read on the extended-command
5354 history list. */
5355 function = Fcompleting_read (build_string (buf),
5356 Vobarray, Qcommandp,
5357 Qt, Qnil, Qextended_command_history);
5358
5359 /* Set this_command_keys to the concatenation of saved_keys and
5360 function, followed by a RET. */
5361 {
5362 struct Lisp_String *str;
5363 Lisp_Object *keys;
5364 int i;
5365 Lisp_Object tem;
5366
5367 this_command_key_count = 0;
5368
5369 keys = XVECTOR (saved_keys)->contents;
5370 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
5371 add_command_key (keys[i]);
5372
5373 str = XSTRING (function);
5374 for (i = 0; i < str->size; i++)
5375 {
5376 XSETFASTINT (tem, str->data[i]);
5377 add_command_key (tem);
5378 }
5379
5380 XSETFASTINT (tem, '\015');
5381 add_command_key (tem);
5382 }
5383
5384 UNGCPRO;
5385
5386 function = Fintern (function, Qnil);
5387 Vprefix_arg = prefixarg;
5388 this_command = function;
5389
5390 return Fcommand_execute (function, Qt);
5391 }
5392 \f
5393
5394 detect_input_pending ()
5395 {
5396 if (!input_pending)
5397 get_input_pending (&input_pending);
5398
5399 return input_pending;
5400 }
5401
5402 /* This is called in some cases before a possible quit.
5403 It cases the next call to detect_input_pending to recompute input_pending.
5404 So calling this function unnecessarily can't do any harm. */
5405 clear_input_pending ()
5406 {
5407 input_pending = 0;
5408 }
5409
5410 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
5411 "T if command input is currently available with no waiting.\n\
5412 Actually, the value is nil only if we can be sure that no input is available.")
5413 ()
5414 {
5415 if (!NILP (Vunread_command_events) || unread_command_char != -1)
5416 return (Qt);
5417
5418 return detect_input_pending () ? Qt : Qnil;
5419 }
5420
5421 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
5422 "Return vector of last 100 events, not counting those from keyboard macros.")
5423 ()
5424 {
5425 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
5426 Lisp_Object val;
5427
5428 if (total_keys < NUM_RECENT_KEYS)
5429 return Fvector (total_keys, keys);
5430 else
5431 {
5432 val = Fvector (NUM_RECENT_KEYS, keys);
5433 bcopy (keys + recent_keys_index,
5434 XVECTOR (val)->contents,
5435 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
5436 bcopy (keys,
5437 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
5438 recent_keys_index * sizeof (Lisp_Object));
5439 return val;
5440 }
5441 }
5442
5443 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
5444 "Return the key sequence that invoked this command.\n\
5445 The value is a string or a vector.")
5446 ()
5447 {
5448 return make_event_array (this_command_key_count,
5449 XVECTOR (this_command_keys)->contents);
5450 }
5451
5452 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
5453 "Return the current depth in recursive edits.")
5454 ()
5455 {
5456 Lisp_Object temp;
5457 XSETFASTINT (temp, command_loop_level + minibuf_level);
5458 return temp;
5459 }
5460
5461 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
5462 "FOpen dribble file: ",
5463 "Start writing all keyboard characters to a dribble file called FILE.\n\
5464 If FILE is nil, close any open dribble file.")
5465 (file)
5466 Lisp_Object file;
5467 {
5468 if (NILP (file))
5469 {
5470 fclose (dribble);
5471 dribble = 0;
5472 }
5473 else
5474 {
5475 file = Fexpand_file_name (file, Qnil);
5476 dribble = fopen (XSTRING (file)->data, "w");
5477 }
5478 return Qnil;
5479 }
5480
5481 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
5482 "Discard the contents of the terminal input buffer.\n\
5483 Also cancel any kbd macro being defined.")
5484 ()
5485 {
5486 defining_kbd_macro = 0;
5487 update_mode_lines++;
5488
5489 Vunread_command_events = Qnil;
5490 unread_command_char = -1;
5491
5492 discard_tty_input ();
5493
5494 /* Without the cast, GCC complains that this assignment loses the
5495 volatile qualifier of kbd_store_ptr. Is there anything wrong
5496 with that? */
5497 kbd_fetch_ptr = (struct input_event *) kbd_store_ptr;
5498 Ffillarray (kbd_buffer_frame_or_window, Qnil);
5499 input_pending = 0;
5500
5501 return Qnil;
5502 }
5503 \f
5504 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
5505 "Stop Emacs and return to superior process. You can resume later.\n\
5506 If `cannot-suspend' is non-nil, or if the system doesn't support job\n\
5507 control, run a subshell instead.\n\n\
5508 If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
5509 to be read as terminal input by Emacs's parent, after suspension.\n\
5510 \n\
5511 Before suspending, run the normal hook `suspend-hook'.\n\
5512 After resumption run the normal hook `suspend-resume-hook'.\n\
5513 \n\
5514 Some operating systems cannot stop the Emacs process and resume it later.\n\
5515 On such systems, Emacs starts a subshell instead of suspending.")
5516 (stuffstring)
5517 Lisp_Object stuffstring;
5518 {
5519 Lisp_Object tem;
5520 int count = specpdl_ptr - specpdl;
5521 int old_height, old_width;
5522 int width, height;
5523 struct gcpro gcpro1, gcpro2;
5524 extern init_sys_modes ();
5525
5526 if (!NILP (stuffstring))
5527 CHECK_STRING (stuffstring, 0);
5528
5529 /* Run the functions in suspend-hook. */
5530 if (!NILP (Vrun_hooks))
5531 call1 (Vrun_hooks, intern ("suspend-hook"));
5532
5533 GCPRO1 (stuffstring);
5534 get_frame_size (&old_width, &old_height);
5535 reset_sys_modes ();
5536 /* sys_suspend can get an error if it tries to fork a subshell
5537 and the system resources aren't available for that. */
5538 record_unwind_protect (init_sys_modes, 0);
5539 stuff_buffered_input (stuffstring);
5540 if (cannot_suspend)
5541 sys_subshell ();
5542 else
5543 sys_suspend ();
5544 unbind_to (count, Qnil);
5545
5546 /* Check if terminal/window size has changed.
5547 Note that this is not useful when we are running directly
5548 with a window system; but suspend should be disabled in that case. */
5549 get_frame_size (&width, &height);
5550 if (width != old_width || height != old_height)
5551 change_frame_size (selected_frame, height, width, 0, 0);
5552
5553 /* Run suspend-resume-hook. */
5554 if (!NILP (Vrun_hooks))
5555 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
5556
5557 UNGCPRO;
5558 return Qnil;
5559 }
5560
5561 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
5562 Then in any case stuff anything Emacs has read ahead and not used. */
5563
5564 stuff_buffered_input (stuffstring)
5565 Lisp_Object stuffstring;
5566 {
5567 register unsigned char *p;
5568
5569 /* stuff_char works only in BSD, versions 4.2 and up. */
5570 #ifdef BSD
5571 #ifndef BSD4_1
5572 if (STRINGP (stuffstring))
5573 {
5574 register int count;
5575
5576 p = XSTRING (stuffstring)->data;
5577 count = XSTRING (stuffstring)->size;
5578 while (count-- > 0)
5579 stuff_char (*p++);
5580 stuff_char ('\n');
5581 }
5582 /* Anything we have read ahead, put back for the shell to read. */
5583 while (kbd_fetch_ptr != kbd_store_ptr)
5584 {
5585 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
5586 kbd_fetch_ptr = kbd_buffer;
5587 if (kbd_fetch_ptr->kind == ascii_keystroke)
5588 stuff_char (kbd_fetch_ptr->code);
5589 kbd_fetch_ptr->kind = no_event;
5590 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_fetch_ptr
5591 - kbd_buffer]
5592 = Qnil);
5593 kbd_fetch_ptr++;
5594 }
5595 input_pending = 0;
5596 #endif
5597 #endif /* BSD and not BSD4_1 */
5598 }
5599 \f
5600 set_waiting_for_input (time_to_clear)
5601 EMACS_TIME *time_to_clear;
5602 {
5603 input_available_clear_time = time_to_clear;
5604
5605 /* Tell interrupt_signal to throw back to read_char, */
5606 waiting_for_input = 1;
5607
5608 /* If interrupt_signal was called before and buffered a C-g,
5609 make it run again now, to avoid timing error. */
5610 if (!NILP (Vquit_flag))
5611 quit_throw_to_read_char ();
5612 }
5613
5614 clear_waiting_for_input ()
5615 {
5616 /* Tell interrupt_signal not to throw back to read_char, */
5617 waiting_for_input = 0;
5618 input_available_clear_time = 0;
5619 }
5620
5621 /* This routine is called at interrupt level in response to C-G.
5622 If interrupt_input, this is the handler for SIGINT.
5623 Otherwise, it is called from kbd_buffer_store_event,
5624 in handling SIGIO or SIGTINT.
5625
5626 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
5627 immediately throw back to read_char.
5628
5629 Otherwise it sets the Lisp variable quit-flag not-nil.
5630 This causes eval to throw, when it gets a chance.
5631 If quit-flag is already non-nil, it stops the job right away. */
5632
5633 SIGTYPE
5634 interrupt_signal ()
5635 {
5636 char c;
5637 /* Must preserve main program's value of errno. */
5638 int old_errno = errno;
5639
5640 #ifdef USG
5641 if (!read_socket_hook && NILP (Vwindow_system))
5642 {
5643 /* USG systems forget handlers when they are used;
5644 must reestablish each time */
5645 signal (SIGINT, interrupt_signal);
5646 signal (SIGQUIT, interrupt_signal);
5647 }
5648 #endif /* USG */
5649
5650 cancel_echoing ();
5651
5652 if (!NILP (Vquit_flag) && FRAME_TERMCAP_P (selected_frame))
5653 {
5654 fflush (stdout);
5655 reset_sys_modes ();
5656 sigfree ();
5657 #ifdef SIGTSTP /* Support possible in later USG versions */
5658 /*
5659 * On systems which can suspend the current process and return to the original
5660 * shell, this command causes the user to end up back at the shell.
5661 * The "Auto-save" and "Abort" questions are not asked until
5662 * the user elects to return to emacs, at which point he can save the current
5663 * job and either dump core or continue.
5664 */
5665 sys_suspend ();
5666 #else
5667 #ifdef VMS
5668 if (sys_suspend () == -1)
5669 {
5670 printf ("Not running as a subprocess;\n");
5671 printf ("you can continue or abort.\n");
5672 }
5673 #else /* not VMS */
5674 /* Perhaps should really fork an inferior shell?
5675 But that would not provide any way to get back
5676 to the original shell, ever. */
5677 printf ("No support for stopping a process on this operating system;\n");
5678 printf ("you can continue or abort.\n");
5679 #endif /* not VMS */
5680 #endif /* not SIGTSTP */
5681 #ifdef MSDOS
5682 /* We must remain inside the screen area when the internal terminal
5683 is used. Note that [Enter] is not echoed by dos. */
5684 cursor_to (0, 0);
5685 #endif
5686 printf ("Auto-save? (y or n) ");
5687 fflush (stdout);
5688 if (((c = getchar ()) & ~040) == 'Y')
5689 {
5690 Fdo_auto_save (Qt, Qnil);
5691 #ifdef MSDOS
5692 printf ("\r\nAuto-save done");
5693 #else /* not MSDOS */
5694 printf ("Auto-save done\n");
5695 #endif /* not MSDOS */
5696 }
5697 while (c != '\n') c = getchar ();
5698 #ifdef MSDOS
5699 printf ("\r\nAbort? (y or n) ");
5700 #else /* not MSDOS */
5701 #ifdef VMS
5702 printf ("Abort (and enter debugger)? (y or n) ");
5703 #else /* not VMS */
5704 printf ("Abort (and dump core)? (y or n) ");
5705 #endif /* not VMS */
5706 #endif /* not MSDOS */
5707 fflush (stdout);
5708 if (((c = getchar ()) & ~040) == 'Y')
5709 abort ();
5710 while (c != '\n') c = getchar ();
5711 #ifdef MSDOS
5712 printf ("\r\nContinuing...\r\n");
5713 #else /* not MSDOS */
5714 printf ("Continuing...\n");
5715 #endif /* not MSDOS */
5716 fflush (stdout);
5717 init_sys_modes ();
5718 }
5719 else
5720 {
5721 /* If executing a function that wants to be interrupted out of
5722 and the user has not deferred quitting by binding `inhibit-quit'
5723 then quit right away. */
5724 if (immediate_quit && NILP (Vinhibit_quit))
5725 {
5726 immediate_quit = 0;
5727 sigfree ();
5728 Fsignal (Qquit, Qnil);
5729 }
5730 else
5731 /* Else request quit when it's safe */
5732 Vquit_flag = Qt;
5733 }
5734
5735 if (waiting_for_input && !echoing)
5736 quit_throw_to_read_char ();
5737
5738 errno = old_errno;
5739 }
5740
5741 /* Handle a C-g by making read_char return C-g. */
5742
5743 quit_throw_to_read_char ()
5744 {
5745 quit_error_check ();
5746 sigfree ();
5747 /* Prevent another signal from doing this before we finish. */
5748 clear_waiting_for_input ();
5749 input_pending = 0;
5750
5751 Vunread_command_events = Qnil;
5752 unread_command_char = -1;
5753
5754 #ifdef POLL_FOR_INPUT
5755 /* May be > 1 if in recursive minibuffer. */
5756 if (poll_suppress_count == 0)
5757 abort ();
5758 #endif
5759 #ifdef MULTI_FRAME
5760 if (FRAMEP (internal_last_event_frame)
5761 && XFRAME (internal_last_event_frame) != selected_frame)
5762 Fhandle_switch_frame (make_lispy_switch_frame (internal_last_event_frame));
5763 #endif
5764
5765 _longjmp (getcjmp, 1);
5766 }
5767 \f
5768 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
5769 "Set mode of reading keyboard input.\n\
5770 First arg INTERRUPT non-nil means use input interrupts;\n\
5771 nil means use CBREAK mode.\n\
5772 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
5773 (no effect except in CBREAK mode).\n\
5774 Third arg META t means accept 8-bit input (for a Meta key).\n\
5775 META nil means ignore the top bit, on the assumption it is parity.\n\
5776 Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
5777 Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\
5778 See also `current-input-mode'.")
5779 (interrupt, flow, meta, quit)
5780 Lisp_Object interrupt, flow, meta, quit;
5781 {
5782 if (!NILP (quit)
5783 && (!INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400))
5784 error ("set-input-mode: QUIT must be an ASCII character");
5785
5786 #ifdef POLL_FOR_INPUT
5787 stop_polling ();
5788 #endif
5789
5790 reset_sys_modes ();
5791 #ifdef SIGIO
5792 /* Note SIGIO has been undef'd if FIONREAD is missing. */
5793 #ifdef NO_SOCK_SIGIO
5794 if (read_socket_hook)
5795 interrupt_input = 0; /* No interrupts if reading from a socket. */
5796 else
5797 #endif /* NO_SOCK_SIGIO */
5798 interrupt_input = !NILP (interrupt);
5799 #else /* not SIGIO */
5800 interrupt_input = 0;
5801 #endif /* not SIGIO */
5802 /* Our VMS input only works by interrupts, as of now. */
5803 #ifdef VMS
5804 interrupt_input = 1;
5805 #endif
5806 flow_control = !NILP (flow);
5807 if (NILP (meta))
5808 meta_key = 0;
5809 else if (EQ (meta, Qt))
5810 meta_key = 1;
5811 else
5812 meta_key = 2;
5813 if (!NILP (quit))
5814 /* Don't let this value be out of range. */
5815 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
5816
5817 init_sys_modes ();
5818
5819 #ifdef POLL_FOR_INPUT
5820 poll_suppress_count = 1;
5821 start_polling ();
5822 #endif
5823 return Qnil;
5824 }
5825
5826 DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
5827 "Return information about the way Emacs currently reads keyboard input.\n\
5828 The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
5829 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
5830 nil, Emacs is using CBREAK mode.\n\
5831 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
5832 terminal; this does not apply if Emacs uses interrupt-driven input.\n\
5833 META is t if accepting 8-bit input with 8th bit as Meta flag.\n\
5834 META nil means ignoring the top bit, on the assumption it is parity.\n\
5835 META is neither t nor nil if accepting 8-bit input and using\n\
5836 all 8 bits as the character code.\n\
5837 QUIT is the character Emacs currently uses to quit.\n\
5838 The elements of this list correspond to the arguments of\n\
5839 `set-input-mode'.")
5840 ()
5841 {
5842 Lisp_Object val[4];
5843
5844 val[0] = interrupt_input ? Qt : Qnil;
5845 val[1] = flow_control ? Qt : Qnil;
5846 val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
5847 XSETFASTINT (val[3], quit_char);
5848
5849 return Flist (sizeof (val) / sizeof (val[0]), val);
5850 }
5851
5852 \f
5853 init_keyboard ()
5854 {
5855 /* This is correct before outermost invocation of the editor loop */
5856 command_loop_level = -1;
5857 immediate_quit = 0;
5858 quit_char = Ctl ('g');
5859 Vunread_command_events = Qnil;
5860 unread_command_char = -1;
5861 total_keys = 0;
5862 recent_keys_index = 0;
5863 kbd_fetch_ptr = kbd_buffer;
5864 kbd_store_ptr = kbd_buffer;
5865 do_mouse_tracking = 0;
5866 input_pending = 0;
5867
5868 #ifdef MULTI_FRAME
5869 /* This means that command_loop_1 won't try to select anything the first
5870 time through. */
5871 internal_last_event_frame = Qnil;
5872 Vlast_event_frame = internal_last_event_frame;
5873 #endif
5874
5875 /* If we're running a dumped Emacs, we need to clear out
5876 kbd_buffer_frame_or_window, in case some events got into it
5877 before we dumped.
5878
5879 If we're running an undumped Emacs, it hasn't been initialized by
5880 syms_of_keyboard yet. */
5881 if (initialized)
5882 Ffillarray (kbd_buffer_frame_or_window, Qnil);
5883
5884 if (!noninteractive && !read_socket_hook && NILP (Vwindow_system))
5885 {
5886 signal (SIGINT, interrupt_signal);
5887 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
5888 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
5889 SIGQUIT and we can't tell which one it will give us. */
5890 signal (SIGQUIT, interrupt_signal);
5891 #endif /* HAVE_TERMIO */
5892 }
5893 /* Note SIGIO has been undef'd if FIONREAD is missing. */
5894 #ifdef SIGIO
5895 if (!noninteractive)
5896 signal (SIGIO, input_available_signal);
5897 #endif /* SIGIO */
5898
5899 /* Use interrupt input by default, if it works and noninterrupt input
5900 has deficiencies. */
5901
5902 #ifdef INTERRUPT_INPUT
5903 interrupt_input = 1;
5904 #else
5905 interrupt_input = 0;
5906 #endif
5907
5908 /* Our VMS input only works by interrupts, as of now. */
5909 #ifdef VMS
5910 interrupt_input = 1;
5911 #endif
5912
5913 sigfree ();
5914 dribble = 0;
5915
5916 if (keyboard_init_hook)
5917 (*keyboard_init_hook) ();
5918
5919 #ifdef POLL_FOR_INPUT
5920 poll_suppress_count = 1;
5921 start_polling ();
5922 #endif
5923 }
5924
5925 /* This type's only use is in syms_of_keyboard, to initialize the
5926 event header symbols and put properties on them. */
5927 struct event_head {
5928 Lisp_Object *var;
5929 char *name;
5930 Lisp_Object *kind;
5931 };
5932
5933 struct event_head head_table[] = {
5934 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
5935 &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement,
5936 &Qswitch_frame, "switch-frame", &Qswitch_frame,
5937 &Qdelete_frame, "delete-frame", &Qdelete_frame,
5938 };
5939
5940 syms_of_keyboard ()
5941 {
5942 Qdisabled_command_hook = intern ("disabled-command-hook");
5943 staticpro (&Qdisabled_command_hook);
5944
5945 Qself_insert_command = intern ("self-insert-command");
5946 staticpro (&Qself_insert_command);
5947
5948 Qforward_char = intern ("forward-char");
5949 staticpro (&Qforward_char);
5950
5951 Qbackward_char = intern ("backward-char");
5952 staticpro (&Qbackward_char);
5953
5954 Qdisabled = intern ("disabled");
5955 staticpro (&Qdisabled);
5956
5957 Qundefined = intern ("undefined");
5958 staticpro (&Qundefined);
5959
5960 Qpre_command_hook = intern ("pre-command-hook");
5961 staticpro (&Qpre_command_hook);
5962
5963 Qpost_command_hook = intern ("post-command-hook");
5964 staticpro (&Qpost_command_hook);
5965
5966 Qcommand_hook_internal = intern ("command-hook-internal");
5967 staticpro (&Qcommand_hook_internal);
5968
5969 Qfunction_key = intern ("function-key");
5970 staticpro (&Qfunction_key);
5971 Qmouse_click = intern ("mouse-click");
5972 staticpro (&Qmouse_click);
5973
5974 Qmenu_enable = intern ("menu-enable");
5975 staticpro (&Qmenu_enable);
5976
5977 Qmode_line = intern ("mode-line");
5978 staticpro (&Qmode_line);
5979 Qvertical_line = intern ("vertical-line");
5980 staticpro (&Qvertical_line);
5981 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
5982 staticpro (&Qvertical_scroll_bar);
5983 Qmenu_bar = intern ("menu-bar");
5984 staticpro (&Qmenu_bar);
5985
5986 Qabove_handle = intern ("above-handle");
5987 staticpro (&Qabove_handle);
5988 Qhandle = intern ("handle");
5989 staticpro (&Qhandle);
5990 Qbelow_handle = intern ("below-handle");
5991 staticpro (&Qbelow_handle);
5992
5993 Qevent_kind = intern ("event-kind");
5994 staticpro (&Qevent_kind);
5995 Qevent_symbol_elements = intern ("event-symbol-elements");
5996 staticpro (&Qevent_symbol_elements);
5997 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
5998 staticpro (&Qevent_symbol_element_mask);
5999 Qmodifier_cache = intern ("modifier-cache");
6000 staticpro (&Qmodifier_cache);
6001
6002 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
6003 staticpro (&Qrecompute_lucid_menubar);
6004 Qactivate_menubar_hook = intern ("activate-menubar-hook");
6005 staticpro (&Qactivate_menubar_hook);
6006
6007 Qpolling_period = intern ("polling-period");
6008 staticpro (&Qpolling_period);
6009
6010 {
6011 struct event_head *p;
6012
6013 for (p = head_table;
6014 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
6015 p++)
6016 {
6017 *p->var = intern (p->name);
6018 staticpro (p->var);
6019 Fput (*p->var, Qevent_kind, *p->kind);
6020 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
6021 }
6022 }
6023
6024 button_down_location = Fmake_vector (make_number (NUM_MOUSE_BUTTONS), Qnil);
6025 staticpro (&button_down_location);
6026
6027 {
6028 int i;
6029 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
6030
6031 modifier_symbols = Fmake_vector (make_number (len), Qnil);
6032 for (i = 0; i < len; i++)
6033 if (modifier_names[i])
6034 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
6035 staticpro (&modifier_symbols);
6036 }
6037
6038 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
6039 staticpro (&recent_keys);
6040
6041 this_command_keys = Fmake_vector (make_number (40), Qnil);
6042 staticpro (&this_command_keys);
6043
6044 Qextended_command_history = intern ("extended-command-history");
6045 Fset (Qextended_command_history, Qnil);
6046 staticpro (&Qextended_command_history);
6047
6048 kbd_buffer_frame_or_window
6049 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
6050 staticpro (&kbd_buffer_frame_or_window);
6051
6052 accent_key_syms = Qnil;
6053 staticpro (&accent_key_syms);
6054
6055 func_key_syms = Qnil;
6056 staticpro (&func_key_syms);
6057
6058 system_key_syms = Qnil;
6059 staticpro (&system_key_syms);
6060
6061 mouse_syms = Qnil;
6062 staticpro (&mouse_syms);
6063
6064 unread_switch_frame = Qnil;
6065 staticpro (&unread_switch_frame);
6066
6067 defsubr (&Sread_key_sequence);
6068 defsubr (&Srecursive_edit);
6069 defsubr (&Strack_mouse);
6070 defsubr (&Sinput_pending_p);
6071 defsubr (&Scommand_execute);
6072 defsubr (&Srecent_keys);
6073 defsubr (&Sthis_command_keys);
6074 defsubr (&Ssuspend_emacs);
6075 defsubr (&Sabort_recursive_edit);
6076 defsubr (&Sexit_recursive_edit);
6077 defsubr (&Srecursion_depth);
6078 defsubr (&Stop_level);
6079 defsubr (&Sdiscard_input);
6080 defsubr (&Sopen_dribble_file);
6081 defsubr (&Sset_input_mode);
6082 defsubr (&Scurrent_input_mode);
6083 defsubr (&Sexecute_extended_command);
6084
6085 DEFVAR_LISP ("last-command-char", &last_command_char,
6086 "Last input event that was part of a command.");
6087
6088 DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char,
6089 "Last input event that was part of a command.");
6090
6091 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
6092 "Last input event in a command, except for mouse menu events.\n\
6093 Mouse menus give back keys that don't look like mouse events;\n\
6094 this variable holds the actual mouse event that led to the menu,\n\
6095 so that you can determine whether the command was run by mouse or not.");
6096
6097 DEFVAR_LISP ("last-input-char", &last_input_char,
6098 "Last input event.");
6099
6100 DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char,
6101 "Last input event.");
6102
6103 DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
6104 "List of objects to be read as next command input events.");
6105
6106 DEFVAR_INT ("unread-command-char", &unread_command_char,
6107 "If not -1, an object to be read as next command input event.");
6108
6109 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
6110 "Meta-prefix character code. Meta-foo as command input\n\
6111 turns into this character followed by foo.");
6112 XSETINT (meta_prefix_char, 033);
6113
6114 DEFVAR_LISP ("last-command", &last_command,
6115 "The last command executed. Normally a symbol with a function definition,\n\
6116 but can be whatever was found in the keymap, or whatever the variable\n\
6117 `this-command' was set to by that command.");
6118 last_command = Qnil;
6119
6120 DEFVAR_LISP ("this-command", &this_command,
6121 "The command now being executed.\n\
6122 The command can set this variable; whatever is put here\n\
6123 will be in `last-command' during the following command.");
6124 this_command = Qnil;
6125
6126 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
6127 "*Number of keyboard input characters between auto-saves.\n\
6128 Zero means disable autosaving due to number of characters typed.");
6129 auto_save_interval = 300;
6130
6131 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
6132 "*Number of seconds idle time before auto-save.\n\
6133 Zero or nil means disable auto-saving due to idleness.\n\
6134 After auto-saving due to this many seconds of idle time,\n\
6135 Emacs also does a garbage collection if that seems to be warranted.");
6136 XSETFASTINT (Vauto_save_timeout, 30);
6137
6138 DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
6139 "*Nonzero means echo unfinished commands after this many seconds of pause.");
6140 echo_keystrokes = 1;
6141
6142 DEFVAR_INT ("polling-period", &polling_period,
6143 "*Interval between polling for input during Lisp execution.\n\
6144 The reason for polling is to make C-g work to stop a running program.\n\
6145 Polling is needed only when using X windows and SIGIO does not work.\n\
6146 Polling is automatically disabled in all other cases.");
6147 polling_period = 2;
6148
6149 DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
6150 "*Maximum time between mouse clicks to make a double-click.\n\
6151 Measured in milliseconds. nil means disable double-click recognition;\n\
6152 t means double-clicks have no time limit and are detected\n\
6153 by position only.");
6154 Vdouble_click_time = make_number (500);
6155
6156 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus,
6157 "*Non-nil means inhibit local map menu bar menus.");
6158 inhibit_local_menu_bar_menus = 0;
6159
6160 DEFVAR_INT ("num-input-keys", &num_input_keys,
6161 "*Number of complete keys read from the keyboard so far.");
6162 num_input_keys = 0;
6163
6164 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
6165 "*The frame in which the most recently read event occurred.\n\
6166 If the last event came from a keyboard macro, this is set to `macro'.");
6167 Vlast_event_frame = Qnil;
6168
6169 DEFVAR_LISP ("help-char", &Vhelp_char,
6170 "Character to recognize as meaning Help.\n\
6171 When it is read, do `(eval help-form)', and display result if it's a string.\n\
6172 If the value of `help-form' is nil, this char can be read normally.");
6173 XSETINT (Vhelp_char, Ctl ('H'));
6174
6175 DEFVAR_LISP ("help-form", &Vhelp_form,
6176 "Form to execute when character `help-char' is read.\n\
6177 If the form returns a string, that string is displayed.\n\
6178 If `help-form' is nil, the help char is not recognized.");
6179 Vhelp_form = Qnil;
6180
6181 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
6182 "Command to run when `help-char' character follows a prefix key.\n\
6183 This command is used only when there is no actual binding\n\
6184 for that character after that prefix key.");
6185 Vprefix_help_command = Qnil;
6186
6187 DEFVAR_LISP ("top-level", &Vtop_level,
6188 "Form to evaluate when Emacs starts up.\n\
6189 Useful to set before you dump a modified Emacs.");
6190 Vtop_level = Qnil;
6191
6192 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
6193 "String used as translate table for keyboard input, or nil.\n\
6194 Each character is looked up in this string and the contents used instead.\n\
6195 If string is of length N, character codes N and up are untranslated.");
6196 Vkeyboard_translate_table = Qnil;
6197
6198 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map,
6199 "Keymap of key translations that can override keymaps.\n\
6200 This keymap works like `function-key-map', but comes after that,\n\
6201 and applies even for keys that have ordinary bindings.");
6202 Vkey_translation_map = Qnil;
6203
6204 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
6205 "Non-nil means to always spawn a subshell instead of suspending,\n\
6206 even if the operating system has support for stopping a process.");
6207 cannot_suspend = 0;
6208
6209 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
6210 "Non-nil means prompt with menus when appropriate.\n\
6211 This is done when reading from a keymap that has a prompt string,\n\
6212 for elements that have prompt strings.\n\
6213 The menu is displayed on the screen\n\
6214 if X menus were enabled at configuration\n\
6215 time and the previous event was a mouse click prefix key.\n\
6216 Otherwise, menu prompting uses the echo area.");
6217 menu_prompting = 1;
6218
6219 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
6220 "Character to see next line of menu prompt.\n\
6221 Type this character while in a menu prompt to rotate around the lines of it.");
6222 XSETINT (menu_prompt_more_char, ' ');
6223
6224 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
6225 "A mask of additional modifier keys to use with every keyboard character.\n\
6226 Emacs applies the modifiers of the character stored here to each keyboard\n\
6227 character it reads. For example, after evaluating the expression\n\
6228 (setq extra-keyboard-modifiers ?\C-x)\n\
6229 all input characters will have the control modifier applied to them.\n\
6230 \n\
6231 Note that the character ?\C-@, equivalent to the integer zero, does\n\
6232 not count as a control character; rather, it counts as a character\n\
6233 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\
6234 cancels any modification.");
6235 extra_keyboard_modifiers = 0;
6236
6237 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
6238 "If an editing command sets this to t, deactivate the mark afterward.\n\
6239 The command loop sets this to nil before each command,\n\
6240 and tests the value when the command returns.\n\
6241 Buffer modification stores t in this variable.");
6242 Vdeactivate_mark = Qnil;
6243
6244 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
6245 "Temporary storage of pre-command-hook or post-command-hook.");
6246 Vcommand_hook_internal = Qnil;
6247
6248 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
6249 "Normal hook run before each command is executed.\n\
6250 While the hook is run, its value is temporarily set to nil\n\
6251 to avoid an unbreakable infinite loop if a hook function gets an error.\n\
6252 As a result, a hook function cannot straightforwardly alter the value of\n\
6253 `pre-command-hook'. See the Emacs Lisp manual for a way of\n\
6254 implementing hook functions that alter the set of hook functions.");
6255 Vpre_command_hook = Qnil;
6256
6257 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
6258 "Normal hook run after each command is executed.\n\
6259 While the hook is run, its value is temporarily set to nil\n\
6260 to avoid an unbreakable infinite loop if a hook function gets an error.\n\
6261 As a result, a hook function cannot straightforwardly alter the value of\n\
6262 `post-command-hook'. See the Emacs Lisp manual for a way of\n\
6263 implementing hook functions that alter the set of hook functions.");
6264 Vpost_command_hook = Qnil;
6265
6266 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
6267 "t means menu bar, specified Lucid style, needs to be recomputed.");
6268 Vlucid_menu_bar_dirty_flag = Qnil;
6269
6270 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
6271 "List of menu bar items to move to the end of the menu bar.\n\
6272 The elements of the list are event types that may have menu bar bindings.");
6273 Vmenu_bar_final_items = Qnil;
6274
6275 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
6276 "Keymap that overrides all other local keymaps.\n\
6277 If this variable is non-nil, it is used as a keymap instead of the\n\
6278 buffer's local map, and the minor mode keymaps and text property keymaps.");
6279 Voverriding_local_map = Qnil;
6280
6281 DEFVAR_BOOL ("track-mouse", &do_mouse_tracking,
6282 "*Non-nil means generate motion events for mouse motion.");
6283
6284 DEFVAR_LISP ("system-key-alist", &Vsystem_key_alist,
6285 "Alist of system-specific X windows key symbols.\n\
6286 Each element should have the form (N . SYMBOL) where N is the\n\
6287 numeric keysym code (sans the \"system-specific\" bit 1<<28)\n\
6288 and SYMBOL is its name.");
6289 Vsystem_key_alist = Qnil;
6290
6291 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
6292 "List of deferred actions to be performed at a later time.\n\
6293 The precise format isn't relevant here; we just check whether it is nil.");
6294 Vdeferred_action_list = Qnil;
6295
6296 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
6297 "Function to call to handle deferred actions, after each command.\n\
6298 This function is called with no arguments after each command\n\
6299 whenever `deferred-action-list' is non-nil.");
6300 Vdeferred_action_function = Qnil;
6301 }
6302
6303 keys_of_keyboard ()
6304 {
6305 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
6306 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
6307 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
6308 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
6309 initial_define_key (meta_map, 'x', "execute-extended-command");
6310 }