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