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