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