(which-func-maxout): Increase default to 500000.
[bpt/emacs.git] / src / keyboard.c
1 /* Keyboard and mouse input; editor command loop.
2 Copyright (C) 1985,86,87,88,89,93,94,95,96,97,99, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include <config.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include "termchar.h"
26 #include "termopts.h"
27 #include "lisp.h"
28 #include "termhooks.h"
29 #include "macros.h"
30 #include "keyboard.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 "syntax.h"
39 #include "intervals.h"
40 #include "blockinput.h"
41 #include "puresize.h"
42 #include "systime.h"
43 #include "atimer.h"
44 #include <setjmp.h>
45 #include <errno.h>
46
47 #ifdef MSDOS
48 #include "msdos.h"
49 #include <time.h>
50 #else /* not MSDOS */
51 #ifndef VMS
52 #include <sys/ioctl.h>
53 #endif
54 #endif /* not MSDOS */
55
56 #include "syssignal.h"
57 #include "systty.h"
58
59 #include <sys/types.h>
60 #ifdef HAVE_UNISTD_H
61 #include <unistd.h>
62 #endif
63
64 /* This is to get the definitions of the XK_ symbols. */
65 #ifdef HAVE_X_WINDOWS
66 #include "xterm.h"
67 #endif
68
69 #ifdef HAVE_NTGUI
70 #include "w32term.h"
71 #endif /* HAVE_NTGUI */
72
73 #ifdef macintosh
74 #include "macterm.h"
75 #endif
76
77 /* Include systime.h after xterm.h to avoid double inclusion of time.h. */
78 #include "systime.h"
79
80 #ifndef USE_CRT_DLL
81 extern int errno;
82 #endif
83
84 /* Variables for blockinput.h: */
85
86 /* Non-zero if interrupt input is blocked right now. */
87 int interrupt_input_blocked;
88
89 /* Nonzero means an input interrupt has arrived
90 during the current critical section. */
91 int interrupt_input_pending;
92
93
94 /* File descriptor to use for input. */
95 extern int input_fd;
96
97 #ifdef HAVE_WINDOW_SYSTEM
98 /* Make all keyboard buffers much bigger when using X windows. */
99 #ifdef macintosh
100 /* But not too big (local data > 32K error) if on macintosh. */
101 #define KBD_BUFFER_SIZE 512
102 #else
103 #define KBD_BUFFER_SIZE 4096
104 #endif
105 #else /* No X-windows, character input */
106 #define KBD_BUFFER_SIZE 256
107 #endif /* No X-windows */
108
109 /* Following definition copied from eval.c */
110
111 struct backtrace
112 {
113 struct backtrace *next;
114 Lisp_Object *function;
115 Lisp_Object *args; /* Points to vector of args. */
116 int nargs; /* length of vector. If nargs is UNEVALLED,
117 args points to slot holding list of
118 unevalled args */
119 char evalargs;
120 };
121
122 #ifdef MULTI_KBOARD
123 KBOARD *initial_kboard;
124 KBOARD *current_kboard;
125 KBOARD *all_kboards;
126 int single_kboard;
127 #else
128 KBOARD the_only_kboard;
129 #endif
130
131 /* Non-nil disable property on a command means
132 do not execute it; call disabled-command-hook's value instead. */
133 Lisp_Object Qdisabled, Qdisabled_command_hook;
134
135 #define NUM_RECENT_KEYS (100)
136 int recent_keys_index; /* Index for storing next element into recent_keys */
137 int total_keys; /* Total number of elements stored into recent_keys */
138 Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
139
140 /* Vector holding the key sequence that invoked the current command.
141 It is reused for each command, and it may be longer than the current
142 sequence; this_command_key_count indicates how many elements
143 actually mean something.
144 It's easier to staticpro a single Lisp_Object than an array. */
145 Lisp_Object this_command_keys;
146 int this_command_key_count;
147
148 /* This vector is used as a buffer to record the events that were actually read
149 by read_key_sequence. */
150 Lisp_Object raw_keybuf;
151 int raw_keybuf_count;
152
153 #define GROW_RAW_KEYBUF \
154 if (raw_keybuf_count == XVECTOR (raw_keybuf)->size) \
155 { \
156 int newsize = 2 * XVECTOR (raw_keybuf)->size; \
157 Lisp_Object new; \
158 new = Fmake_vector (make_number (newsize), Qnil); \
159 bcopy (XVECTOR (raw_keybuf)->contents, XVECTOR (new)->contents, \
160 raw_keybuf_count * sizeof (Lisp_Object)); \
161 raw_keybuf = new; \
162 }
163
164 /* Number of elements of this_command_keys
165 that precede this key sequence. */
166 int this_single_command_key_start;
167
168 /* Record values of this_command_key_count and echo_length ()
169 before this command was read. */
170 static int before_command_key_count;
171 static int before_command_echo_length;
172 /* Values of before_command_key_count and before_command_echo_length
173 saved by reset-this-command-lengths. */
174 static int before_command_key_count_1;
175 static int before_command_echo_length_1;
176 /* Flag set by reset-this-command-lengths,
177 saying to reset the lengths when add_command_key is called. */
178 static int before_command_restore_flag;
179
180 extern int minbuf_level;
181
182 extern int message_enable_multibyte;
183
184 extern struct backtrace *backtrace_list;
185
186 /* If non-nil, the function that implements the display of help.
187 It's called with one argument, the help string to display. */
188
189 Lisp_Object Vshow_help_function;
190
191 /* Nonzero means do menu prompting. */
192 static int menu_prompting;
193
194 /* Character to see next line of menu prompt. */
195 static Lisp_Object menu_prompt_more_char;
196
197 /* For longjmp to where kbd input is being done. */
198 static jmp_buf getcjmp;
199
200 /* True while doing kbd input. */
201 int waiting_for_input;
202
203 /* True while displaying for echoing. Delays C-g throwing. */
204
205 int echoing;
206
207 /* Non-null means we can start echoing at the next input pause even
208 though there is something in the echo area. */
209
210 static struct kboard *ok_to_echo_at_next_pause;
211
212 /* The kboard last echoing, or null for none. Reset to 0 in
213 cancel_echoing. If non-null, and a current echo area message
214 exists, and echo_message_buffer is eq to the current message
215 buffer, we know that the message comes from echo_kboard. */
216
217 static struct kboard *echo_kboard;
218
219 /* The buffer used for echoing. Set in echo_now, reset in
220 cancel_echoing. */
221
222 Lisp_Object echo_message_buffer;
223
224 /* Nonzero means disregard local maps for the menu bar. */
225 static int inhibit_local_menu_bar_menus;
226
227 /* Nonzero means C-g should cause immediate error-signal. */
228 int immediate_quit;
229
230 /* The user's ERASE setting. */
231 Lisp_Object Vtty_erase_char;
232
233 /* Character to recognize as the help char. */
234 Lisp_Object Vhelp_char;
235
236 /* List of other event types to recognize as meaning "help". */
237 Lisp_Object Vhelp_event_list;
238
239 /* Form to execute when help char is typed. */
240 Lisp_Object Vhelp_form;
241
242 /* Command to run when the help character follows a prefix key. */
243 Lisp_Object Vprefix_help_command;
244
245 /* List of items that should move to the end of the menu bar. */
246 Lisp_Object Vmenu_bar_final_items;
247
248 /* Non-nil means show the equivalent key-binding for
249 any M-x command that has one.
250 The value can be a length of time to show the message for.
251 If the value is non-nil and not a number, we wait 2 seconds. */
252 Lisp_Object Vsuggest_key_bindings;
253
254 /* How long to display an echo-area message when the minibuffer is active.
255 If the value is not a number, such messages don't time out. */
256 Lisp_Object Vminibuffer_message_timeout;
257
258 /* Character that causes a quit. Normally C-g.
259
260 If we are running on an ordinary terminal, this must be an ordinary
261 ASCII char, since we want to make it our interrupt character.
262
263 If we are not running on an ordinary terminal, it still needs to be
264 an ordinary ASCII char. This character needs to be recognized in
265 the input interrupt handler. At this point, the keystroke is
266 represented as a struct input_event, while the desired quit
267 character is specified as a lispy event. The mapping from struct
268 input_events to lispy events cannot run in an interrupt handler,
269 and the reverse mapping is difficult for anything but ASCII
270 keystrokes.
271
272 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
273 ASCII character. */
274 int quit_char;
275
276 extern Lisp_Object current_global_map;
277 extern int minibuf_level;
278
279 /* If non-nil, this is a map that overrides all other local maps. */
280 Lisp_Object Voverriding_local_map;
281
282 /* If non-nil, Voverriding_local_map applies to the menu bar. */
283 Lisp_Object Voverriding_local_map_menu_flag;
284
285 /* Keymap that defines special misc events that should
286 be processed immediately at a low level. */
287 Lisp_Object Vspecial_event_map;
288
289 /* Current depth in recursive edits. */
290 int command_loop_level;
291
292 /* Total number of times command_loop has read a key sequence. */
293 int num_input_keys;
294
295 /* Last input character read as a command. */
296 Lisp_Object last_command_char;
297
298 /* Last input character read as a command, not counting menus
299 reached by the mouse. */
300 Lisp_Object last_nonmenu_event;
301
302 /* Last input character read for any purpose. */
303 Lisp_Object last_input_char;
304
305 /* If not Qnil, a list of objects to be read as subsequent command input. */
306 Lisp_Object Vunread_command_events;
307
308 /* If not Qnil, a list of objects to be read as subsequent command input
309 including input method processing. */
310 Lisp_Object Vunread_input_method_events;
311
312 /* If not Qnil, a list of objects to be read as subsequent command input
313 but NOT including input method processing. */
314 Lisp_Object Vunread_post_input_method_events;
315
316 /* If not -1, an event to be read as subsequent command input. */
317 int unread_command_char;
318
319 /* If not Qnil, this is a switch-frame event which we decided to put
320 off until the end of a key sequence. This should be read as the
321 next command input, after any unread_command_events.
322
323 read_key_sequence uses this to delay switch-frame events until the
324 end of the key sequence; Fread_char uses it to put off switch-frame
325 events until a non-ASCII event is acceptable as input. */
326 Lisp_Object unread_switch_frame;
327
328 /* A mask of extra modifier bits to put into every keyboard char. */
329 int extra_keyboard_modifiers;
330
331 /* Char to use as prefix when a meta character is typed in.
332 This is bound on entry to minibuffer in case ESC is changed there. */
333
334 Lisp_Object meta_prefix_char;
335
336 /* Last size recorded for a current buffer which is not a minibuffer. */
337 static int last_non_minibuf_size;
338
339 /* Number of idle seconds before an auto-save and garbage collection. */
340 static Lisp_Object Vauto_save_timeout;
341
342 /* Total number of times read_char has returned. */
343 int num_input_events;
344
345 /* Total number of times read_char has returned, outside of macros. */
346 int num_nonmacro_input_events;
347
348 /* Auto-save automatically when this many characters have been typed
349 since the last time. */
350
351 static int auto_save_interval;
352
353 /* Value of num_nonmacro_input_events as of last auto save. */
354
355 int last_auto_save;
356
357 /* The command being executed by the command loop.
358 Commands may set this, and the value set will be copied into
359 current_kboard->Vlast_command instead of the actual command. */
360 Lisp_Object Vthis_command;
361
362 /* This is like Vthis_command, except that commands never set it. */
363 Lisp_Object real_this_command;
364
365 /* The value of point when the last command was executed. */
366 int last_point_position;
367
368 /* The buffer that was current when the last command was started. */
369 Lisp_Object last_point_position_buffer;
370
371 /* The frame in which the last input event occurred, or Qmacro if the
372 last event came from a macro. We use this to determine when to
373 generate switch-frame events. This may be cleared by functions
374 like Fselect_frame, to make sure that a switch-frame event is
375 generated by the next character. */
376 Lisp_Object internal_last_event_frame;
377
378 /* A user-visible version of the above, intended to allow users to
379 figure out where the last event came from, if the event doesn't
380 carry that information itself (i.e. if it was a character). */
381 Lisp_Object Vlast_event_frame;
382
383 /* The timestamp of the last input event we received from the X server.
384 X Windows wants this for selection ownership. */
385 unsigned long last_event_timestamp;
386
387 Lisp_Object Qself_insert_command;
388 Lisp_Object Qforward_char;
389 Lisp_Object Qbackward_char;
390 Lisp_Object Qundefined;
391 Lisp_Object Qtimer_event_handler;
392
393 /* read_key_sequence stores here the command definition of the
394 key sequence that it reads. */
395 Lisp_Object read_key_sequence_cmd;
396
397 /* Echo unfinished commands after this many seconds of pause. */
398 Lisp_Object Vecho_keystrokes;
399
400 /* Form to evaluate (if non-nil) when Emacs is started. */
401 Lisp_Object Vtop_level;
402
403 /* User-supplied string to translate input characters through. */
404 Lisp_Object Vkeyboard_translate_table;
405
406 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
407 extern Lisp_Object Vfunction_key_map;
408
409 /* Another keymap that maps key sequences into key sequences.
410 This one takes precedence over ordinary definitions. */
411 extern Lisp_Object Vkey_translation_map;
412
413 /* If non-nil, this implements the current input method. */
414 Lisp_Object Vinput_method_function;
415 Lisp_Object Qinput_method_function;
416
417 /* When we call Vinput_method_function,
418 this holds the echo area message that was just erased. */
419 Lisp_Object Vinput_method_previous_message;
420
421 /* Non-nil means deactivate the mark at end of this command. */
422 Lisp_Object Vdeactivate_mark;
423
424 /* Menu bar specified in Lucid Emacs fashion. */
425
426 Lisp_Object Vlucid_menu_bar_dirty_flag;
427 Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
428
429 Lisp_Object Qecho_area_clear_hook;
430
431 /* Hooks to run before and after each command. */
432 Lisp_Object Qpre_command_hook, Vpre_command_hook;
433 Lisp_Object Qpost_command_hook, Vpost_command_hook;
434 Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
435 /* Hook run after a command if there's no more input soon. */
436 Lisp_Object Qpost_command_idle_hook, Vpost_command_idle_hook;
437
438 /* Delay time in microseconds before running post-command-idle-hook. */
439 int post_command_idle_delay;
440
441 /* List of deferred actions to be performed at a later time.
442 The precise format isn't relevant here; we just check whether it is nil. */
443 Lisp_Object Vdeferred_action_list;
444
445 /* Function to call to handle deferred actions, when there are any. */
446 Lisp_Object Vdeferred_action_function;
447 Lisp_Object Qdeferred_action_function;
448
449 Lisp_Object Qinput_method_exit_on_first_char;
450 Lisp_Object Qinput_method_use_echo_area;
451
452 /* File in which we write all commands we read. */
453 FILE *dribble;
454
455 /* Nonzero if input is available. */
456 int input_pending;
457
458 /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
459 keep 0200 bit in input chars. 0 to ignore the 0200 bit. */
460
461 int meta_key;
462
463 /* Non-zero means force key bindings update in parse_menu_item. */
464
465 int update_menu_bindings;
466
467 extern char *pending_malloc_warning;
468
469 /* Circular buffer for pre-read keyboard input. */
470
471 static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
472
473 /* Vector to GCPRO the Lisp objects referenced from kbd_buffer.
474
475 The interrupt-level event handlers will never enqueue an event on a
476 frame which is not in Vframe_list, and once an event is dequeued,
477 internal_last_event_frame or the event itself points to the frame.
478 So that's all fine.
479
480 But while the event is sitting in the queue, it's completely
481 unprotected. Suppose the user types one command which will run for
482 a while and then delete a frame, and then types another event at
483 the frame that will be deleted, before the command gets around to
484 it. Suppose there are no references to this frame elsewhere in
485 Emacs, and a GC occurs before the second event is dequeued. Now we
486 have an event referring to a freed frame, which will crash Emacs
487 when it is dequeued.
488
489 Similar things happen when an event on a scroll bar is enqueued; the
490 window may be deleted while the event is in the queue.
491
492 So, we use this vector to protect the Lisp_Objects in the event
493 queue. That way, they'll be dequeued as dead frames or windows,
494 but still valid Lisp objects.
495
496 If kbd_buffer[i].kind != no_event, then
497
498 AREF (kbd_buffer_gcpro, 2 * i) == kbd_buffer[i].frame_or_window.
499 AREF (kbd_buffer_gcpro, 2 * i + 1) == kbd_buffer[i].arg. */
500
501 static Lisp_Object kbd_buffer_gcpro;
502
503 /* Pointer to next available character in kbd_buffer.
504 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
505 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
506 next available char is in kbd_buffer[0]. */
507 static struct input_event *kbd_fetch_ptr;
508
509 /* Pointer to next place to store character in kbd_buffer. This
510 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
511 character should go in kbd_buffer[0]. */
512 static struct input_event * volatile kbd_store_ptr;
513
514 /* The above pair of variables forms a "queue empty" flag. When we
515 enqueue a non-hook event, we increment kbd_store_ptr. When we
516 dequeue a non-hook event, we increment kbd_fetch_ptr. We say that
517 there is input available iff the two pointers are not equal.
518
519 Why not just have a flag set and cleared by the enqueuing and
520 dequeuing functions? Such a flag could be screwed up by interrupts
521 at inopportune times. */
522
523 /* If this flag is non-nil, we check mouse_moved to see when the
524 mouse moves, and motion events will appear in the input stream.
525 Otherwise, mouse motion is ignored. */
526 Lisp_Object do_mouse_tracking;
527
528 /* Symbols to head events. */
529 Lisp_Object Qmouse_movement;
530 Lisp_Object Qscroll_bar_movement;
531 Lisp_Object Qswitch_frame;
532 Lisp_Object Qdelete_frame;
533 Lisp_Object Qiconify_frame;
534 Lisp_Object Qmake_frame_visible;
535 Lisp_Object Qhelp_echo;
536
537 /* Symbols to denote kinds of events. */
538 Lisp_Object Qfunction_key;
539 Lisp_Object Qmouse_click;
540 #ifdef WINDOWSNT
541 Lisp_Object Qmouse_wheel;
542 Lisp_Object Qlanguage_change;
543 #endif
544 Lisp_Object Qdrag_n_drop;
545 /* Lisp_Object Qmouse_movement; - also an event header */
546
547 /* Properties of event headers. */
548 Lisp_Object Qevent_kind;
549 Lisp_Object Qevent_symbol_elements;
550
551 /* menu item parts */
552 Lisp_Object Qmenu_alias;
553 Lisp_Object Qmenu_enable;
554 Lisp_Object QCenable, QCvisible, QChelp, QCfilter, QCkeys, QCkey_sequence;
555 Lisp_Object QCbutton, QCtoggle, QCradio;
556 extern Lisp_Object Vdefine_key_rebound_commands;
557 extern Lisp_Object Qmenu_item;
558
559 /* An event header symbol HEAD may have a property named
560 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
561 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
562 mask of modifiers applied to it. If present, this is used to help
563 speed up parse_modifiers. */
564 Lisp_Object Qevent_symbol_element_mask;
565
566 /* An unmodified event header BASE may have a property named
567 Qmodifier_cache, which is an alist mapping modifier masks onto
568 modified versions of BASE. If present, this helps speed up
569 apply_modifiers. */
570 Lisp_Object Qmodifier_cache;
571
572 /* Symbols to use for parts of windows. */
573 Lisp_Object Qmode_line;
574 Lisp_Object Qvertical_line;
575 Lisp_Object Qvertical_scroll_bar;
576 Lisp_Object Qmenu_bar;
577
578 Lisp_Object recursive_edit_unwind (), command_loop ();
579 Lisp_Object Fthis_command_keys ();
580 Lisp_Object Qextended_command_history;
581 EMACS_TIME timer_check ();
582
583 extern Lisp_Object Vhistory_length;
584
585 extern char *x_get_keysym_name ();
586
587 static void record_menu_key ();
588
589 Lisp_Object Qpolling_period;
590
591 /* List of absolute timers. Appears in order of next scheduled event. */
592 Lisp_Object Vtimer_list;
593
594 /* List of idle time timers. Appears in order of next scheduled event. */
595 Lisp_Object Vtimer_idle_list;
596
597 /* Incremented whenever a timer is run. */
598 int timers_run;
599
600 extern Lisp_Object Vprint_level, Vprint_length;
601
602 /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
603 happens. */
604 EMACS_TIME *input_available_clear_time;
605
606 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
607 Default is 1 if INTERRUPT_INPUT is defined. */
608 int interrupt_input;
609
610 /* Nonzero while interrupts are temporarily deferred during redisplay. */
611 int interrupts_deferred;
612
613 /* Nonzero means use ^S/^Q for flow control. */
614 int flow_control;
615
616 /* Allow m- file to inhibit use of FIONREAD. */
617 #ifdef BROKEN_FIONREAD
618 #undef FIONREAD
619 #endif
620
621 /* We are unable to use interrupts if FIONREAD is not available,
622 so flush SIGIO so we won't try. */
623 #ifndef FIONREAD
624 #ifdef SIGIO
625 #undef SIGIO
626 #endif
627 #endif
628
629 /* If we support a window system, turn on the code to poll periodically
630 to detect C-g. It isn't actually used when doing interrupt input. */
631 #ifdef HAVE_WINDOW_SYSTEM
632 #define POLL_FOR_INPUT
633 #endif
634
635 /* After a command is executed, if point is moved into a region that
636 has specific properties (e.g. composition, display), we adjust
637 point to the boundary of the region. But, if a command sets this
638 valiable to non-nil, we suppress this point adjustment. This
639 variable is set to nil before reading a command. */
640
641 Lisp_Object Vdisable_point_adjustment;
642
643 /* If non-nil, always disable point adjustment. */
644
645 Lisp_Object Vglobal_disable_point_adjustment;
646
647 \f
648 /* Global variable declarations. */
649
650 /* Function for init_keyboard to call with no args (if nonzero). */
651 void (*keyboard_init_hook) ();
652
653 static int read_avail_input P_ ((int));
654 static void get_input_pending P_ ((int *, int));
655 static int readable_events P_ ((int));
656 static Lisp_Object read_char_x_menu_prompt P_ ((int, Lisp_Object *,
657 Lisp_Object, int *));
658 static Lisp_Object read_char_x_menu_prompt ();
659 static Lisp_Object read_char_minibuf_menu_prompt P_ ((int, int,
660 Lisp_Object *));
661 static Lisp_Object make_lispy_event P_ ((struct input_event *));
662 #ifdef HAVE_MOUSE
663 static Lisp_Object make_lispy_movement P_ ((struct frame *, Lisp_Object,
664 enum scroll_bar_part,
665 Lisp_Object, Lisp_Object,
666 unsigned long));
667 #endif
668 static Lisp_Object modify_event_symbol P_ ((int, unsigned, Lisp_Object,
669 Lisp_Object, char **,
670 Lisp_Object *, unsigned));
671 static Lisp_Object make_lispy_switch_frame P_ ((Lisp_Object));
672 static int parse_solitary_modifier P_ ((Lisp_Object));
673 static int parse_solitary_modifier ();
674 static void save_getcjmp P_ ((jmp_buf));
675 static void save_getcjmp ();
676 static void restore_getcjmp P_ ((jmp_buf));
677 static Lisp_Object apply_modifiers P_ ((int, Lisp_Object));
678 static void clear_event P_ ((struct input_event *));
679
680 /* Nonzero means don't try to suspend even if the operating system seems
681 to support it. */
682 static int cannot_suspend;
683
684 #define min(a,b) ((a)<(b)?(a):(b))
685 #define max(a,b) ((a)>(b)?(a):(b))
686
687 /* Install the string STR as the beginning of the string of echoing,
688 so that it serves as a prompt for the next character.
689 Also start echoing. */
690
691 void
692 echo_prompt (str)
693 Lisp_Object str;
694 {
695 int nbytes = STRING_BYTES (XSTRING (str));
696 int multibyte_p = STRING_MULTIBYTE (str);
697
698 if (nbytes > ECHOBUFSIZE - 4)
699 {
700 if (multibyte_p)
701 {
702 /* Have to find the last character that fit's into the
703 echo buffer. */
704 unsigned char *p = XSTRING (str)->data;
705 unsigned char *pend = p + ECHOBUFSIZE - 4;
706 int char_len;
707
708 do
709 {
710 PARSE_MULTIBYTE_SEQ (p, pend - p, char_len);
711 p += char_len;
712 }
713 while (p < pend);
714
715 nbytes = p - XSTRING (str)->data - char_len;
716 }
717 else
718 nbytes = ECHOBUFSIZE - 4;
719 }
720
721 nbytes = copy_text (XSTRING (str)->data, current_kboard->echobuf, nbytes,
722 STRING_MULTIBYTE (str), 1);
723 current_kboard->echoptr = current_kboard->echobuf + nbytes;
724 *current_kboard->echoptr = '\0';
725 current_kboard->echo_after_prompt = nbytes;
726
727 echo_now ();
728 }
729
730 /* Add C to the echo string, if echoing is going on.
731 C can be a character, which is printed prettily ("M-C-x" and all that
732 jazz), or a symbol, whose name is printed. */
733
734 void
735 echo_char (c)
736 Lisp_Object c;
737 {
738 if (current_kboard->immediate_echo)
739 {
740 char *ptr = current_kboard->echoptr;
741
742 if (ptr != current_kboard->echobuf)
743 *ptr++ = ' ';
744
745 /* If someone has passed us a composite event, use its head symbol. */
746 c = EVENT_HEAD (c);
747
748 if (INTEGERP (c))
749 {
750 int ch = XINT (c);
751
752 if (ptr - current_kboard->echobuf
753 > ECHOBUFSIZE - KEY_DESCRIPTION_SIZE)
754 return;
755
756 ptr = push_key_description (ch, ptr, 1);
757 }
758 else if (SYMBOLP (c))
759 {
760 struct Lisp_String *name = XSYMBOL (c)->name;
761 if ((ptr - current_kboard->echobuf) + STRING_BYTES (name) + 4
762 > ECHOBUFSIZE)
763 return;
764 ptr += copy_text (name->data, ptr, STRING_BYTES (name),
765 name->size_byte >= 0, 1);
766 }
767
768 if (current_kboard->echoptr == current_kboard->echobuf
769 && help_char_p (c))
770 {
771 strcpy (ptr, " (Type ? for further options)");
772 ptr += strlen (ptr);
773 }
774
775 *ptr = 0;
776 current_kboard->echoptr = ptr;
777
778 echo_now ();
779 }
780 }
781
782 /* Temporarily add a dash to the end of the echo string if it's not
783 empty, so that it serves as a mini-prompt for the very next character. */
784
785 void
786 echo_dash ()
787 {
788 if (!current_kboard->immediate_echo
789 && current_kboard->echoptr == current_kboard->echobuf)
790 return;
791 /* Do nothing if we just printed a prompt. */
792 if (current_kboard->echo_after_prompt
793 == current_kboard->echoptr - current_kboard->echobuf)
794 return;
795 /* Do nothing if not echoing at all. */
796 if (current_kboard->echoptr == 0)
797 return;
798
799 /* Put a dash at the end of the buffer temporarily,
800 but make it go away when the next character is added. */
801 current_kboard->echoptr[0] = '-';
802 current_kboard->echoptr[1] = 0;
803
804 echo_now ();
805 }
806
807 /* Display the current echo string, and begin echoing if not already
808 doing so. */
809
810 void
811 echo_now ()
812 {
813 if (!current_kboard->immediate_echo)
814 {
815 int i;
816 current_kboard->immediate_echo = 1;
817
818 for (i = 0; i < this_command_key_count; i++)
819 {
820 Lisp_Object c;
821 c = XVECTOR (this_command_keys)->contents[i];
822 if (! (EVENT_HAS_PARAMETERS (c)
823 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
824 echo_char (c);
825 }
826 echo_dash ();
827 }
828
829 echoing = 1;
830 message2_nolog (current_kboard->echobuf, strlen (current_kboard->echobuf),
831 1);
832 echoing = 0;
833
834 /* Record in what buffer we echoed, and from which kboard. */
835 echo_message_buffer = echo_area_buffer[0];
836 echo_kboard = current_kboard;
837
838 if (waiting_for_input && !NILP (Vquit_flag))
839 quit_throw_to_read_char ();
840 }
841
842 /* Turn off echoing, for the start of a new command. */
843
844 void
845 cancel_echoing ()
846 {
847 current_kboard->immediate_echo = 0;
848 current_kboard->echoptr = current_kboard->echobuf;
849 current_kboard->echo_after_prompt = -1;
850 ok_to_echo_at_next_pause = NULL;
851 echo_kboard = NULL;
852 echo_message_buffer = Qnil;
853 }
854
855 /* Return the length of the current echo string. */
856
857 static int
858 echo_length ()
859 {
860 return current_kboard->echoptr - current_kboard->echobuf;
861 }
862
863 /* Truncate the current echo message to its first LEN chars.
864 This and echo_char get used by read_key_sequence when the user
865 switches frames while entering a key sequence. */
866
867 static void
868 echo_truncate (len)
869 int len;
870 {
871 current_kboard->echobuf[len] = '\0';
872 current_kboard->echoptr = current_kboard->echobuf + len;
873 truncate_echo_area (len);
874 }
875
876 \f
877 /* Functions for manipulating this_command_keys. */
878 static void
879 add_command_key (key)
880 Lisp_Object key;
881 {
882 int size = XVECTOR (this_command_keys)->size;
883
884 /* If reset-this-command-length was called recently, obey it now.
885 See the doc string of that function for an explanation of why. */
886 if (before_command_restore_flag)
887 {
888 this_command_key_count = before_command_key_count_1;
889 if (this_command_key_count < this_single_command_key_start)
890 this_single_command_key_start = this_command_key_count;
891 echo_truncate (before_command_echo_length_1);
892 before_command_restore_flag = 0;
893 }
894
895 if (this_command_key_count >= size)
896 {
897 Lisp_Object new_keys;
898
899 new_keys = Fmake_vector (make_number (size * 2), Qnil);
900 bcopy (XVECTOR (this_command_keys)->contents,
901 XVECTOR (new_keys)->contents,
902 size * sizeof (Lisp_Object));
903
904 this_command_keys = new_keys;
905 }
906
907 XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
908 }
909 \f
910 Lisp_Object
911 recursive_edit_1 ()
912 {
913 int count = specpdl_ptr - specpdl;
914 Lisp_Object val;
915
916 if (command_loop_level > 0)
917 {
918 specbind (Qstandard_output, Qt);
919 specbind (Qstandard_input, Qt);
920 }
921
922 #ifdef HAVE_X_WINDOWS
923 /* The command loop has started an hourglass timer, so we have to
924 cancel it here, otherwise it will fire because the recursive edit
925 can take some time. */
926 if (display_hourglass_p)
927 cancel_hourglass ();
928 #endif
929
930 /* This function may have been called from a debugger called from
931 within redisplay, for instance by Edebugging a function called
932 from fontification-functions. We want to allow redisplay in
933 the debugging session.
934
935 The recursive edit is left with a `(throw exit ...)'. The `exit'
936 tag is not caught anywhere in redisplay, i.e. when we leave the
937 recursive edit, the original redisplay leading to the recursive
938 edit will be unwound. The outcome should therefore be safe. */
939 specbind (Qinhibit_redisplay, Qnil);
940 redisplaying_p = 0;
941
942 val = command_loop ();
943 if (EQ (val, Qt))
944 Fsignal (Qquit, Qnil);
945 /* Handle throw from read_minibuf when using minibuffer
946 while it's active but we're in another window. */
947 if (STRINGP (val))
948 Fsignal (Qerror, Fcons (val, Qnil));
949
950 return unbind_to (count, Qnil);
951 }
952
953 /* When an auto-save happens, record the "time", and don't do again soon. */
954
955 void
956 record_auto_save ()
957 {
958 last_auto_save = num_nonmacro_input_events;
959 }
960
961 /* Make an auto save happen as soon as possible at command level. */
962
963 void
964 force_auto_save_soon ()
965 {
966 last_auto_save = - auto_save_interval - 1;
967
968 record_asynch_buffer_change ();
969 }
970 \f
971 DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
972 "Invoke the editor command loop recursively.\n\
973 To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
974 that tells this function to return.\n\
975 Alternately, `(throw 'exit t)' makes this function signal an error.\n\
976 This function is called by the editor initialization to begin editing.")
977 ()
978 {
979 int count = specpdl_ptr - specpdl;
980
981 command_loop_level++;
982 update_mode_lines = 1;
983
984 record_unwind_protect (recursive_edit_unwind,
985 (command_loop_level
986 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
987 ? Fcurrent_buffer ()
988 : Qnil);
989 recursive_edit_1 ();
990 return unbind_to (count, Qnil);
991 }
992
993 Lisp_Object
994 recursive_edit_unwind (buffer)
995 Lisp_Object buffer;
996 {
997 if (!NILP (buffer))
998 Fset_buffer (buffer);
999
1000 command_loop_level--;
1001 update_mode_lines = 1;
1002 return Qnil;
1003 }
1004 \f
1005 static void
1006 any_kboard_state ()
1007 {
1008 #ifdef MULTI_KBOARD
1009 #if 0 /* Theory: if there's anything in Vunread_command_events,
1010 it will right away be read by read_key_sequence,
1011 and then if we do switch KBOARDS, it will go into the side
1012 queue then. So we don't need to do anything special here -- rms. */
1013 if (CONSP (Vunread_command_events))
1014 {
1015 current_kboard->kbd_queue
1016 = nconc2 (Vunread_command_events, current_kboard->kbd_queue);
1017 current_kboard->kbd_queue_has_data = 1;
1018 }
1019 Vunread_command_events = Qnil;
1020 #endif
1021 single_kboard = 0;
1022 #endif
1023 }
1024
1025 /* Switch to the single-kboard state, making current_kboard
1026 the only KBOARD from which further input is accepted. */
1027
1028 void
1029 single_kboard_state ()
1030 {
1031 #ifdef MULTI_KBOARD
1032 single_kboard = 1;
1033 #endif
1034 }
1035
1036 /* Maintain a stack of kboards, so other parts of Emacs
1037 can switch temporarily to the kboard of a given frame
1038 and then revert to the previous status. */
1039
1040 struct kboard_stack
1041 {
1042 KBOARD *kboard;
1043 struct kboard_stack *next;
1044 };
1045
1046 static struct kboard_stack *kboard_stack;
1047
1048 void
1049 push_frame_kboard (f)
1050 FRAME_PTR f;
1051 {
1052 #ifdef MULTI_KBOARD
1053 struct kboard_stack *p
1054 = (struct kboard_stack *) xmalloc (sizeof (struct kboard_stack));
1055
1056 p->next = kboard_stack;
1057 p->kboard = current_kboard;
1058 kboard_stack = p;
1059
1060 current_kboard = FRAME_KBOARD (f);
1061 #endif
1062 }
1063
1064 void
1065 pop_frame_kboard ()
1066 {
1067 #ifdef MULTI_KBOARD
1068 struct kboard_stack *p = kboard_stack;
1069 current_kboard = p->kboard;
1070 kboard_stack = p->next;
1071 xfree (p);
1072 #endif
1073 }
1074 \f
1075 /* Handle errors that are not handled at inner levels
1076 by printing an error message and returning to the editor command loop. */
1077
1078 Lisp_Object
1079 cmd_error (data)
1080 Lisp_Object data;
1081 {
1082 Lisp_Object old_level, old_length;
1083 char macroerror[50];
1084
1085 if (!NILP (executing_macro))
1086 {
1087 if (executing_macro_iterations == 1)
1088 sprintf (macroerror, "After 1 kbd macro iteration: ");
1089 else
1090 sprintf (macroerror, "After %d kbd macro iterations: ",
1091 executing_macro_iterations);
1092 }
1093 else
1094 *macroerror = 0;
1095
1096 Vstandard_output = Qt;
1097 Vstandard_input = Qt;
1098 Vexecuting_macro = Qnil;
1099 executing_macro = Qnil;
1100 current_kboard->Vprefix_arg = Qnil;
1101 current_kboard->Vlast_prefix_arg = Qnil;
1102 cancel_echoing ();
1103
1104 /* Avoid unquittable loop if data contains a circular list. */
1105 old_level = Vprint_level;
1106 old_length = Vprint_length;
1107 XSETFASTINT (Vprint_level, 10);
1108 XSETFASTINT (Vprint_length, 10);
1109 cmd_error_internal (data, macroerror);
1110 Vprint_level = old_level;
1111 Vprint_length = old_length;
1112
1113 Vquit_flag = Qnil;
1114
1115 Vinhibit_quit = Qnil;
1116 #ifdef MULTI_KBOARD
1117 any_kboard_state ();
1118 #endif
1119
1120 return make_number (0);
1121 }
1122
1123 /* Take actions on handling an error. DATA is the data that describes
1124 the error.
1125
1126 CONTEXT is a C-string containing ASCII characters only which
1127 describes the context in which the error happened. If we need to
1128 generalize CONTEXT to allow multibyte characters, make it a Lisp
1129 string. */
1130
1131 void
1132 cmd_error_internal (data, context)
1133 Lisp_Object data;
1134 char *context;
1135 {
1136 Lisp_Object stream;
1137 int kill_emacs_p = 0;
1138 struct frame *sf = SELECTED_FRAME ();
1139
1140 Vquit_flag = Qnil;
1141 Vinhibit_quit = Qt;
1142 clear_message (1, 0);
1143
1144 /* If the window system or terminal frame hasn't been initialized
1145 yet, or we're not interactive, it's best to dump this message out
1146 to stderr and exit. */
1147 if (!sf->glyphs_initialized_p
1148 /* This is the case of the frame dumped with Emacs, when we're
1149 running under a window system. */
1150 || (!NILP (Vwindow_system)
1151 && !inhibit_window_system
1152 && FRAME_TERMCAP_P (sf))
1153 || noninteractive)
1154 {
1155 stream = Qexternal_debugging_output;
1156 kill_emacs_p = 1;
1157 }
1158 else
1159 {
1160 Fdiscard_input ();
1161 bitch_at_user ();
1162 stream = Qt;
1163 }
1164
1165 if (context != 0)
1166 write_string_1 (context, -1, stream);
1167
1168 print_error_message (data, stream);
1169
1170 /* If the window system or terminal frame hasn't been initialized
1171 yet, or we're in -batch mode, this error should cause Emacs to exit. */
1172 if (kill_emacs_p)
1173 {
1174 Fterpri (stream);
1175 Fkill_emacs (make_number (-1));
1176 }
1177 }
1178 \f
1179 Lisp_Object command_loop_1 ();
1180 Lisp_Object command_loop_2 ();
1181 Lisp_Object top_level_1 ();
1182
1183 /* Entry to editor-command-loop.
1184 This level has the catches for exiting/returning to editor command loop.
1185 It returns nil to exit recursive edit, t to abort it. */
1186
1187 Lisp_Object
1188 command_loop ()
1189 {
1190 if (command_loop_level > 0 || minibuf_level > 0)
1191 {
1192 Lisp_Object val;
1193 val = internal_catch (Qexit, command_loop_2, Qnil);
1194 executing_macro = Qnil;
1195 return val;
1196 }
1197 else
1198 while (1)
1199 {
1200 internal_catch (Qtop_level, top_level_1, Qnil);
1201 internal_catch (Qtop_level, command_loop_2, Qnil);
1202 executing_macro = Qnil;
1203
1204 /* End of file in -batch run causes exit here. */
1205 if (noninteractive)
1206 Fkill_emacs (Qt);
1207 }
1208 }
1209
1210 /* Here we catch errors in execution of commands within the
1211 editing loop, and reenter the editing loop.
1212 When there is an error, cmd_error runs and returns a non-nil
1213 value to us. A value of nil means that cmd_loop_1 itself
1214 returned due to end of file (or end of kbd macro). */
1215
1216 Lisp_Object
1217 command_loop_2 ()
1218 {
1219 register Lisp_Object val;
1220
1221 do
1222 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
1223 while (!NILP (val));
1224
1225 return Qnil;
1226 }
1227
1228 Lisp_Object
1229 top_level_2 ()
1230 {
1231 return Feval (Vtop_level);
1232 }
1233
1234 Lisp_Object
1235 top_level_1 ()
1236 {
1237 /* On entry to the outer level, run the startup file */
1238 if (!NILP (Vtop_level))
1239 internal_condition_case (top_level_2, Qerror, cmd_error);
1240 else if (!NILP (Vpurify_flag))
1241 message ("Bare impure Emacs (standard Lisp code not loaded)");
1242 else
1243 message ("Bare Emacs (standard Lisp code not loaded)");
1244 return Qnil;
1245 }
1246
1247 DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
1248 "Exit all recursive editing levels.")
1249 ()
1250 {
1251 #ifdef HAVE_X_WINDOWS
1252 if (display_hourglass_p)
1253 cancel_hourglass ();
1254 #endif
1255 return Fthrow (Qtop_level, Qnil);
1256 }
1257
1258 DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
1259 "Exit from the innermost recursive edit or minibuffer.")
1260 ()
1261 {
1262 if (command_loop_level > 0 || minibuf_level > 0)
1263 Fthrow (Qexit, Qnil);
1264
1265 error ("No recursive edit is in progress");
1266 return Qnil;
1267 }
1268
1269 DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
1270 "Abort the command that requested this recursive edit or minibuffer input.")
1271 ()
1272 {
1273 if (command_loop_level > 0 || minibuf_level > 0)
1274 Fthrow (Qexit, Qt);
1275
1276 error ("No recursive edit is in progress");
1277 return Qnil;
1278 }
1279 \f
1280 /* This is the actual command reading loop,
1281 sans error-handling encapsulation. */
1282
1283 EXFUN (Fcommand_execute, 4);
1284 static int read_key_sequence P_ ((Lisp_Object *, int, Lisp_Object,
1285 int, int, int));
1286 void safe_run_hooks P_ ((Lisp_Object));
1287 static void adjust_point_for_property P_ ((int));
1288
1289 Lisp_Object
1290 command_loop_1 ()
1291 {
1292 Lisp_Object cmd;
1293 int lose;
1294 int nonundocount;
1295 Lisp_Object keybuf[30];
1296 int i;
1297 int no_direct;
1298 int prev_modiff;
1299 struct buffer *prev_buffer = NULL;
1300 #ifdef MULTI_KBOARD
1301 int was_locked = single_kboard;
1302 #endif
1303
1304 current_kboard->Vprefix_arg = Qnil;
1305 current_kboard->Vlast_prefix_arg = Qnil;
1306 Vdeactivate_mark = Qnil;
1307 waiting_for_input = 0;
1308 cancel_echoing ();
1309
1310 nonundocount = 0;
1311 this_command_key_count = 0;
1312 this_single_command_key_start = 0;
1313
1314 /* Make sure this hook runs after commands that get errors and
1315 throw to top level. */
1316 /* Note that the value cell will never directly contain nil
1317 if the symbol is a local variable. */
1318 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1319 safe_run_hooks (Qpost_command_hook);
1320
1321 /* If displaying a message, resize the echo area window to fit
1322 that message's size exactly. */
1323 if (!NILP (echo_area_buffer[0]))
1324 resize_echo_area_axactly ();
1325
1326 if (!NILP (Vdeferred_action_list))
1327 call0 (Vdeferred_action_function);
1328
1329 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1330 {
1331 if (NILP (Vunread_command_events)
1332 && NILP (Vunread_input_method_events)
1333 && NILP (Vunread_post_input_method_events)
1334 && NILP (Vexecuting_macro)
1335 && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
1336 safe_run_hooks (Qpost_command_idle_hook);
1337 }
1338
1339 /* Do this after running Vpost_command_hook, for consistency. */
1340 current_kboard->Vlast_command = Vthis_command;
1341 current_kboard->Vreal_last_command = real_this_command;
1342
1343 while (1)
1344 {
1345 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
1346 Fkill_emacs (Qnil);
1347
1348 /* Make sure the current window's buffer is selected. */
1349 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1350 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1351
1352 /* Display any malloc warning that just came out. Use while because
1353 displaying one warning can cause another. */
1354
1355 while (pending_malloc_warning)
1356 display_malloc_warning ();
1357
1358 no_direct = 0;
1359
1360 Vdeactivate_mark = Qnil;
1361
1362 /* If minibuffer on and echo area in use,
1363 wait a short time and redraw minibuffer. */
1364
1365 if (minibuf_level
1366 && !NILP (echo_area_buffer[0])
1367 && EQ (minibuf_window, echo_area_window)
1368 && NUMBERP (Vminibuffer_message_timeout))
1369 {
1370 /* Bind inhibit-quit to t so that C-g gets read in
1371 rather than quitting back to the minibuffer. */
1372 int count = specpdl_ptr - specpdl;
1373 specbind (Qinhibit_quit, Qt);
1374
1375 Fsit_for (Vminibuffer_message_timeout, Qnil, Qnil);
1376 /* Clear the echo area. */
1377 message2 (0, 0, 0);
1378 safe_run_hooks (Qecho_area_clear_hook);
1379
1380 unbind_to (count, Qnil);
1381
1382 /* If a C-g came in before, treat it as input now. */
1383 if (!NILP (Vquit_flag))
1384 {
1385 Vquit_flag = Qnil;
1386 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1387 }
1388 }
1389
1390 #ifdef C_ALLOCA
1391 alloca (0); /* Cause a garbage collection now */
1392 /* Since we can free the most stuff here. */
1393 #endif /* C_ALLOCA */
1394
1395 #if 0
1396 /* Select the frame that the last event came from. Usually,
1397 switch-frame events will take care of this, but if some lisp
1398 code swallows a switch-frame event, we'll fix things up here.
1399 Is this a good idea? */
1400 if (FRAMEP (internal_last_event_frame)
1401 && !EQ (internal_last_event_frame, selected_frame))
1402 Fselect_frame (internal_last_event_frame, Qnil);
1403 #endif
1404 /* If it has changed current-menubar from previous value,
1405 really recompute the menubar from the value. */
1406 if (! NILP (Vlucid_menu_bar_dirty_flag)
1407 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
1408 call0 (Qrecompute_lucid_menubar);
1409
1410 before_command_key_count = this_command_key_count;
1411 before_command_echo_length = echo_length ();
1412
1413 Vthis_command = Qnil;
1414 real_this_command = Qnil;
1415
1416 /* Read next key sequence; i gets its length. */
1417 i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
1418 Qnil, 0, 1, 1);
1419
1420 /* A filter may have run while we were reading the input. */
1421 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
1422 Fkill_emacs (Qnil);
1423 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1424 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1425
1426 ++num_input_keys;
1427
1428 /* Now we have read a key sequence of length I,
1429 or else I is 0 and we found end of file. */
1430
1431 if (i == 0) /* End of file -- happens only in */
1432 return Qnil; /* a kbd macro, at the end. */
1433 /* -1 means read_key_sequence got a menu that was rejected.
1434 Just loop around and read another command. */
1435 if (i == -1)
1436 {
1437 cancel_echoing ();
1438 this_command_key_count = 0;
1439 this_single_command_key_start = 0;
1440 goto finalize;
1441 }
1442
1443 last_command_char = keybuf[i - 1];
1444
1445 /* If the previous command tried to force a specific window-start,
1446 forget about that, in case this command moves point far away
1447 from that position. But also throw away beg_unchanged and
1448 end_unchanged information in that case, so that redisplay will
1449 update the whole window properly. */
1450 if (!NILP (XWINDOW (selected_window)->force_start))
1451 {
1452 struct buffer *b;
1453 XWINDOW (selected_window)->force_start = Qnil;
1454 b = XBUFFER (XWINDOW (selected_window)->buffer);
1455 BUF_BEG_UNCHANGED (b) = BUF_END_UNCHANGED (b) = 0;
1456 }
1457
1458 cmd = read_key_sequence_cmd;
1459 if (!NILP (Vexecuting_macro))
1460 {
1461 if (!NILP (Vquit_flag))
1462 {
1463 Vexecuting_macro = Qt;
1464 QUIT; /* Make some noise. */
1465 /* Will return since macro now empty. */
1466 }
1467 }
1468
1469 /* Do redisplay processing after this command except in special
1470 cases identified below. */
1471 prev_buffer = current_buffer;
1472 prev_modiff = MODIFF;
1473 last_point_position = PT;
1474 XSETBUFFER (last_point_position_buffer, prev_buffer);
1475
1476 /* By default, we adjust point to a boundary of a region that
1477 has such a property that should be treated intangible
1478 (e.g. composition, display). But, some commands will set
1479 this variable differently. */
1480 Vdisable_point_adjustment = Qnil;
1481
1482 /* Process filters and timers may have messed with deactivate-mark.
1483 reset it before we execute the command. */
1484 Vdeactivate_mark = Qnil;
1485
1486 /* Execute the command. */
1487
1488 Vthis_command = cmd;
1489 real_this_command = cmd;
1490 /* Note that the value cell will never directly contain nil
1491 if the symbol is a local variable. */
1492 if (!NILP (Vpre_command_hook) && !NILP (Vrun_hooks))
1493 safe_run_hooks (Qpre_command_hook);
1494
1495 if (NILP (Vthis_command))
1496 {
1497 /* nil means key is undefined. */
1498 bitch_at_user ();
1499 current_kboard->defining_kbd_macro = Qnil;
1500 update_mode_lines = 1;
1501 current_kboard->Vprefix_arg = Qnil;
1502 }
1503 else
1504 {
1505 if (NILP (current_kboard->Vprefix_arg) && ! no_direct)
1506 {
1507 /* In case we jump to directly_done. */
1508 Vcurrent_prefix_arg = current_kboard->Vprefix_arg;
1509
1510 /* Recognize some common commands in common situations and
1511 do them directly. */
1512 if (EQ (Vthis_command, Qforward_char) && PT < ZV)
1513 {
1514 struct Lisp_Char_Table *dp
1515 = window_display_table (XWINDOW (selected_window));
1516 lose = FETCH_CHAR (PT_BYTE);
1517 SET_PT (PT + 1);
1518 if ((dp
1519 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1520 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1521 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1522 && (lose >= 0x20 && lose < 0x7f)))
1523 : (lose >= 0x20 && lose < 0x7f))
1524 /* To extract the case of continuation on
1525 wide-column characters. */
1526 && (WIDTH_BY_CHAR_HEAD (FETCH_BYTE (PT_BYTE)) == 1)
1527 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1528 >= MODIFF)
1529 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1530 >= OVERLAY_MODIFF)
1531 && (XFASTINT (XWINDOW (selected_window)->last_point)
1532 == PT - 1)
1533 && !windows_or_buffers_changed
1534 && EQ (current_buffer->selective_display, Qnil)
1535 && !detect_input_pending ()
1536 && NILP (XWINDOW (selected_window)->column_number_displayed)
1537 && NILP (Vexecuting_macro))
1538 direct_output_forward_char (1);
1539 goto directly_done;
1540 }
1541 else if (EQ (Vthis_command, Qbackward_char) && PT > BEGV)
1542 {
1543 struct Lisp_Char_Table *dp
1544 = window_display_table (XWINDOW (selected_window));
1545 SET_PT (PT - 1);
1546 lose = FETCH_CHAR (PT_BYTE);
1547 if ((dp
1548 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1549 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1550 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1551 && (lose >= 0x20 && lose < 0x7f)))
1552 : (lose >= 0x20 && lose < 0x7f))
1553 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1554 >= MODIFF)
1555 && (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1556 >= OVERLAY_MODIFF)
1557 && (XFASTINT (XWINDOW (selected_window)->last_point)
1558 == PT + 1)
1559 && !windows_or_buffers_changed
1560 && EQ (current_buffer->selective_display, Qnil)
1561 && !detect_input_pending ()
1562 && NILP (XWINDOW (selected_window)->column_number_displayed)
1563 && NILP (Vexecuting_macro))
1564 direct_output_forward_char (-1);
1565 goto directly_done;
1566 }
1567 else if (EQ (Vthis_command, Qself_insert_command)
1568 /* Try this optimization only on ascii keystrokes. */
1569 && INTEGERP (last_command_char))
1570 {
1571 unsigned int c = XINT (last_command_char);
1572 int value;
1573 if (NILP (Vexecuting_macro)
1574 && !EQ (minibuf_window, selected_window))
1575 {
1576 if (!nonundocount || nonundocount >= 20)
1577 {
1578 Fundo_boundary ();
1579 nonundocount = 0;
1580 }
1581 nonundocount++;
1582 }
1583
1584 lose = ((XFASTINT (XWINDOW (selected_window)->last_modified)
1585 < MODIFF)
1586 || (XFASTINT (XWINDOW (selected_window)->last_overlay_modified)
1587 < OVERLAY_MODIFF)
1588 || (XFASTINT (XWINDOW (selected_window)->last_point)
1589 != PT)
1590 || MODIFF <= SAVE_MODIFF
1591 || windows_or_buffers_changed
1592 || !EQ (current_buffer->selective_display, Qnil)
1593 || detect_input_pending ()
1594 || !NILP (XWINDOW (selected_window)->column_number_displayed)
1595 || !NILP (Vexecuting_macro));
1596
1597 value = internal_self_insert (c, 0);
1598
1599 if (value == 2)
1600 nonundocount = 0;
1601
1602 /* VALUE == 1 when AFTER-CHANGE functions are
1603 installed which is the case most of the time
1604 because FONT-LOCK installs one. */
1605 if (!lose && !value)
1606 direct_output_for_insert (c);
1607 goto directly_done;
1608 }
1609 }
1610
1611 /* Here for a command that isn't executed directly */
1612
1613 #ifdef HAVE_X_WINDOWS
1614 if (display_hourglass_p)
1615 start_hourglass ();
1616 #endif
1617
1618 nonundocount = 0;
1619 if (NILP (current_kboard->Vprefix_arg))
1620 Fundo_boundary ();
1621 Fcommand_execute (Vthis_command, Qnil, Qnil, Qnil);
1622
1623 #ifdef HAVE_X_WINDOWS
1624 if (display_hourglass_p)
1625 cancel_hourglass ();
1626 #endif
1627 }
1628 directly_done: ;
1629 current_kboard->Vlast_prefix_arg = Vcurrent_prefix_arg;
1630
1631 /* Note that the value cell will never directly contain nil
1632 if the symbol is a local variable. */
1633 if (!NILP (Vpost_command_hook) && !NILP (Vrun_hooks))
1634 safe_run_hooks (Qpost_command_hook);
1635
1636 /* If displaying a message, resize the echo area window to fit
1637 that message's size exactly. */
1638 if (!NILP (echo_area_buffer[0]))
1639 resize_echo_area_axactly ();
1640
1641 if (!NILP (Vdeferred_action_list))
1642 safe_run_hooks (Qdeferred_action_function);
1643
1644 if (!NILP (Vpost_command_idle_hook) && !NILP (Vrun_hooks))
1645 {
1646 if (NILP (Vunread_command_events)
1647 && NILP (Vunread_input_method_events)
1648 && NILP (Vunread_post_input_method_events)
1649 && NILP (Vexecuting_macro)
1650 && !NILP (sit_for (0, post_command_idle_delay, 0, 1, 1)))
1651 safe_run_hooks (Qpost_command_idle_hook);
1652 }
1653
1654 /* If there is a prefix argument,
1655 1) We don't want Vlast_command to be ``universal-argument''
1656 (that would be dumb), so don't set Vlast_command,
1657 2) we want to leave echoing on so that the prefix will be
1658 echoed as part of this key sequence, so don't call
1659 cancel_echoing, and
1660 3) we want to leave this_command_key_count non-zero, so that
1661 read_char will realize that it is re-reading a character, and
1662 not echo it a second time.
1663
1664 If the command didn't actually create a prefix arg,
1665 but is merely a frame event that is transparent to prefix args,
1666 then the above doesn't apply. */
1667 if (NILP (current_kboard->Vprefix_arg) || CONSP (last_command_char))
1668 {
1669 current_kboard->Vlast_command = Vthis_command;
1670 current_kboard->Vreal_last_command = real_this_command;
1671 cancel_echoing ();
1672 this_command_key_count = 0;
1673 this_single_command_key_start = 0;
1674 }
1675
1676 if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
1677 {
1678 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1679 {
1680 current_buffer->mark_active = Qnil;
1681 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1682 }
1683 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1684 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1685 }
1686
1687 finalize:
1688
1689 if (current_buffer == prev_buffer
1690 && last_point_position != PT
1691 && NILP (Vdisable_point_adjustment)
1692 && NILP (Vglobal_disable_point_adjustment))
1693 adjust_point_for_property (last_point_position);
1694
1695 /* Install chars successfully executed in kbd macro. */
1696
1697 if (!NILP (current_kboard->defining_kbd_macro)
1698 && NILP (current_kboard->Vprefix_arg))
1699 finalize_kbd_macro_chars ();
1700
1701 #ifdef MULTI_KBOARD
1702 if (!was_locked)
1703 any_kboard_state ();
1704 #endif
1705 }
1706 }
1707
1708 extern Lisp_Object Qcomposition, Qdisplay;
1709
1710 /* Adjust point to a boundary of a region that has such a property
1711 that should be treated intangible. For the moment, we check
1712 `composition' and `display' property. LAST_PT is the last position
1713 of point. */
1714
1715 static void
1716 adjust_point_for_property (last_pt)
1717 int last_pt;
1718 {
1719 int start, end;
1720 Lisp_Object val;
1721 int check_composition = 1, check_display = 1;
1722
1723 while (check_composition || check_display)
1724 {
1725 if (check_composition
1726 && PT > BEGV && PT < ZV
1727 && get_property_and_range (PT, Qcomposition, &val, &start, &end, Qnil)
1728 && COMPOSITION_VALID_P (start, end, val)
1729 && start < PT && end > PT
1730 && (last_pt <= start || last_pt >= end))
1731 {
1732 if (PT < last_pt)
1733 SET_PT (start);
1734 else
1735 SET_PT (end);
1736 check_display = 1;
1737 }
1738 check_composition = 0;
1739 if (check_display
1740 && PT > BEGV && PT < ZV
1741 && get_property_and_range (PT, Qdisplay, &val, &start, &end, Qnil)
1742 && display_prop_intangible_p (val)
1743 && start < PT && end > PT
1744 && (last_pt <= start || last_pt >= end))
1745 {
1746 if (PT < last_pt)
1747 SET_PT (start);
1748 else
1749 SET_PT (end);
1750 check_composition = 1;
1751 }
1752 check_display = 0;
1753 }
1754 }
1755
1756 /* Subroutine for safe_run_hooks: run the hook HOOK. */
1757
1758 static Lisp_Object
1759 safe_run_hooks_1 (hook)
1760 Lisp_Object hook;
1761 {
1762 return call1 (Vrun_hooks, Vinhibit_quit);
1763 }
1764
1765 /* Subroutine for safe_run_hooks: handle an error by clearing out the hook. */
1766
1767 static Lisp_Object
1768 safe_run_hooks_error (data)
1769 Lisp_Object data;
1770 {
1771 return Fset (Vinhibit_quit, Qnil);
1772 }
1773
1774 /* If we get an error while running the hook, cause the hook variable
1775 to be nil. Also inhibit quits, so that C-g won't cause the hook
1776 to mysteriously evaporate. */
1777
1778 void
1779 safe_run_hooks (hook)
1780 Lisp_Object hook;
1781 {
1782 int count = specpdl_ptr - specpdl;
1783 specbind (Qinhibit_quit, hook);
1784
1785 internal_condition_case (safe_run_hooks_1, Qt, safe_run_hooks_error);
1786
1787 unbind_to (count, Qnil);
1788 }
1789
1790 \f
1791 /* Number of seconds between polling for input. This is a Lisp
1792 variable that can be bound. */
1793
1794 int polling_period;
1795
1796 /* Nonzero means polling for input is temporarily suppressed. */
1797
1798 int poll_suppress_count;
1799
1800 /* Asynchronous timer for polling. */
1801
1802 struct atimer *poll_timer;
1803
1804
1805 #ifdef POLL_FOR_INPUT
1806
1807 /* Poll for input, so what we catch a C-g if it comes in. This
1808 function is called from x_make_frame_visible, see comment
1809 there. */
1810
1811 void
1812 poll_for_input_1 ()
1813 {
1814 if (interrupt_input_blocked == 0
1815 && !waiting_for_input)
1816 read_avail_input (0);
1817 }
1818
1819 /* Timer callback function for poll_timer. TIMER is equal to
1820 poll_timer. */
1821
1822 void
1823 poll_for_input (timer)
1824 struct atimer *timer;
1825 {
1826 if (poll_suppress_count == 0)
1827 poll_for_input_1 ();
1828 }
1829
1830 #endif /* POLL_FOR_INPUT */
1831
1832 /* Begin signals to poll for input, if they are appropriate.
1833 This function is called unconditionally from various places. */
1834
1835 void
1836 start_polling ()
1837 {
1838 #ifdef POLL_FOR_INPUT
1839 if (read_socket_hook && !interrupt_input)
1840 {
1841 /* Turn alarm handling on unconditionally. It might have
1842 been turned off in process.c. */
1843 turn_on_atimers (1);
1844
1845 /* If poll timer doesn't exist, are we need one with
1846 a different interval, start a new one. */
1847 if (poll_timer == NULL
1848 || EMACS_SECS (poll_timer->interval) != polling_period)
1849 {
1850 EMACS_TIME interval;
1851
1852 if (poll_timer)
1853 cancel_atimer (poll_timer);
1854
1855 EMACS_SET_SECS_USECS (interval, polling_period, 0);
1856 poll_timer = start_atimer (ATIMER_CONTINUOUS, interval,
1857 poll_for_input, NULL);
1858 }
1859
1860 /* Let the timer's callback function poll for input
1861 if this becomes zero. */
1862 --poll_suppress_count;
1863 }
1864 #endif
1865 }
1866
1867 /* Nonzero if we are using polling to handle input asynchronously. */
1868
1869 int
1870 input_polling_used ()
1871 {
1872 #ifdef POLL_FOR_INPUT
1873 return read_socket_hook && !interrupt_input;
1874 #else
1875 return 0;
1876 #endif
1877 }
1878
1879 /* Turn off polling. */
1880
1881 void
1882 stop_polling ()
1883 {
1884 #ifdef POLL_FOR_INPUT
1885 if (read_socket_hook && !interrupt_input)
1886 ++poll_suppress_count;
1887 #endif
1888 }
1889
1890 /* Set the value of poll_suppress_count to COUNT
1891 and start or stop polling accordingly. */
1892
1893 void
1894 set_poll_suppress_count (count)
1895 int count;
1896 {
1897 #ifdef POLL_FOR_INPUT
1898 if (count == 0 && poll_suppress_count != 0)
1899 {
1900 poll_suppress_count = 1;
1901 start_polling ();
1902 }
1903 else if (count != 0 && poll_suppress_count == 0)
1904 {
1905 stop_polling ();
1906 }
1907 poll_suppress_count = count;
1908 #endif
1909 }
1910
1911 /* Bind polling_period to a value at least N.
1912 But don't decrease it. */
1913
1914 void
1915 bind_polling_period (n)
1916 int n;
1917 {
1918 #ifdef POLL_FOR_INPUT
1919 int new = polling_period;
1920
1921 if (n > new)
1922 new = n;
1923
1924 stop_other_atimers (poll_timer);
1925 stop_polling ();
1926 specbind (Qpolling_period, make_number (new));
1927 /* Start a new alarm with the new period. */
1928 start_polling ();
1929 #endif
1930 }
1931 \f
1932 /* Apply the control modifier to CHARACTER. */
1933
1934 int
1935 make_ctrl_char (c)
1936 int c;
1937 {
1938 /* Save the upper bits here. */
1939 int upper = c & ~0177;
1940
1941 c &= 0177;
1942
1943 /* Everything in the columns containing the upper-case letters
1944 denotes a control character. */
1945 if (c >= 0100 && c < 0140)
1946 {
1947 int oc = c;
1948 c &= ~0140;
1949 /* Set the shift modifier for a control char
1950 made from a shifted letter. But only for letters! */
1951 if (oc >= 'A' && oc <= 'Z')
1952 c |= shift_modifier;
1953 }
1954
1955 /* The lower-case letters denote control characters too. */
1956 else if (c >= 'a' && c <= 'z')
1957 c &= ~0140;
1958
1959 /* Include the bits for control and shift
1960 only if the basic ASCII code can't indicate them. */
1961 else if (c >= ' ')
1962 c |= ctrl_modifier;
1963
1964 /* Replace the high bits. */
1965 c |= (upper & ~ctrl_modifier);
1966
1967 return c;
1968 }
1969
1970 /* Display help echo in the echo area.
1971
1972 HELP a string means display that string, HELP nil means clear the
1973 help echo. If HELP is a function, call it with OBJECT and POS as
1974 arguments; the function should return a help string or nil for
1975 none. For all other types of HELP evaluate it to obtain a string.
1976
1977 WINDOW is the window in which the help was generated, if any.
1978 It is nil if not in a window.
1979
1980 If OBJECT is a buffer, POS is the position in the buffer where the
1981 `help-echo' text property was found.
1982
1983 If OBJECT is an overlay, that overlay has a `help-echo' property,
1984 and POS is the position in the overlay's buffer under the mouse.
1985
1986 If OBJECT is a string (an overlay string or a string displayed with
1987 the `display' property). POS is the position in that string under
1988 the mouse.
1989
1990 OK_TO_IVERWRITE_KEYSTROKE_ECHO non-zero means it's okay if the help
1991 echo overwrites a keystroke echo currently displayed in the echo
1992 area.
1993
1994 Note: this function may only be called with HELP nil or a string
1995 from X code running asynchronously. */
1996
1997 void
1998 show_help_echo (help, window, object, pos, ok_to_overwrite_keystroke_echo)
1999 Lisp_Object help, window, object, pos;
2000 int ok_to_overwrite_keystroke_echo;
2001 {
2002 if (!NILP (help) && !STRINGP (help))
2003 {
2004 if (FUNCTIONP (help))
2005 {
2006 Lisp_Object args[4];
2007 args[0] = help;
2008 args[1] = window;
2009 args[2] = object;
2010 args[3] = pos;
2011 help = safe_call (4, args);
2012 }
2013 else
2014 help = safe_eval (help);
2015
2016 if (!STRINGP (help))
2017 return;
2018 }
2019
2020 if (STRINGP (help) || NILP (help))
2021 {
2022 if (!NILP (Vshow_help_function))
2023 call1 (Vshow_help_function, help);
2024 else if (/* Don't overwrite minibuffer contents. */
2025 !MINI_WINDOW_P (XWINDOW (selected_window))
2026 /* Don't overwrite a keystroke echo. */
2027 && (NILP (echo_message_buffer)
2028 || ok_to_overwrite_keystroke_echo)
2029 /* Don't overwrite a prompt. */
2030 && !cursor_in_echo_area)
2031 {
2032 if (STRINGP (help))
2033 {
2034 int count = specpdl_ptr - specpdl;
2035 specbind (Qmessage_truncate_lines, Qt);
2036 message3_nolog (help, STRING_BYTES (XSTRING (help)),
2037 STRING_MULTIBYTE (help));
2038 unbind_to (count, Qnil);
2039 }
2040 else
2041 message (0);
2042 }
2043
2044 help_echo_showing_p = STRINGP (help);
2045 }
2046 }
2047
2048
2049 \f
2050 /* Input of single characters from keyboard */
2051
2052 Lisp_Object print_help ();
2053 static Lisp_Object kbd_buffer_get_event ();
2054 static void record_char ();
2055
2056 #ifdef MULTI_KBOARD
2057 static jmp_buf wrong_kboard_jmpbuf;
2058 #endif
2059
2060 /* read a character from the keyboard; call the redisplay if needed */
2061 /* commandflag 0 means do not do auto-saving, but do do redisplay.
2062 -1 means do not do redisplay, but do do autosaving.
2063 1 means do both. */
2064
2065 /* The arguments MAPS and NMAPS are for menu prompting.
2066 MAPS is an array of keymaps; NMAPS is the length of MAPS.
2067
2068 PREV_EVENT is the previous input event, or nil if we are reading
2069 the first event of a key sequence (or not reading a key sequence).
2070 If PREV_EVENT is t, that is a "magic" value that says
2071 not to run input methods, but in other respects to act as if
2072 not reading a key sequence.
2073
2074 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
2075 if we used a mouse menu to read the input, or zero otherwise. If
2076 USED_MOUSE_MENU is null, we don't dereference it.
2077
2078 Value is t if we showed a menu and the user rejected it. */
2079
2080 Lisp_Object
2081 read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
2082 int commandflag;
2083 int nmaps;
2084 Lisp_Object *maps;
2085 Lisp_Object prev_event;
2086 int *used_mouse_menu;
2087 {
2088 volatile Lisp_Object c;
2089 int count;
2090 jmp_buf local_getcjmp;
2091 jmp_buf save_jump;
2092 volatile int key_already_recorded = 0;
2093 Lisp_Object tem, save;
2094 volatile Lisp_Object previous_echo_area_message;
2095 volatile Lisp_Object also_record;
2096 volatile int reread;
2097 struct gcpro gcpro1, gcpro2;
2098
2099 also_record = Qnil;
2100
2101 before_command_key_count = this_command_key_count;
2102 before_command_echo_length = echo_length ();
2103 c = Qnil;
2104 previous_echo_area_message = Qnil;
2105
2106 GCPRO2 (c, previous_echo_area_message);
2107
2108 retry:
2109
2110 reread = 0;
2111 if (CONSP (Vunread_post_input_method_events))
2112 {
2113 c = XCAR (Vunread_post_input_method_events);
2114 Vunread_post_input_method_events
2115 = XCDR (Vunread_post_input_method_events);
2116
2117 /* Undo what read_char_x_menu_prompt did when it unread
2118 additional keys returned by Fx_popup_menu. */
2119 if (CONSP (c)
2120 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
2121 && NILP (XCDR (c)))
2122 c = XCAR (c);
2123
2124 reread = 1;
2125 goto reread_first;
2126 }
2127
2128 if (unread_command_char != -1)
2129 {
2130 XSETINT (c, unread_command_char);
2131 unread_command_char = -1;
2132
2133 reread = 1;
2134 goto reread_first;
2135 }
2136
2137 if (CONSP (Vunread_command_events))
2138 {
2139 c = XCAR (Vunread_command_events);
2140 Vunread_command_events = XCDR (Vunread_command_events);
2141
2142 /* Undo what read_char_x_menu_prompt did when it unread
2143 additional keys returned by Fx_popup_menu. */
2144 if (CONSP (c)
2145 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
2146 && NILP (XCDR (c)))
2147 c = XCAR (c);
2148
2149 /* If the queued event is something that used the mouse,
2150 set used_mouse_menu accordingly. */
2151 if (used_mouse_menu
2152 && (EQ (c, Qtool_bar) || EQ (c, Qmenu_bar)))
2153 *used_mouse_menu = 1;
2154
2155 reread = 1;
2156 goto reread_for_input_method;
2157 }
2158
2159 if (CONSP (Vunread_input_method_events))
2160 {
2161 c = XCAR (Vunread_input_method_events);
2162 Vunread_input_method_events = XCDR (Vunread_input_method_events);
2163
2164 /* Undo what read_char_x_menu_prompt did when it unread
2165 additional keys returned by Fx_popup_menu. */
2166 if (CONSP (c)
2167 && (SYMBOLP (XCAR (c)) || INTEGERP (XCAR (c)))
2168 && NILP (XCDR (c)))
2169 c = XCAR (c);
2170 reread = 1;
2171 goto reread_for_input_method;
2172 }
2173
2174 /* If there is no function key translated before
2175 reset-this-command-lengths takes effect, forget about it. */
2176 before_command_restore_flag = 0;
2177
2178 if (!NILP (Vexecuting_macro))
2179 {
2180 /* We set this to Qmacro; since that's not a frame, nobody will
2181 try to switch frames on us, and the selected window will
2182 remain unchanged.
2183
2184 Since this event came from a macro, it would be misleading to
2185 leave internal_last_event_frame set to wherever the last
2186 real event came from. Normally, a switch-frame event selects
2187 internal_last_event_frame after each command is read, but
2188 events read from a macro should never cause a new frame to be
2189 selected. */
2190 Vlast_event_frame = internal_last_event_frame = Qmacro;
2191
2192 /* Exit the macro if we are at the end.
2193 Also, some things replace the macro with t
2194 to force an early exit. */
2195 if (EQ (Vexecuting_macro, Qt)
2196 || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
2197 {
2198 XSETINT (c, -1);
2199 RETURN_UNGCPRO (c);
2200 }
2201
2202 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
2203 if (STRINGP (Vexecuting_macro)
2204 && (XINT (c) & 0x80))
2205 XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
2206
2207 executing_macro_index++;
2208
2209 goto from_macro;
2210 }
2211
2212 if (!NILP (unread_switch_frame))
2213 {
2214 c = unread_switch_frame;
2215 unread_switch_frame = Qnil;
2216
2217 /* This event should make it into this_command_keys, and get echoed
2218 again, so we do not set `reread'. */
2219 goto reread_first;
2220 }
2221
2222 /* if redisplay was requested */
2223 if (commandflag >= 0)
2224 {
2225 /* If there is pending input, process any events which are not
2226 user-visible, such as X selection_request events. */
2227 if (input_pending
2228 || detect_input_pending_run_timers (0))
2229 swallow_events (0); /* may clear input_pending */
2230
2231 /* Redisplay if no pending input. */
2232 while (!input_pending)
2233 {
2234 if (help_echo_showing_p && !EQ (selected_window, minibuf_window))
2235 redisplay_preserve_echo_area (5);
2236 else
2237 redisplay ();
2238
2239 if (!input_pending)
2240 /* Normal case: no input arrived during redisplay. */
2241 break;
2242
2243 /* Input arrived and pre-empted redisplay.
2244 Process any events which are not user-visible. */
2245 swallow_events (0);
2246 /* If that cleared input_pending, try again to redisplay. */
2247 }
2248 }
2249
2250 /* Message turns off echoing unless more keystrokes turn it on again.
2251
2252 The code in 20.x for the condition was
2253
2254 1. echo_area_glyphs && *echo_area_glyphs
2255 2. && echo_area_glyphs != current_kboard->echobuf
2256 3. && ok_to_echo_at_next_pause != echo_area_glyphs
2257
2258 (1) means there's a current message displayed
2259
2260 (2) means it's not the message from echoing from the current
2261 kboard.
2262
2263 (3) There's only one place in 20.x where ok_to_echo_at_next_pause
2264 is set to a non-null value. This is done in read_char and it is
2265 set to echo_area_glyphs after a call to echo_char. That means
2266 ok_to_echo_at_next_pause is either null or
2267 current_kboard->echobuf with the appropriate current_kboard at
2268 that time.
2269
2270 So, condition (3) means in clear text ok_to_echo_at_next_pause
2271 must be either null, or the current message isn't from echoing at
2272 all, or it's from echoing from a different kboard than the
2273 current one. */
2274
2275 if (/* There currently something in the echo area */
2276 !NILP (echo_area_buffer[0])
2277 && (/* And it's either not from echoing. */
2278 !EQ (echo_area_buffer[0], echo_message_buffer)
2279 /* Or it's an echo from a different kboard. */
2280 || echo_kboard != current_kboard
2281 /* Or we explicitly allow overwriting whatever there is. */
2282 || ok_to_echo_at_next_pause == NULL))
2283 cancel_echoing ();
2284 else
2285 echo_dash ();
2286
2287 /* Try reading a character via menu prompting in the minibuf.
2288 Try this before the sit-for, because the sit-for
2289 would do the wrong thing if we are supposed to do
2290 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
2291 after a mouse event so don't try a minibuf menu. */
2292 c = Qnil;
2293 if (nmaps > 0 && INTERACTIVE
2294 && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
2295 /* Don't bring up a menu if we already have another event. */
2296 && NILP (Vunread_command_events)
2297 && unread_command_char < 0
2298 && !detect_input_pending_run_timers (0))
2299 {
2300 c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
2301 if (! NILP (c))
2302 {
2303 key_already_recorded = 1;
2304 goto non_reread_1;
2305 }
2306 }
2307
2308 /* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
2309 We will do that below, temporarily for short sections of code,
2310 when appropriate. local_getcjmp must be in effect
2311 around any call to sit_for or kbd_buffer_get_event;
2312 it *must not* be in effect when we call redisplay. */
2313
2314 if (_setjmp (local_getcjmp))
2315 {
2316 XSETINT (c, quit_char);
2317 internal_last_event_frame = selected_frame;
2318 Vlast_event_frame = internal_last_event_frame;
2319 /* If we report the quit char as an event,
2320 don't do so more than once. */
2321 if (!NILP (Vinhibit_quit))
2322 Vquit_flag = Qnil;
2323
2324 #ifdef MULTI_KBOARD
2325 {
2326 KBOARD *kb = FRAME_KBOARD (XFRAME (selected_frame));
2327 if (kb != current_kboard)
2328 {
2329 Lisp_Object *tailp = &kb->kbd_queue;
2330 /* We shouldn't get here if we were in single-kboard mode! */
2331 if (single_kboard)
2332 abort ();
2333 while (CONSP (*tailp))
2334 tailp = &XCDR (*tailp);
2335 if (!NILP (*tailp))
2336 abort ();
2337 *tailp = Fcons (c, Qnil);
2338 kb->kbd_queue_has_data = 1;
2339 current_kboard = kb;
2340 /* This is going to exit from read_char
2341 so we had better get rid of this frame's stuff. */
2342 UNGCPRO;
2343 longjmp (wrong_kboard_jmpbuf, 1);
2344 }
2345 }
2346 #endif
2347 goto non_reread;
2348 }
2349
2350 timer_start_idle ();
2351
2352 /* If in middle of key sequence and minibuffer not active,
2353 start echoing if enough time elapses. */
2354
2355 if (minibuf_level == 0
2356 && !current_kboard->immediate_echo
2357 && this_command_key_count > 0
2358 && ! noninteractive
2359 && (FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
2360 && NILP (Fzerop (Vecho_keystrokes))
2361 && (/* No message. */
2362 NILP (echo_area_buffer[0])
2363 /* Or empty message. */
2364 || (BUF_BEG (XBUFFER (echo_area_buffer[0]))
2365 == BUF_Z (XBUFFER (echo_area_buffer[0])))
2366 /* Or already echoing from same kboard. */
2367 || (echo_kboard && ok_to_echo_at_next_pause == echo_kboard)
2368 /* Or not echoing before and echoing allowed. */
2369 || (!echo_kboard && ok_to_echo_at_next_pause)))
2370 {
2371 Lisp_Object tem0;
2372
2373 /* After a mouse event, start echoing right away.
2374 This is because we are probably about to display a menu,
2375 and we don't want to delay before doing so. */
2376 if (EVENT_HAS_PARAMETERS (prev_event))
2377 echo_now ();
2378 else
2379 {
2380 int sec, usec;
2381 double duration = extract_float (Vecho_keystrokes);
2382 sec = (int) duration;
2383 usec = (duration - sec) * 1000000;
2384 save_getcjmp (save_jump);
2385 restore_getcjmp (local_getcjmp);
2386 tem0 = sit_for (sec, usec, 1, 1, 0);
2387 restore_getcjmp (save_jump);
2388 if (EQ (tem0, Qt)
2389 && ! CONSP (Vunread_command_events))
2390 echo_now ();
2391 }
2392 }
2393
2394 /* Maybe auto save due to number of keystrokes. */
2395
2396 if (commandflag != 0
2397 && auto_save_interval > 0
2398 && num_nonmacro_input_events - last_auto_save > max (auto_save_interval, 20)
2399 && !detect_input_pending_run_timers (0))
2400 {
2401 Fdo_auto_save (Qnil, Qnil);
2402 /* Hooks can actually change some buffers in auto save. */
2403 redisplay ();
2404 }
2405
2406 /* Try reading using an X menu.
2407 This is never confused with reading using the minibuf
2408 because the recursive call of read_char in read_char_minibuf_menu_prompt
2409 does not pass on any keymaps. */
2410
2411 if (nmaps > 0 && INTERACTIVE
2412 && !NILP (prev_event)
2413 && EVENT_HAS_PARAMETERS (prev_event)
2414 && !EQ (XCAR (prev_event), Qmenu_bar)
2415 && !EQ (XCAR (prev_event), Qtool_bar)
2416 /* Don't bring up a menu if we already have another event. */
2417 && NILP (Vunread_command_events)
2418 && unread_command_char < 0)
2419 {
2420 c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
2421
2422 /* Now that we have read an event, Emacs is not idle. */
2423 timer_stop_idle ();
2424
2425 RETURN_UNGCPRO (c);
2426 }
2427
2428 /* Maybe autosave and/or garbage collect due to idleness. */
2429
2430 if (INTERACTIVE && NILP (c))
2431 {
2432 int delay_level, buffer_size;
2433
2434 /* Slow down auto saves logarithmically in size of current buffer,
2435 and garbage collect while we're at it. */
2436 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
2437 last_non_minibuf_size = Z - BEG;
2438 buffer_size = (last_non_minibuf_size >> 8) + 1;
2439 delay_level = 0;
2440 while (buffer_size > 64)
2441 delay_level++, buffer_size -= buffer_size >> 2;
2442 if (delay_level < 4) delay_level = 4;
2443 /* delay_level is 4 for files under around 50k, 7 at 100k,
2444 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
2445
2446 /* Auto save if enough time goes by without input. */
2447 if (commandflag != 0
2448 && num_nonmacro_input_events > last_auto_save
2449 && INTEGERP (Vauto_save_timeout)
2450 && XINT (Vauto_save_timeout) > 0)
2451 {
2452 Lisp_Object tem0;
2453
2454 save_getcjmp (save_jump);
2455 restore_getcjmp (local_getcjmp);
2456 tem0 = sit_for (delay_level * XFASTINT (Vauto_save_timeout) / 4,
2457 0, 1, 1, 0);
2458 restore_getcjmp (save_jump);
2459
2460 if (EQ (tem0, Qt)
2461 && ! CONSP (Vunread_command_events))
2462 {
2463 Fdo_auto_save (Qnil, Qnil);
2464
2465 /* If we have auto-saved and there is still no input
2466 available, garbage collect if there has been enough
2467 consing going on to make it worthwhile. */
2468 if (!detect_input_pending_run_timers (0)
2469 && consing_since_gc > gc_cons_threshold / 2)
2470 Fgarbage_collect ();
2471
2472 redisplay ();
2473 }
2474 }
2475 }
2476
2477 /* If this has become non-nil here, it has been set by a timer
2478 or sentinel or filter. */
2479 if (CONSP (Vunread_command_events))
2480 {
2481 c = XCAR (Vunread_command_events);
2482 Vunread_command_events = XCDR (Vunread_command_events);
2483 }
2484
2485 /* Read something from current KBOARD's side queue, if possible. */
2486
2487 if (NILP (c))
2488 {
2489 if (current_kboard->kbd_queue_has_data)
2490 {
2491 if (!CONSP (current_kboard->kbd_queue))
2492 abort ();
2493 c = XCAR (current_kboard->kbd_queue);
2494 current_kboard->kbd_queue
2495 = XCDR (current_kboard->kbd_queue);
2496 if (NILP (current_kboard->kbd_queue))
2497 current_kboard->kbd_queue_has_data = 0;
2498 input_pending = readable_events (0);
2499 if (EVENT_HAS_PARAMETERS (c)
2500 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qswitch_frame))
2501 internal_last_event_frame = XCAR (XCDR (c));
2502 Vlast_event_frame = internal_last_event_frame;
2503 }
2504 }
2505
2506 #ifdef MULTI_KBOARD
2507 /* If current_kboard's side queue is empty check the other kboards.
2508 If one of them has data that we have not yet seen here,
2509 switch to it and process the data waiting for it.
2510
2511 Note: if the events queued up for another kboard
2512 have already been seen here, and therefore are not a complete command,
2513 the kbd_queue_has_data field is 0, so we skip that kboard here.
2514 That's to avoid an infinite loop switching between kboards here. */
2515 if (NILP (c) && !single_kboard)
2516 {
2517 KBOARD *kb;
2518 for (kb = all_kboards; kb; kb = kb->next_kboard)
2519 if (kb->kbd_queue_has_data)
2520 {
2521 current_kboard = kb;
2522 /* This is going to exit from read_char
2523 so we had better get rid of this frame's stuff. */
2524 UNGCPRO;
2525 longjmp (wrong_kboard_jmpbuf, 1);
2526 }
2527 }
2528 #endif
2529
2530 wrong_kboard:
2531
2532 stop_polling ();
2533
2534 /* Finally, we read from the main queue,
2535 and if that gives us something we can't use yet, we put it on the
2536 appropriate side queue and try again. */
2537
2538 if (NILP (c))
2539 {
2540 KBOARD *kb;
2541
2542 /* Actually read a character, waiting if necessary. */
2543 save_getcjmp (save_jump);
2544 restore_getcjmp (local_getcjmp);
2545 timer_start_idle ();
2546 c = kbd_buffer_get_event (&kb, used_mouse_menu);
2547 restore_getcjmp (save_jump);
2548
2549 #ifdef MULTI_KBOARD
2550 if (! NILP (c) && (kb != current_kboard))
2551 {
2552 Lisp_Object *tailp = &kb->kbd_queue;
2553 while (CONSP (*tailp))
2554 tailp = &XCDR (*tailp);
2555 if (!NILP (*tailp))
2556 abort ();
2557 *tailp = Fcons (c, Qnil);
2558 kb->kbd_queue_has_data = 1;
2559 c = Qnil;
2560 if (single_kboard)
2561 goto wrong_kboard;
2562 current_kboard = kb;
2563 /* This is going to exit from read_char
2564 so we had better get rid of this frame's stuff. */
2565 UNGCPRO;
2566 longjmp (wrong_kboard_jmpbuf, 1);
2567 }
2568 #endif
2569 }
2570
2571 /* Terminate Emacs in batch mode if at eof. */
2572 if (noninteractive && INTEGERP (c) && XINT (c) < 0)
2573 Fkill_emacs (make_number (1));
2574
2575 if (INTEGERP (c))
2576 {
2577 /* Add in any extra modifiers, where appropriate. */
2578 if ((extra_keyboard_modifiers & CHAR_CTL)
2579 || ((extra_keyboard_modifiers & 0177) < ' '
2580 && (extra_keyboard_modifiers & 0177) != 0))
2581 XSETINT (c, make_ctrl_char (XINT (c)));
2582
2583 /* Transfer any other modifier bits directly from
2584 extra_keyboard_modifiers to c. Ignore the actual character code
2585 in the low 16 bits of extra_keyboard_modifiers. */
2586 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
2587 }
2588
2589 non_reread:
2590
2591 timer_stop_idle ();
2592 start_polling ();
2593
2594 if (NILP (c))
2595 {
2596 if (commandflag >= 0
2597 && !input_pending && !detect_input_pending_run_timers (0))
2598 redisplay ();
2599
2600 goto wrong_kboard;
2601 }
2602
2603 non_reread_1:
2604
2605 /* Buffer switch events are only for internal wakeups
2606 so don't show them to the user.
2607 Also, don't record a key if we already did. */
2608 if (BUFFERP (c) || key_already_recorded)
2609 RETURN_UNGCPRO (c);
2610
2611 /* Process special events within read_char
2612 and loop around to read another event. */
2613 save = Vquit_flag;
2614 Vquit_flag = Qnil;
2615 tem = access_keymap (get_keymap (Vspecial_event_map, 0, 1), c, 0, 0, 1);
2616 Vquit_flag = save;
2617
2618 if (!NILP (tem))
2619 {
2620 int was_locked = single_kboard;
2621
2622 last_input_char = c;
2623 Fcommand_execute (tem, Qnil, Fvector (1, &last_input_char), Qt);
2624
2625 /* Resume allowing input from any kboard, if that was true before. */
2626 if (!was_locked)
2627 any_kboard_state ();
2628
2629 goto retry;
2630 }
2631
2632 /* Handle things that only apply to characters. */
2633 if (INTEGERP (c))
2634 {
2635 /* If kbd_buffer_get_event gave us an EOF, return that. */
2636 if (XINT (c) == -1)
2637 RETURN_UNGCPRO (c);
2638
2639 if ((STRINGP (Vkeyboard_translate_table)
2640 && XSTRING (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2641 || (VECTORP (Vkeyboard_translate_table)
2642 && XVECTOR (Vkeyboard_translate_table)->size > (unsigned) XFASTINT (c))
2643 || (CHAR_TABLE_P (Vkeyboard_translate_table)
2644 && CHAR_TABLE_ORDINARY_SLOTS > (unsigned) XFASTINT (c)))
2645 {
2646 Lisp_Object d;
2647 d = Faref (Vkeyboard_translate_table, c);
2648 /* nil in keyboard-translate-table means no translation. */
2649 if (!NILP (d))
2650 c = d;
2651 }
2652 }
2653
2654 /* If this event is a mouse click in the menu bar,
2655 return just menu-bar for now. Modify the mouse click event
2656 so we won't do this twice, then queue it up. */
2657 if (EVENT_HAS_PARAMETERS (c)
2658 && CONSP (XCDR (c))
2659 && CONSP (EVENT_START (c))
2660 && CONSP (XCDR (EVENT_START (c))))
2661 {
2662 Lisp_Object posn;
2663
2664 posn = POSN_BUFFER_POSN (EVENT_START (c));
2665 /* Handle menu-bar events:
2666 insert the dummy prefix event `menu-bar'. */
2667 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
2668 {
2669 /* Change menu-bar to (menu-bar) as the event "position". */
2670 POSN_BUFFER_POSN (EVENT_START (c)) = Fcons (posn, Qnil);
2671
2672 also_record = c;
2673 Vunread_command_events = Fcons (c, Vunread_command_events);
2674 c = posn;
2675 }
2676 }
2677
2678 /* Store these characters into recent_keys, the dribble file if any,
2679 and the keyboard macro being defined, if any. */
2680 record_char (c);
2681 if (! NILP (also_record))
2682 record_char (also_record);
2683
2684 /* Wipe the echo area.
2685 But first, if we are about to use an input method,
2686 save the echo area contents for it to refer to. */
2687 if (INTEGERP (c)
2688 && ! NILP (Vinput_method_function)
2689 && (unsigned) XINT (c) >= ' '
2690 && (unsigned) XINT (c) != 127
2691 && (unsigned) XINT (c) < 256)
2692 {
2693 previous_echo_area_message = Fcurrent_message ();
2694 Vinput_method_previous_message = previous_echo_area_message;
2695 }
2696
2697 /* Now wipe the echo area, except for help events which do their
2698 own stuff with the echo area. */
2699 if (!CONSP (c) || !(EQ (Qhelp_echo, XCAR (c))))
2700 {
2701 if (!NILP (echo_area_buffer[0]))
2702 safe_run_hooks (Qecho_area_clear_hook);
2703 clear_message (1, 0);
2704 }
2705
2706 reread_for_input_method:
2707 from_macro:
2708 /* Pass this to the input method, if appropriate. */
2709 if (INTEGERP (c)
2710 && ! NILP (Vinput_method_function)
2711 /* Don't run the input method within a key sequence,
2712 after the first event of the key sequence. */
2713 && NILP (prev_event)
2714 && (unsigned) XINT (c) >= ' '
2715 && (unsigned) XINT (c) != 127
2716 && (unsigned) XINT (c) < 256)
2717 {
2718 Lisp_Object keys;
2719 int key_count;
2720 struct gcpro gcpro1;
2721 int count = specpdl_ptr - specpdl;
2722
2723 /* Save the echo status. */
2724 int saved_immediate_echo = current_kboard->immediate_echo;
2725 struct kboard *saved_ok_to_echo = ok_to_echo_at_next_pause;
2726 int saved_echo_after_prompt = current_kboard->echo_after_prompt;
2727
2728 if (before_command_restore_flag)
2729 {
2730 this_command_key_count = before_command_key_count_1;
2731 if (this_command_key_count < this_single_command_key_start)
2732 this_single_command_key_start = this_command_key_count;
2733 echo_truncate (before_command_echo_length_1);
2734 before_command_restore_flag = 0;
2735 }
2736
2737 /* Save the this_command_keys status. */
2738 key_count = this_command_key_count;
2739
2740 if (key_count > 0)
2741 keys = Fcopy_sequence (this_command_keys);
2742 else
2743 keys = Qnil;
2744 GCPRO1 (keys);
2745
2746 /* Clear out this_command_keys. */
2747 this_command_key_count = 0;
2748
2749 /* Now wipe the echo area. */
2750 if (!NILP (echo_area_buffer[0]))
2751 safe_run_hooks (Qecho_area_clear_hook);
2752 clear_message (1, 0);
2753 echo_truncate (0);
2754
2755 /* If we are not reading a key sequence,
2756 never use the echo area. */
2757 if (maps == 0)
2758 {
2759 specbind (Qinput_method_use_echo_area, Qt);
2760 }
2761
2762 /* Call the input method. */
2763 tem = call1 (Vinput_method_function, c);
2764
2765 tem = unbind_to (count, tem);
2766
2767 /* Restore the saved echoing state
2768 and this_command_keys state. */
2769 this_command_key_count = key_count;
2770 if (key_count > 0)
2771 this_command_keys = keys;
2772
2773 cancel_echoing ();
2774 ok_to_echo_at_next_pause = saved_ok_to_echo;
2775 current_kboard->echo_after_prompt = saved_echo_after_prompt;
2776 if (saved_immediate_echo)
2777 echo_now ();
2778
2779 UNGCPRO;
2780
2781 /* The input method can return no events. */
2782 if (! CONSP (tem))
2783 {
2784 /* Bring back the previous message, if any. */
2785 if (! NILP (previous_echo_area_message))
2786 message_with_string ("%s", previous_echo_area_message, 0);
2787 goto retry;
2788 }
2789 /* It returned one event or more. */
2790 c = XCAR (tem);
2791 Vunread_post_input_method_events
2792 = nconc2 (XCDR (tem), Vunread_post_input_method_events);
2793 }
2794
2795 reread_first:
2796
2797 /* Display help if not echoing. */
2798 if (CONSP (c) && EQ (XCAR (c), Qhelp_echo))
2799 {
2800 /* (help-echo FRAME HELP WINDOW OBJECT POS). */
2801 Lisp_Object help, object, position, window;
2802 help = Fnth (make_number (2), c);
2803 window = Fnth (make_number (3), c);
2804 object = Fnth (make_number (4), c);
2805 position = Fnth (make_number (5), c);
2806 show_help_echo (help, window, object, position, 0);
2807 goto retry;
2808 }
2809
2810 if (this_command_key_count == 0 || ! reread)
2811 {
2812 before_command_key_count = this_command_key_count;
2813 before_command_echo_length = echo_length ();
2814
2815 /* Don't echo mouse motion events. */
2816 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
2817 && NILP (Fzerop (Vecho_keystrokes))
2818 && ! (EVENT_HAS_PARAMETERS (c)
2819 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
2820 {
2821 echo_char (c);
2822 if (! NILP (also_record))
2823 echo_char (also_record);
2824 /* Once we reread a character, echoing can happen
2825 the next time we pause to read a new one. */
2826 ok_to_echo_at_next_pause = current_kboard;
2827 }
2828
2829 /* Record this character as part of the current key. */
2830 add_command_key (c);
2831 if (! NILP (also_record))
2832 add_command_key (also_record);
2833 }
2834
2835 last_input_char = c;
2836 num_input_events++;
2837
2838 /* Process the help character specially if enabled */
2839 if (!NILP (Vhelp_form) && help_char_p (c))
2840 {
2841 Lisp_Object tem0;
2842 count = specpdl_ptr - specpdl;
2843
2844 record_unwind_protect (Fset_window_configuration,
2845 Fcurrent_window_configuration (Qnil));
2846
2847 tem0 = Feval (Vhelp_form);
2848 if (STRINGP (tem0))
2849 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
2850
2851 cancel_echoing ();
2852 do
2853 c = read_char (0, 0, 0, Qnil, 0);
2854 while (BUFFERP (c));
2855 /* Remove the help from the frame */
2856 unbind_to (count, Qnil);
2857
2858 redisplay ();
2859 if (EQ (c, make_number (040)))
2860 {
2861 cancel_echoing ();
2862 do
2863 c = read_char (0, 0, 0, Qnil, 0);
2864 while (BUFFERP (c));
2865 }
2866 }
2867
2868 RETURN_UNGCPRO (c);
2869 }
2870
2871 /* Record a key that came from a mouse menu.
2872 Record it for echoing, for this-command-keys, and so on. */
2873
2874 static void
2875 record_menu_key (c)
2876 Lisp_Object c;
2877 {
2878 /* Wipe the echo area. */
2879 clear_message (1, 0);
2880
2881 record_char (c);
2882
2883 before_command_key_count = this_command_key_count;
2884 before_command_echo_length = echo_length ();
2885
2886 /* Don't echo mouse motion events. */
2887 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
2888 && NILP (Fzerop (Vecho_keystrokes)))
2889 {
2890 echo_char (c);
2891
2892 /* Once we reread a character, echoing can happen
2893 the next time we pause to read a new one. */
2894 ok_to_echo_at_next_pause = 0;
2895 }
2896
2897 /* Record this character as part of the current key. */
2898 add_command_key (c);
2899
2900 /* Re-reading in the middle of a command */
2901 last_input_char = c;
2902 num_input_events++;
2903 }
2904
2905 /* Return 1 if should recognize C as "the help character". */
2906
2907 int
2908 help_char_p (c)
2909 Lisp_Object c;
2910 {
2911 Lisp_Object tail;
2912
2913 if (EQ (c, Vhelp_char))
2914 return 1;
2915 for (tail = Vhelp_event_list; CONSP (tail); tail = XCDR (tail))
2916 if (EQ (c, XCAR (tail)))
2917 return 1;
2918 return 0;
2919 }
2920
2921 /* Record the input event C in various ways. */
2922
2923 static void
2924 record_char (c)
2925 Lisp_Object c;
2926 {
2927 /* Don't record `help-echo' in recent_keys unless it shows some help
2928 message, and a different help than the previoiusly recorded
2929 event. */
2930 if (CONSP (c) && EQ (XCAR (c), Qhelp_echo))
2931 {
2932 Lisp_Object help;
2933
2934 help = Fnth (make_number (2), c);
2935 if (STRINGP (help))
2936 {
2937 int last_idx;
2938 Lisp_Object last_c, last_help;
2939
2940 last_idx = recent_keys_index - 1;
2941 if (last_idx < 0)
2942 last_idx = NUM_RECENT_KEYS - 1;
2943 last_c = AREF (recent_keys, last_idx);
2944
2945 if (!CONSP (last_c)
2946 || !EQ (XCAR (last_c), Qhelp_echo)
2947 || (last_help = Fnth (make_number (2), last_c),
2948 !EQ (last_help, help)))
2949 {
2950 total_keys++;
2951 ASET (recent_keys, recent_keys_index, c);
2952 if (++recent_keys_index >= NUM_RECENT_KEYS)
2953 recent_keys_index = 0;
2954 }
2955 }
2956 }
2957 else
2958 {
2959 total_keys++;
2960 ASET (recent_keys, recent_keys_index, c);
2961 if (++recent_keys_index >= NUM_RECENT_KEYS)
2962 recent_keys_index = 0;
2963 }
2964
2965 /* Write c to the dribble file. If c is a lispy event, write
2966 the event's symbol to the dribble file, in <brackets>. Bleaugh.
2967 If you, dear reader, have a better idea, you've got the source. :-) */
2968 if (dribble)
2969 {
2970 if (INTEGERP (c))
2971 {
2972 if (XUINT (c) < 0x100)
2973 putc (XINT (c), dribble);
2974 else
2975 fprintf (dribble, " 0x%x", (int) XUINT (c));
2976 }
2977 else
2978 {
2979 Lisp_Object dribblee;
2980
2981 /* If it's a structured event, take the event header. */
2982 dribblee = EVENT_HEAD (c);
2983
2984 if (SYMBOLP (dribblee))
2985 {
2986 putc ('<', dribble);
2987 fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
2988 STRING_BYTES (XSYMBOL (dribblee)->name),
2989 dribble);
2990 putc ('>', dribble);
2991 }
2992 }
2993
2994 fflush (dribble);
2995 }
2996
2997 if (!CONSP (c) || !EQ (Qhelp_echo, XCAR (c)))
2998 store_kbd_macro_char (c);
2999
3000 num_nonmacro_input_events++;
3001 }
3002
3003 Lisp_Object
3004 print_help (object)
3005 Lisp_Object object;
3006 {
3007 struct buffer *old = current_buffer;
3008 Fprinc (object, Qnil);
3009 set_buffer_internal (XBUFFER (Vstandard_output));
3010 call0 (intern ("help-mode"));
3011 set_buffer_internal (old);
3012 return Qnil;
3013 }
3014
3015 /* Copy out or in the info on where C-g should throw to.
3016 This is used when running Lisp code from within get_char,
3017 in case get_char is called recursively.
3018 See read_process_output. */
3019
3020 static void
3021 save_getcjmp (temp)
3022 jmp_buf temp;
3023 {
3024 bcopy (getcjmp, temp, sizeof getcjmp);
3025 }
3026
3027 static void
3028 restore_getcjmp (temp)
3029 jmp_buf temp;
3030 {
3031 bcopy (temp, getcjmp, sizeof getcjmp);
3032 }
3033 \f
3034 #ifdef HAVE_MOUSE
3035
3036 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
3037 of this function. */
3038
3039 static Lisp_Object
3040 tracking_off (old_value)
3041 Lisp_Object old_value;
3042 {
3043 do_mouse_tracking = old_value;
3044 if (NILP (old_value))
3045 {
3046 /* Redisplay may have been preempted because there was input
3047 available, and it assumes it will be called again after the
3048 input has been processed. If the only input available was
3049 the sort that we have just disabled, then we need to call
3050 redisplay. */
3051 if (!readable_events (1))
3052 {
3053 redisplay_preserve_echo_area (6);
3054 get_input_pending (&input_pending, 1);
3055 }
3056 }
3057 return Qnil;
3058 }
3059
3060 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
3061 "Evaluate BODY with mouse movement events enabled.\n\
3062 Within a `track-mouse' form, mouse motion generates input events that\n\
3063 you can read with `read-event'.\n\
3064 Normally, mouse motion is ignored.")
3065 (args)
3066 Lisp_Object args;
3067 {
3068 int count = specpdl_ptr - specpdl;
3069 Lisp_Object val;
3070
3071 record_unwind_protect (tracking_off, do_mouse_tracking);
3072
3073 do_mouse_tracking = Qt;
3074
3075 val = Fprogn (args);
3076 return unbind_to (count, val);
3077 }
3078
3079 /* If mouse has moved on some frame, return one of those frames.
3080 Return 0 otherwise. */
3081
3082 static FRAME_PTR
3083 some_mouse_moved ()
3084 {
3085 Lisp_Object tail, frame;
3086
3087 FOR_EACH_FRAME (tail, frame)
3088 {
3089 if (XFRAME (frame)->mouse_moved)
3090 return XFRAME (frame);
3091 }
3092
3093 return 0;
3094 }
3095
3096 #endif /* HAVE_MOUSE */
3097 \f
3098 /* Low level keyboard/mouse input.
3099 kbd_buffer_store_event places events in kbd_buffer, and
3100 kbd_buffer_get_event retrieves them. */
3101
3102 /* Return true iff there are any events in the queue that read-char
3103 would return. If this returns false, a read-char would block. */
3104 static int
3105 readable_events (do_timers_now)
3106 int do_timers_now;
3107 {
3108 if (do_timers_now)
3109 timer_check (do_timers_now);
3110
3111 if (kbd_fetch_ptr != kbd_store_ptr)
3112 return 1;
3113 #ifdef HAVE_MOUSE
3114 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3115 return 1;
3116 #endif
3117 if (single_kboard)
3118 {
3119 if (current_kboard->kbd_queue_has_data)
3120 return 1;
3121 }
3122 else
3123 {
3124 KBOARD *kb;
3125 for (kb = all_kboards; kb; kb = kb->next_kboard)
3126 if (kb->kbd_queue_has_data)
3127 return 1;
3128 }
3129 return 0;
3130 }
3131
3132 /* Set this for debugging, to have a way to get out */
3133 int stop_character;
3134
3135 #ifdef MULTI_KBOARD
3136 static KBOARD *
3137 event_to_kboard (event)
3138 struct input_event *event;
3139 {
3140 Lisp_Object frame;
3141 frame = event->frame_or_window;
3142 if (CONSP (frame))
3143 frame = XCAR (frame);
3144 else if (WINDOWP (frame))
3145 frame = WINDOW_FRAME (XWINDOW (frame));
3146
3147 /* There are still some events that don't set this field.
3148 For now, just ignore the problem.
3149 Also ignore dead frames here. */
3150 if (!FRAMEP (frame) || !FRAME_LIVE_P (XFRAME (frame)))
3151 return 0;
3152 else
3153 return FRAME_KBOARD (XFRAME (frame));
3154 }
3155 #endif
3156
3157 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
3158
3159 void
3160 kbd_buffer_store_event (event)
3161 register struct input_event *event;
3162 {
3163 if (event->kind == no_event)
3164 abort ();
3165
3166 if (event->kind == ascii_keystroke)
3167 {
3168 register int c = event->code & 0377;
3169
3170 if (event->modifiers & ctrl_modifier)
3171 c = make_ctrl_char (c);
3172
3173 c |= (event->modifiers
3174 & (meta_modifier | alt_modifier
3175 | hyper_modifier | super_modifier));
3176
3177 if (c == quit_char)
3178 {
3179 extern SIGTYPE interrupt_signal ();
3180 #ifdef MULTI_KBOARD
3181 KBOARD *kb;
3182 struct input_event *sp;
3183
3184 if (single_kboard
3185 && (kb = FRAME_KBOARD (XFRAME (event->frame_or_window)),
3186 kb != current_kboard))
3187 {
3188 kb->kbd_queue
3189 = Fcons (make_lispy_switch_frame (event->frame_or_window),
3190 Fcons (make_number (c), Qnil));
3191 kb->kbd_queue_has_data = 1;
3192 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
3193 {
3194 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3195 sp = kbd_buffer;
3196
3197 if (event_to_kboard (sp) == kb)
3198 {
3199 sp->kind = no_event;
3200 sp->frame_or_window = Qnil;
3201 sp->arg = Qnil;
3202 }
3203 }
3204 return;
3205 }
3206 #endif
3207
3208 /* If this results in a quit_char being returned to Emacs as
3209 input, set Vlast_event_frame properly. If this doesn't
3210 get returned to Emacs as an event, the next event read
3211 will set Vlast_event_frame again, so this is safe to do. */
3212 {
3213 Lisp_Object focus;
3214
3215 focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
3216 if (NILP (focus))
3217 focus = event->frame_or_window;
3218 internal_last_event_frame = focus;
3219 Vlast_event_frame = focus;
3220 }
3221
3222 last_event_timestamp = event->timestamp;
3223 interrupt_signal ();
3224 return;
3225 }
3226
3227 if (c && c == stop_character)
3228 {
3229 sys_suspend ();
3230 return;
3231 }
3232 }
3233 /* Don't insert two buffer_switch_event's in a row.
3234 Just ignore the second one. */
3235 else if (event->kind == buffer_switch_event
3236 && kbd_fetch_ptr != kbd_store_ptr
3237 && kbd_store_ptr->kind == buffer_switch_event)
3238 return;
3239
3240 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
3241 kbd_store_ptr = kbd_buffer;
3242
3243 /* Don't let the very last slot in the buffer become full,
3244 since that would make the two pointers equal,
3245 and that is indistinguishable from an empty buffer.
3246 Discard the event if it would fill the last slot. */
3247 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
3248 {
3249 int idx;
3250
3251 #if 0 /* The selection_request_event case looks bogus, and it's error
3252 prone to assign individual members for other events, in case
3253 the input_event structure is changed. --2000-07-13, gerd. */
3254 struct input_event *sp = kbd_store_ptr;
3255 sp->kind = event->kind;
3256 if (event->kind == selection_request_event)
3257 {
3258 /* We must not use the ordinary copying code for this case,
3259 since `part' is an enum and copying it might not copy enough
3260 in this case. */
3261 bcopy (event, (char *) sp, sizeof (*event));
3262 }
3263 else
3264
3265 {
3266 sp->code = event->code;
3267 sp->part = event->part;
3268 sp->frame_or_window = event->frame_or_window;
3269 sp->arg = event->arg;
3270 sp->modifiers = event->modifiers;
3271 sp->x = event->x;
3272 sp->y = event->y;
3273 sp->timestamp = event->timestamp;
3274 }
3275 #else
3276 *kbd_store_ptr = *event;
3277 #endif
3278
3279 idx = 2 * (kbd_store_ptr - kbd_buffer);
3280 ASET (kbd_buffer_gcpro, idx, event->frame_or_window);
3281 ASET (kbd_buffer_gcpro, idx + 1, event->arg);
3282 ++kbd_store_ptr;
3283 }
3284 }
3285
3286
3287 /* Generate HELP_EVENT input_events in BUFP which has room for
3288 SIZE events. If there's not enough room in BUFP, ignore this
3289 event.
3290
3291 HELP is the help form.
3292
3293 FRAME is the frame on which the help is generated. OBJECT is the
3294 Lisp object where the help was found (a buffer, a string, an
3295 overlay, or nil if neither from a string nor from a buffer. POS is
3296 the position within OBJECT where the help was found.
3297
3298 Value is the number of input_events generated. */
3299
3300 int
3301 gen_help_event (bufp, size, help, frame, window, object, pos)
3302 struct input_event *bufp;
3303 int size;
3304 Lisp_Object help, frame, object, window;
3305 int pos;
3306 {
3307 int nevents_stored = 0;
3308
3309 if (size >= 2)
3310 {
3311 bufp->kind = HELP_EVENT;
3312 bufp->frame_or_window = frame;
3313 bufp->arg = object;
3314 bufp->x = make_number (pos);
3315 bufp->code = 0;
3316
3317 ++bufp;
3318 bufp->kind = HELP_EVENT;
3319 bufp->frame_or_window = WINDOWP (window) ? window : frame;
3320 bufp->arg = help;
3321 bufp->code = 1;
3322 nevents_stored = 2;
3323 }
3324
3325 return nevents_stored;
3326 }
3327
3328
3329 /* Store HELP_EVENTs for HELP on FRAME in the input queue. */
3330
3331 void
3332 kbd_buffer_store_help_event (frame, help)
3333 Lisp_Object frame, help;
3334 {
3335 struct input_event event;
3336
3337 event.kind = HELP_EVENT;
3338 event.frame_or_window = frame;
3339 event.arg = Qnil;
3340 event.x = make_number (0);
3341 event.code = 0;
3342 kbd_buffer_store_event (&event);
3343
3344 event.kind = HELP_EVENT;
3345 event.frame_or_window = frame;
3346 event.arg = help;
3347 event.x = make_number (0);
3348 event.code = 1;
3349 kbd_buffer_store_event (&event);
3350 }
3351
3352 \f
3353 /* Discard any mouse events in the event buffer by setting them to
3354 no_event. */
3355 void
3356 discard_mouse_events ()
3357 {
3358 struct input_event *sp;
3359 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
3360 {
3361 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3362 sp = kbd_buffer;
3363
3364 if (sp->kind == mouse_click
3365 #ifdef WINDOWSNT
3366 || sp->kind == w32_scroll_bar_click
3367 #endif
3368 || sp->kind == scroll_bar_click)
3369 {
3370 sp->kind = no_event;
3371 }
3372 }
3373 }
3374
3375
3376 /* Return non-zero if there are any real events waiting in the event
3377 buffer, not counting `no_event's.
3378
3379 If DISCARD is non-zero, discard no_event events at the front of
3380 the input queue, possibly leaving the input queue empty if there
3381 are no real input events. */
3382
3383 int
3384 kbd_buffer_events_waiting (discard)
3385 int discard;
3386 {
3387 struct input_event *sp;
3388
3389 for (sp = kbd_fetch_ptr;
3390 sp != kbd_store_ptr && sp->kind == no_event;
3391 ++sp)
3392 {
3393 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
3394 sp = kbd_buffer;
3395 }
3396
3397 if (discard)
3398 kbd_fetch_ptr = sp;
3399
3400 return sp != kbd_store_ptr && sp->kind != no_event;
3401 }
3402
3403 \f
3404 /* Clear input event EVENT. */
3405
3406 static INLINE void
3407 clear_event (event)
3408 struct input_event *event;
3409 {
3410 int idx = 2 * (event - kbd_buffer);
3411 ASET (kbd_buffer_gcpro, idx, Qnil);
3412 ASET (kbd_buffer_gcpro, idx + 1, Qnil);
3413 event->kind = no_event;
3414 }
3415
3416
3417 /* Read one event from the event buffer, waiting if necessary.
3418 The value is a Lisp object representing the event.
3419 The value is nil for an event that should be ignored,
3420 or that was handled here.
3421 We always read and discard one event. */
3422
3423 static Lisp_Object
3424 kbd_buffer_get_event (kbp, used_mouse_menu)
3425 KBOARD **kbp;
3426 int *used_mouse_menu;
3427 {
3428 register int c;
3429 Lisp_Object obj;
3430
3431 if (noninteractive)
3432 {
3433 c = getchar ();
3434 XSETINT (obj, c);
3435 *kbp = current_kboard;
3436 return obj;
3437 }
3438
3439 /* Wait until there is input available. */
3440 for (;;)
3441 {
3442 if (kbd_fetch_ptr != kbd_store_ptr)
3443 break;
3444 #ifdef HAVE_MOUSE
3445 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3446 break;
3447 #endif
3448
3449 /* If the quit flag is set, then read_char will return
3450 quit_char, so that counts as "available input." */
3451 if (!NILP (Vquit_flag))
3452 quit_throw_to_read_char ();
3453
3454 /* One way or another, wait until input is available; then, if
3455 interrupt handlers have not read it, read it now. */
3456
3457 #ifdef OLDVMS
3458 wait_for_kbd_input ();
3459 #else
3460 /* Note SIGIO has been undef'd if FIONREAD is missing. */
3461 #ifdef SIGIO
3462 gobble_input (0);
3463 #endif /* SIGIO */
3464 if (kbd_fetch_ptr != kbd_store_ptr)
3465 break;
3466 #ifdef HAVE_MOUSE
3467 if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3468 break;
3469 #endif
3470 {
3471 Lisp_Object minus_one;
3472
3473 XSETINT (minus_one, -1);
3474 wait_reading_process_input (0, 0, minus_one, 1);
3475
3476 if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
3477 /* Pass 1 for EXPECT since we just waited to have input. */
3478 read_avail_input (1);
3479 }
3480 #endif /* not VMS */
3481 }
3482
3483 if (CONSP (Vunread_command_events))
3484 {
3485 Lisp_Object first;
3486 first = XCAR (Vunread_command_events);
3487 Vunread_command_events = XCDR (Vunread_command_events);
3488 *kbp = current_kboard;
3489 return first;
3490 }
3491
3492 /* At this point, we know that there is a readable event available
3493 somewhere. If the event queue is empty, then there must be a
3494 mouse movement enabled and available. */
3495 if (kbd_fetch_ptr != kbd_store_ptr)
3496 {
3497 struct input_event *event;
3498
3499 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3500 ? kbd_fetch_ptr
3501 : kbd_buffer);
3502
3503 last_event_timestamp = event->timestamp;
3504
3505 #ifdef MULTI_KBOARD
3506 *kbp = event_to_kboard (event);
3507 if (*kbp == 0)
3508 *kbp = current_kboard; /* Better than returning null ptr? */
3509 #else
3510 *kbp = &the_only_kboard;
3511 #endif
3512
3513 obj = Qnil;
3514
3515 /* These two kinds of events get special handling
3516 and don't actually appear to the command loop.
3517 We return nil for them. */
3518 if (event->kind == selection_request_event)
3519 {
3520 #ifdef HAVE_X11
3521 struct input_event copy;
3522
3523 /* Remove it from the buffer before processing it,
3524 since otherwise swallow_events will see it
3525 and process it again. */
3526 copy = *event;
3527 kbd_fetch_ptr = event + 1;
3528 input_pending = readable_events (0);
3529 x_handle_selection_request (&copy);
3530 #else
3531 /* We're getting selection request events, but we don't have
3532 a window system. */
3533 abort ();
3534 #endif
3535 }
3536
3537 else if (event->kind == selection_clear_event)
3538 {
3539 #ifdef HAVE_X11
3540 struct input_event copy;
3541
3542 /* Remove it from the buffer before processing it. */
3543 copy = *event;
3544 kbd_fetch_ptr = event + 1;
3545 input_pending = readable_events (0);
3546 x_handle_selection_clear (&copy);
3547 #else
3548 /* We're getting selection request events, but we don't have
3549 a window system. */
3550 abort ();
3551 #endif
3552 }
3553 #if defined (HAVE_X11) || defined (HAVE_NTGUI) || defined (macintosh)
3554 else if (event->kind == delete_window_event)
3555 {
3556 /* Make an event (delete-frame (FRAME)). */
3557 obj = Fcons (event->frame_or_window, Qnil);
3558 obj = Fcons (Qdelete_frame, Fcons (obj, Qnil));
3559 kbd_fetch_ptr = event + 1;
3560 }
3561 #endif
3562 #if defined (HAVE_X11) || defined (HAVE_NTGUI)
3563 else if (event->kind == iconify_event)
3564 {
3565 /* Make an event (iconify-frame (FRAME)). */
3566 obj = Fcons (event->frame_or_window, Qnil);
3567 obj = Fcons (Qiconify_frame, Fcons (obj, Qnil));
3568 kbd_fetch_ptr = event + 1;
3569 }
3570 else if (event->kind == deiconify_event)
3571 {
3572 /* Make an event (make-frame-visible (FRAME)). */
3573 obj = Fcons (event->frame_or_window, Qnil);
3574 obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil));
3575 kbd_fetch_ptr = event + 1;
3576 }
3577 #endif
3578 else if (event->kind == buffer_switch_event)
3579 {
3580 /* The value doesn't matter here; only the type is tested. */
3581 XSETBUFFER (obj, current_buffer);
3582 kbd_fetch_ptr = event + 1;
3583 }
3584 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
3585 else if (event->kind == menu_bar_activate_event)
3586 {
3587 kbd_fetch_ptr = event + 1;
3588 input_pending = readable_events (0);
3589 if (FRAME_LIVE_P (XFRAME (event->frame_or_window)))
3590 x_activate_menubar (XFRAME (event->frame_or_window));
3591 }
3592 #endif
3593 #ifdef WINDOWSNT
3594 else if (event->kind == language_change_event)
3595 {
3596 /* Make an event (language-change (FRAME CHARSET LCID)). */
3597 obj = Fcons (event->modifiers, Qnil);
3598 obj = Fcons (event->code, Qnil);
3599 obj = Fcons (event->frame_or_window, obj);
3600 obj = Fcons (Qlanguage_change, Fcons (obj, Qnil));
3601 kbd_fetch_ptr = event + 1;
3602 }
3603 #endif
3604 /* Just discard these, by returning nil.
3605 With MULTI_KBOARD, these events are used as placeholders
3606 when we need to randomly delete events from the queue.
3607 (They shouldn't otherwise be found in the buffer,
3608 but on some machines it appears they do show up
3609 even without MULTI_KBOARD.) */
3610 /* On Windows NT/9X, no_event is used to delete extraneous
3611 mouse events during a popup-menu call. */
3612 else if (event->kind == no_event)
3613 kbd_fetch_ptr = event + 1;
3614 else if (event->kind == HELP_EVENT)
3615 {
3616 /* There are always two HELP_EVENTs in the input queue. */
3617 Lisp_Object object, position, help, frame, window;
3618
3619 xassert (event->code == 0);
3620 frame = event->frame_or_window;
3621 object = event->arg;
3622 position = event->x;
3623 clear_event (event);
3624
3625 kbd_fetch_ptr = event + 1;
3626 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3627 ? kbd_fetch_ptr
3628 : kbd_buffer);
3629 xassert (event->code == 1);
3630 help = event->arg;
3631 window = event->frame_or_window;
3632 if (!WINDOWP (window))
3633 window = Qnil;
3634 obj = Fcons (Qhelp_echo,
3635 list5 (frame, help, window, object, position));
3636 clear_event (event);
3637 kbd_fetch_ptr = event + 1;
3638 }
3639 else if (event->kind == FOCUS_IN_EVENT)
3640 {
3641 /* Notification of a FocusIn event. The frame receiving the
3642 focus is in event->frame_or_window. Generate a
3643 switch-frame event if necessary. */
3644 Lisp_Object frame, focus;
3645
3646 frame = event->frame_or_window;
3647 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
3648 if (FRAMEP (focus))
3649 frame = focus;
3650
3651 if (!EQ (frame, internal_last_event_frame)
3652 && !EQ (frame, selected_frame))
3653 obj = make_lispy_switch_frame (frame);
3654 internal_last_event_frame = frame;
3655 kbd_fetch_ptr = event + 1;
3656 }
3657 else
3658 {
3659 /* If this event is on a different frame, return a switch-frame this
3660 time, and leave the event in the queue for next time. */
3661 Lisp_Object frame;
3662 Lisp_Object focus;
3663
3664 frame = event->frame_or_window;
3665 if (CONSP (frame))
3666 frame = XCAR (frame);
3667 else if (WINDOWP (frame))
3668 frame = WINDOW_FRAME (XWINDOW (frame));
3669
3670 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
3671 if (! NILP (focus))
3672 frame = focus;
3673
3674 if (! EQ (frame, internal_last_event_frame)
3675 && !EQ (frame, selected_frame))
3676 obj = make_lispy_switch_frame (frame);
3677 internal_last_event_frame = frame;
3678
3679 /* If we didn't decide to make a switch-frame event, go ahead
3680 and build a real event from the queue entry. */
3681
3682 if (NILP (obj))
3683 {
3684 obj = make_lispy_event (event);
3685
3686 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI)
3687 /* If this was a menu selection, then set the flag to inhibit
3688 writing to last_nonmenu_event. Don't do this if the event
3689 we're returning is (menu-bar), though; that indicates the
3690 beginning of the menu sequence, and we might as well leave
3691 that as the `event with parameters' for this selection. */
3692 if (used_mouse_menu
3693 && !EQ (event->frame_or_window, event->arg)
3694 && (event->kind == MENU_BAR_EVENT
3695 || event->kind == TOOL_BAR_EVENT))
3696 *used_mouse_menu = 1;
3697 #endif
3698
3699 /* Wipe out this event, to catch bugs. */
3700 clear_event (event);
3701 kbd_fetch_ptr = event + 1;
3702 }
3703 }
3704 }
3705 #ifdef HAVE_MOUSE
3706 /* Try generating a mouse motion event. */
3707 else if (!NILP (do_mouse_tracking) && some_mouse_moved ())
3708 {
3709 FRAME_PTR f = some_mouse_moved ();
3710 Lisp_Object bar_window;
3711 enum scroll_bar_part part;
3712 Lisp_Object x, y;
3713 unsigned long time;
3714
3715 *kbp = current_kboard;
3716 /* Note that this uses F to determine which display to look at.
3717 If there is no valid info, it does not store anything
3718 so x remains nil. */
3719 x = Qnil;
3720 (*mouse_position_hook) (&f, 0, &bar_window, &part, &x, &y, &time);
3721
3722 obj = Qnil;
3723
3724 /* Decide if we should generate a switch-frame event. Don't
3725 generate switch-frame events for motion outside of all Emacs
3726 frames. */
3727 if (!NILP (x) && f)
3728 {
3729 Lisp_Object frame;
3730
3731 frame = FRAME_FOCUS_FRAME (f);
3732 if (NILP (frame))
3733 XSETFRAME (frame, f);
3734
3735 if (! EQ (frame, internal_last_event_frame)
3736 && !EQ (frame, selected_frame))
3737 obj = make_lispy_switch_frame (frame);
3738 internal_last_event_frame = frame;
3739 }
3740
3741 /* If we didn't decide to make a switch-frame event, go ahead and
3742 return a mouse-motion event. */
3743 if (!NILP (x) && NILP (obj))
3744 obj = make_lispy_movement (f, bar_window, part, x, y, time);
3745 }
3746 #endif /* HAVE_MOUSE */
3747 else
3748 /* We were promised by the above while loop that there was
3749 something for us to read! */
3750 abort ();
3751
3752 input_pending = readable_events (0);
3753
3754 Vlast_event_frame = internal_last_event_frame;
3755
3756 return (obj);
3757 }
3758 \f
3759 /* Process any events that are not user-visible,
3760 then return, without reading any user-visible events. */
3761
3762 void
3763 swallow_events (do_display)
3764 int do_display;
3765 {
3766 int old_timers_run;
3767
3768 while (kbd_fetch_ptr != kbd_store_ptr)
3769 {
3770 struct input_event *event;
3771
3772 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
3773 ? kbd_fetch_ptr
3774 : kbd_buffer);
3775
3776 last_event_timestamp = event->timestamp;
3777
3778 /* These two kinds of events get special handling
3779 and don't actually appear to the command loop. */
3780 if (event->kind == selection_request_event)
3781 {
3782 #ifdef HAVE_X11
3783 struct input_event copy;
3784
3785 /* Remove it from the buffer before processing it,
3786 since otherwise swallow_events called recursively could see it
3787 and process it again. */
3788 copy = *event;
3789 kbd_fetch_ptr = event + 1;
3790 input_pending = readable_events (0);
3791 x_handle_selection_request (&copy);
3792 #else
3793 /* We're getting selection request events, but we don't have
3794 a window system. */
3795 abort ();
3796 #endif
3797 }
3798
3799 else if (event->kind == selection_clear_event)
3800 {
3801 #ifdef HAVE_X11
3802 struct input_event copy;
3803
3804 /* Remove it from the buffer before processing it, */
3805 copy = *event;
3806
3807 kbd_fetch_ptr = event + 1;
3808 input_pending = readable_events (0);
3809 x_handle_selection_clear (&copy);
3810 #else
3811 /* We're getting selection request events, but we don't have
3812 a window system. */
3813 abort ();
3814 #endif
3815 }
3816 else
3817 break;
3818 }
3819
3820 old_timers_run = timers_run;
3821 get_input_pending (&input_pending, 1);
3822
3823 if (timers_run != old_timers_run && do_display)
3824 redisplay_preserve_echo_area (7);
3825 }
3826 \f
3827 static EMACS_TIME timer_idleness_start_time;
3828
3829 /* Record the start of when Emacs is idle,
3830 for the sake of running idle-time timers. */
3831
3832 void
3833 timer_start_idle ()
3834 {
3835 Lisp_Object timers;
3836
3837 /* If we are already in the idle state, do nothing. */
3838 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3839 return;
3840
3841 EMACS_GET_TIME (timer_idleness_start_time);
3842
3843 /* Mark all idle-time timers as once again candidates for running. */
3844 for (timers = Vtimer_idle_list; CONSP (timers); timers = XCDR (timers))
3845 {
3846 Lisp_Object timer;
3847
3848 timer = XCAR (timers);
3849
3850 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3851 continue;
3852 XVECTOR (timer)->contents[0] = Qnil;
3853 }
3854 }
3855
3856 /* Record that Emacs is no longer idle, so stop running idle-time timers. */
3857
3858 void
3859 timer_stop_idle ()
3860 {
3861 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
3862 }
3863
3864 /* This is only for debugging. */
3865 struct input_event last_timer_event;
3866
3867 /* Check whether a timer has fired. To prevent larger problems we simply
3868 disregard elements that are not proper timers. Do not make a circular
3869 timer list for the time being.
3870
3871 Returns the number of seconds to wait until the next timer fires. If a
3872 timer is triggering now, return zero seconds.
3873 If no timer is active, return -1 seconds.
3874
3875 If a timer is ripe, we run it, with quitting turned off.
3876
3877 DO_IT_NOW is now ignored. It used to mean that we should
3878 run the timer directly instead of queueing a timer-event.
3879 Now we always run timers directly. */
3880
3881 EMACS_TIME
3882 timer_check (do_it_now)
3883 int do_it_now;
3884 {
3885 EMACS_TIME nexttime;
3886 EMACS_TIME now, idleness_now;
3887 Lisp_Object timers, idle_timers, chosen_timer;
3888 struct gcpro gcpro1, gcpro2, gcpro3;
3889
3890 EMACS_SET_SECS (nexttime, -1);
3891 EMACS_SET_USECS (nexttime, -1);
3892
3893 /* Always consider the ordinary timers. */
3894 timers = Vtimer_list;
3895 /* Consider the idle timers only if Emacs is idle. */
3896 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3897 idle_timers = Vtimer_idle_list;
3898 else
3899 idle_timers = Qnil;
3900 chosen_timer = Qnil;
3901 GCPRO3 (timers, idle_timers, chosen_timer);
3902
3903 if (CONSP (timers) || CONSP (idle_timers))
3904 {
3905 EMACS_GET_TIME (now);
3906 if (! EMACS_TIME_NEG_P (timer_idleness_start_time))
3907 EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time);
3908 }
3909
3910 while (CONSP (timers) || CONSP (idle_timers))
3911 {
3912 Lisp_Object *vector;
3913 Lisp_Object timer = Qnil, idle_timer = Qnil;
3914 EMACS_TIME timer_time, idle_timer_time;
3915 EMACS_TIME difference, timer_difference, idle_timer_difference;
3916
3917 /* Skip past invalid timers and timers already handled. */
3918 if (!NILP (timers))
3919 {
3920 timer = XCAR (timers);
3921 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3922 {
3923 timers = XCDR (timers);
3924 continue;
3925 }
3926 vector = XVECTOR (timer)->contents;
3927
3928 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3929 || !INTEGERP (vector[3])
3930 || ! NILP (vector[0]))
3931 {
3932 timers = XCDR (timers);
3933 continue;
3934 }
3935 }
3936 if (!NILP (idle_timers))
3937 {
3938 timer = XCAR (idle_timers);
3939 if (!VECTORP (timer) || XVECTOR (timer)->size != 8)
3940 {
3941 idle_timers = XCDR (idle_timers);
3942 continue;
3943 }
3944 vector = XVECTOR (timer)->contents;
3945
3946 if (!INTEGERP (vector[1]) || !INTEGERP (vector[2])
3947 || !INTEGERP (vector[3])
3948 || ! NILP (vector[0]))
3949 {
3950 idle_timers = XCDR (idle_timers);
3951 continue;
3952 }
3953 }
3954
3955 /* Set TIMER, TIMER_TIME and TIMER_DIFFERENCE
3956 based on the next ordinary timer.
3957 TIMER_DIFFERENCE is the distance in time from NOW to when
3958 this timer becomes ripe (negative if it's already ripe). */
3959 if (!NILP (timers))
3960 {
3961 timer = XCAR (timers);
3962 vector = XVECTOR (timer)->contents;
3963 EMACS_SET_SECS (timer_time,
3964 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3965 EMACS_SET_USECS (timer_time, XINT (vector[3]));
3966 EMACS_SUB_TIME (timer_difference, timer_time, now);
3967 }
3968
3969 /* Set IDLE_TIMER, IDLE_TIMER_TIME and IDLE_TIMER_DIFFERENCE
3970 based on the next idle timer. */
3971 if (!NILP (idle_timers))
3972 {
3973 idle_timer = XCAR (idle_timers);
3974 vector = XVECTOR (idle_timer)->contents;
3975 EMACS_SET_SECS (idle_timer_time,
3976 (XINT (vector[1]) << 16) | (XINT (vector[2])));
3977 EMACS_SET_USECS (idle_timer_time, XINT (vector[3]));
3978 EMACS_SUB_TIME (idle_timer_difference, idle_timer_time, idleness_now);
3979 }
3980
3981 /* Decide which timer is the next timer,
3982 and set CHOSEN_TIMER, VECTOR and DIFFERENCE accordingly.
3983 Also step down the list where we found that timer. */
3984
3985 if (! NILP (timers) && ! NILP (idle_timers))
3986 {
3987 EMACS_TIME temp;
3988 EMACS_SUB_TIME (temp, timer_difference, idle_timer_difference);
3989 if (EMACS_TIME_NEG_P (temp))
3990 {
3991 chosen_timer = timer;
3992 timers = XCDR (timers);
3993 difference = timer_difference;
3994 }
3995 else
3996 {
3997 chosen_timer = idle_timer;
3998 idle_timers = XCDR (idle_timers);
3999 difference = idle_timer_difference;
4000 }
4001 }
4002 else if (! NILP (timers))
4003 {
4004 chosen_timer = timer;
4005 timers = XCDR (timers);
4006 difference = timer_difference;
4007 }
4008 else
4009 {
4010 chosen_timer = idle_timer;
4011 idle_timers = XCDR (idle_timers);
4012 difference = idle_timer_difference;
4013 }
4014 vector = XVECTOR (chosen_timer)->contents;
4015
4016 /* If timer is ripe, run it if it hasn't been run. */
4017 if (EMACS_TIME_NEG_P (difference)
4018 || (EMACS_SECS (difference) == 0
4019 && EMACS_USECS (difference) == 0))
4020 {
4021 if (NILP (vector[0]))
4022 {
4023 int was_locked = single_kboard;
4024 int count = BINDING_STACK_SIZE ();
4025 Lisp_Object old_deactivate_mark = Vdeactivate_mark;
4026
4027 /* Mark the timer as triggered to prevent problems if the lisp
4028 code fails to reschedule it right. */
4029 vector[0] = Qt;
4030
4031 specbind (Qinhibit_quit, Qt);
4032
4033 call1 (Qtimer_event_handler, chosen_timer);
4034 Vdeactivate_mark = old_deactivate_mark;
4035 timers_run++;
4036 unbind_to (count, Qnil);
4037
4038 /* Resume allowing input from any kboard, if that was true before. */
4039 if (!was_locked)
4040 any_kboard_state ();
4041
4042 /* Since we have handled the event,
4043 we don't need to tell the caller to wake up and do it. */
4044 }
4045 }
4046 else
4047 /* When we encounter a timer that is still waiting,
4048 return the amount of time to wait before it is ripe. */
4049 {
4050 UNGCPRO;
4051 return difference;
4052 }
4053 }
4054
4055 /* No timers are pending in the future. */
4056 /* Return 0 if we generated an event, and -1 if not. */
4057 UNGCPRO;
4058 return nexttime;
4059 }
4060 \f
4061 /* Caches for modify_event_symbol. */
4062 static Lisp_Object accent_key_syms;
4063 static Lisp_Object func_key_syms;
4064 static Lisp_Object mouse_syms;
4065 #ifdef WINDOWSNT
4066 static Lisp_Object mouse_wheel_syms;
4067 #endif
4068 static Lisp_Object drag_n_drop_syms;
4069
4070 /* This is a list of keysym codes for special "accent" characters.
4071 It parallels lispy_accent_keys. */
4072
4073 static int lispy_accent_codes[] =
4074 {
4075 #ifdef XK_dead_circumflex
4076 XK_dead_circumflex,
4077 #else
4078 0,
4079 #endif
4080 #ifdef XK_dead_grave
4081 XK_dead_grave,
4082 #else
4083 0,
4084 #endif
4085 #ifdef XK_dead_tilde
4086 XK_dead_tilde,
4087 #else
4088 0,
4089 #endif
4090 #ifdef XK_dead_diaeresis
4091 XK_dead_diaeresis,
4092 #else
4093 0,
4094 #endif
4095 #ifdef XK_dead_macron
4096 XK_dead_macron,
4097 #else
4098 0,
4099 #endif
4100 #ifdef XK_dead_degree
4101 XK_dead_degree,
4102 #else
4103 0,
4104 #endif
4105 #ifdef XK_dead_acute
4106 XK_dead_acute,
4107 #else
4108 0,
4109 #endif
4110 #ifdef XK_dead_cedilla
4111 XK_dead_cedilla,
4112 #else
4113 0,
4114 #endif
4115 #ifdef XK_dead_breve
4116 XK_dead_breve,
4117 #else
4118 0,
4119 #endif
4120 #ifdef XK_dead_ogonek
4121 XK_dead_ogonek,
4122 #else
4123 0,
4124 #endif
4125 #ifdef XK_dead_caron
4126 XK_dead_caron,
4127 #else
4128 0,
4129 #endif
4130 #ifdef XK_dead_doubleacute
4131 XK_dead_doubleacute,
4132 #else
4133 0,
4134 #endif
4135 #ifdef XK_dead_abovedot
4136 XK_dead_abovedot,
4137 #else
4138 0,
4139 #endif
4140 };
4141
4142 /* This is a list of Lisp names for special "accent" characters.
4143 It parallels lispy_accent_codes. */
4144
4145 static char *lispy_accent_keys[] =
4146 {
4147 "dead-circumflex",
4148 "dead-grave",
4149 "dead-tilde",
4150 "dead-diaeresis",
4151 "dead-macron",
4152 "dead-degree",
4153 "dead-acute",
4154 "dead-cedilla",
4155 "dead-breve",
4156 "dead-ogonek",
4157 "dead-caron",
4158 "dead-doubleacute",
4159 "dead-abovedot",
4160 };
4161
4162 #ifdef HAVE_NTGUI
4163 #define FUNCTION_KEY_OFFSET 0x0
4164
4165 char *lispy_function_keys[] =
4166 {
4167 0, /* 0 */
4168
4169 0, /* VK_LBUTTON 0x01 */
4170 0, /* VK_RBUTTON 0x02 */
4171 "cancel", /* VK_CANCEL 0x03 */
4172 0, /* VK_MBUTTON 0x04 */
4173
4174 0, 0, 0, /* 0x05 .. 0x07 */
4175
4176 "backspace", /* VK_BACK 0x08 */
4177 "tab", /* VK_TAB 0x09 */
4178
4179 0, 0, /* 0x0A .. 0x0B */
4180
4181 "clear", /* VK_CLEAR 0x0C */
4182 "return", /* VK_RETURN 0x0D */
4183
4184 0, 0, /* 0x0E .. 0x0F */
4185
4186 0, /* VK_SHIFT 0x10 */
4187 0, /* VK_CONTROL 0x11 */
4188 0, /* VK_MENU 0x12 */
4189 "pause", /* VK_PAUSE 0x13 */
4190 "capslock", /* VK_CAPITAL 0x14 */
4191
4192 0, 0, 0, 0, 0, 0, /* 0x15 .. 0x1A */
4193
4194 "escape", /* VK_ESCAPE 0x1B */
4195
4196 0, 0, 0, 0, /* 0x1C .. 0x1F */
4197
4198 0, /* VK_SPACE 0x20 */
4199 "prior", /* VK_PRIOR 0x21 */
4200 "next", /* VK_NEXT 0x22 */
4201 "end", /* VK_END 0x23 */
4202 "home", /* VK_HOME 0x24 */
4203 "left", /* VK_LEFT 0x25 */
4204 "up", /* VK_UP 0x26 */
4205 "right", /* VK_RIGHT 0x27 */
4206 "down", /* VK_DOWN 0x28 */
4207 "select", /* VK_SELECT 0x29 */
4208 "print", /* VK_PRINT 0x2A */
4209 "execute", /* VK_EXECUTE 0x2B */
4210 "snapshot", /* VK_SNAPSHOT 0x2C */
4211 "insert", /* VK_INSERT 0x2D */
4212 "delete", /* VK_DELETE 0x2E */
4213 "help", /* VK_HELP 0x2F */
4214
4215 /* VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39) */
4216
4217 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4218
4219 0, 0, 0, 0, 0, 0, 0, /* 0x3A .. 0x40 */
4220
4221 /* VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A) */
4222
4223 0, 0, 0, 0, 0, 0, 0, 0, 0,
4224 0, 0, 0, 0, 0, 0, 0, 0, 0,
4225 0, 0, 0, 0, 0, 0, 0, 0,
4226
4227 "lwindow", /* VK_LWIN 0x5B */
4228 "rwindow", /* VK_RWIN 0x5C */
4229 "apps", /* VK_APPS 0x5D */
4230
4231 0, 0, /* 0x5E .. 0x5F */
4232
4233 "kp-0", /* VK_NUMPAD0 0x60 */
4234 "kp-1", /* VK_NUMPAD1 0x61 */
4235 "kp-2", /* VK_NUMPAD2 0x62 */
4236 "kp-3", /* VK_NUMPAD3 0x63 */
4237 "kp-4", /* VK_NUMPAD4 0x64 */
4238 "kp-5", /* VK_NUMPAD5 0x65 */
4239 "kp-6", /* VK_NUMPAD6 0x66 */
4240 "kp-7", /* VK_NUMPAD7 0x67 */
4241 "kp-8", /* VK_NUMPAD8 0x68 */
4242 "kp-9", /* VK_NUMPAD9 0x69 */
4243 "kp-multiply", /* VK_MULTIPLY 0x6A */
4244 "kp-add", /* VK_ADD 0x6B */
4245 "kp-separator", /* VK_SEPARATOR 0x6C */
4246 "kp-subtract", /* VK_SUBTRACT 0x6D */
4247 "kp-decimal", /* VK_DECIMAL 0x6E */
4248 "kp-divide", /* VK_DIVIDE 0x6F */
4249 "f1", /* VK_F1 0x70 */
4250 "f2", /* VK_F2 0x71 */
4251 "f3", /* VK_F3 0x72 */
4252 "f4", /* VK_F4 0x73 */
4253 "f5", /* VK_F5 0x74 */
4254 "f6", /* VK_F6 0x75 */
4255 "f7", /* VK_F7 0x76 */
4256 "f8", /* VK_F8 0x77 */
4257 "f9", /* VK_F9 0x78 */
4258 "f10", /* VK_F10 0x79 */
4259 "f11", /* VK_F11 0x7A */
4260 "f12", /* VK_F12 0x7B */
4261 "f13", /* VK_F13 0x7C */
4262 "f14", /* VK_F14 0x7D */
4263 "f15", /* VK_F15 0x7E */
4264 "f16", /* VK_F16 0x7F */
4265 "f17", /* VK_F17 0x80 */
4266 "f18", /* VK_F18 0x81 */
4267 "f19", /* VK_F19 0x82 */
4268 "f20", /* VK_F20 0x83 */
4269 "f21", /* VK_F21 0x84 */
4270 "f22", /* VK_F22 0x85 */
4271 "f23", /* VK_F23 0x86 */
4272 "f24", /* VK_F24 0x87 */
4273
4274 0, 0, 0, 0, /* 0x88 .. 0x8B */
4275 0, 0, 0, 0, /* 0x8C .. 0x8F */
4276
4277 "kp-numlock", /* VK_NUMLOCK 0x90 */
4278 "scroll", /* VK_SCROLL 0x91 */
4279
4280 "kp-space", /* VK_NUMPAD_CLEAR 0x92 */
4281 "kp-enter", /* VK_NUMPAD_ENTER 0x93 */
4282 "kp-prior", /* VK_NUMPAD_PRIOR 0x94 */
4283 "kp-next", /* VK_NUMPAD_NEXT 0x95 */
4284 "kp-end", /* VK_NUMPAD_END 0x96 */
4285 "kp-home", /* VK_NUMPAD_HOME 0x97 */
4286 "kp-left", /* VK_NUMPAD_LEFT 0x98 */
4287 "kp-up", /* VK_NUMPAD_UP 0x99 */
4288 "kp-right", /* VK_NUMPAD_RIGHT 0x9A */
4289 "kp-down", /* VK_NUMPAD_DOWN 0x9B */
4290 "kp-insert", /* VK_NUMPAD_INSERT 0x9C */
4291 "kp-delete", /* VK_NUMPAD_DELETE 0x9D */
4292
4293 0, 0, /* 0x9E .. 0x9F */
4294
4295 /*
4296 * VK_L* & VK_R* - left and right Alt, Ctrl and Shift virtual keys.
4297 * Used only as parameters to GetAsyncKeyState and GetKeyState.
4298 * No other API or message will distinguish left and right keys this way.
4299 */
4300 /* 0xA0 .. 0xEF */
4301
4302 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4303 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4304 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4305 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4306 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4307
4308 /* 0xF0 .. 0xF5 */
4309
4310 0, 0, 0, 0, 0, 0,
4311
4312 "attn", /* VK_ATTN 0xF6 */
4313 "crsel", /* VK_CRSEL 0xF7 */
4314 "exsel", /* VK_EXSEL 0xF8 */
4315 "ereof", /* VK_EREOF 0xF9 */
4316 "play", /* VK_PLAY 0xFA */
4317 "zoom", /* VK_ZOOM 0xFB */
4318 "noname", /* VK_NONAME 0xFC */
4319 "pa1", /* VK_PA1 0xFD */
4320 "oem_clear", /* VK_OEM_CLEAR 0xFE */
4321 0 /* 0xFF */
4322 };
4323
4324 #else /* not HAVE_NTGUI */
4325
4326 #ifdef XK_kana_A
4327 static char *lispy_kana_keys[] =
4328 {
4329 /* X Keysym value */
4330 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x400 .. 0x40f */
4331 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x410 .. 0x41f */
4332 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x420 .. 0x42f */
4333 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x430 .. 0x43f */
4334 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x440 .. 0x44f */
4335 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x450 .. 0x45f */
4336 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x460 .. 0x46f */
4337 0,0,0,0,0,0,0,0,0,0,0,0,0,0,"overline",0,
4338 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x480 .. 0x48f */
4339 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x490 .. 0x49f */
4340 0, "kana-fullstop", "kana-openingbracket", "kana-closingbracket",
4341 "kana-comma", "kana-conjunctive", "kana-WO", "kana-a",
4342 "kana-i", "kana-u", "kana-e", "kana-o",
4343 "kana-ya", "kana-yu", "kana-yo", "kana-tsu",
4344 "prolongedsound", "kana-A", "kana-I", "kana-U",
4345 "kana-E", "kana-O", "kana-KA", "kana-KI",
4346 "kana-KU", "kana-KE", "kana-KO", "kana-SA",
4347 "kana-SHI", "kana-SU", "kana-SE", "kana-SO",
4348 "kana-TA", "kana-CHI", "kana-TSU", "kana-TE",
4349 "kana-TO", "kana-NA", "kana-NI", "kana-NU",
4350 "kana-NE", "kana-NO", "kana-HA", "kana-HI",
4351 "kana-FU", "kana-HE", "kana-HO", "kana-MA",
4352 "kana-MI", "kana-MU", "kana-ME", "kana-MO",
4353 "kana-YA", "kana-YU", "kana-YO", "kana-RA",
4354 "kana-RI", "kana-RU", "kana-RE", "kana-RO",
4355 "kana-WA", "kana-N", "voicedsound", "semivoicedsound",
4356 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4e0 .. 0x4ef */
4357 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4f0 .. 0x4ff */
4358 };
4359 #endif /* XK_kana_A */
4360
4361 #define FUNCTION_KEY_OFFSET 0xff00
4362
4363 /* You'll notice that this table is arranged to be conveniently
4364 indexed by X Windows keysym values. */
4365 static char *lispy_function_keys[] =
4366 {
4367 /* X Keysym value */
4368
4369 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00...0f */
4370 "backspace", "tab", "linefeed", "clear",
4371 0, "return", 0, 0,
4372 0, 0, 0, "pause", /* 0xff10...1f */
4373 0, 0, 0, 0, 0, 0, 0, "escape",
4374 0, 0, 0, 0,
4375 0, "kanji", "muhenkan", "henkan", /* 0xff20...2f */
4376 "romaji", "hiragana", "katakana", "hiragana-katakana",
4377 "zenkaku", "hankaku", "zenkaku-hankaku", "touroku",
4378 "massyo", "kana-lock", "kana-shift", "eisu-shift",
4379 "eisu-toggle", /* 0xff30...3f */
4380 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
4381 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
4382
4383 "home", "left", "up", "right", /* 0xff50 */ /* IsCursorKey */
4384 "down", "prior", "next", "end",
4385 "begin", 0, 0, 0, 0, 0, 0, 0,
4386 "select", /* 0xff60 */ /* IsMiscFunctionKey */
4387 "print",
4388 "execute",
4389 "insert",
4390 0, /* 0xff64 */
4391 "undo",
4392 "redo",
4393 "menu",
4394 "find",
4395 "cancel",
4396 "help",
4397 "break", /* 0xff6b */
4398
4399 0, 0, 0, 0,
4400 0, 0, 0, 0, "backtab", 0, 0, 0, /* 0xff70... */
4401 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff78... */
4402 "kp-space", /* 0xff80 */ /* IsKeypadKey */
4403 0, 0, 0, 0, 0, 0, 0, 0,
4404 "kp-tab", /* 0xff89 */
4405 0, 0, 0,
4406 "kp-enter", /* 0xff8d */
4407 0, 0, 0,
4408 "kp-f1", /* 0xff91 */
4409 "kp-f2",
4410 "kp-f3",
4411 "kp-f4",
4412 "kp-home", /* 0xff95 */
4413 "kp-left",
4414 "kp-up",
4415 "kp-right",
4416 "kp-down",
4417 "kp-prior", /* kp-page-up */
4418 "kp-next", /* kp-page-down */
4419 "kp-end",
4420 "kp-begin",
4421 "kp-insert",
4422 "kp-delete",
4423 0, /* 0xffa0 */
4424 0, 0, 0, 0, 0, 0, 0, 0, 0,
4425 "kp-multiply", /* 0xffaa */
4426 "kp-add",
4427 "kp-separator",
4428 "kp-subtract",
4429 "kp-decimal",
4430 "kp-divide", /* 0xffaf */
4431 "kp-0", /* 0xffb0 */
4432 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
4433 0, /* 0xffba */
4434 0, 0,
4435 "kp-equal", /* 0xffbd */
4436 "f1", /* 0xffbe */ /* IsFunctionKey */
4437 "f2",
4438 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
4439 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
4440 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
4441 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
4442 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
4443 0, 0, 0, 0, 0, 0, 0, 0,
4444 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
4445 0, 0, 0, 0, 0, 0, 0, "delete"
4446 };
4447
4448 /* ISO 9995 Function and Modifier Keys; the first byte is 0xFE. */
4449 #define ISO_FUNCTION_KEY_OFFSET 0xfe00
4450
4451 static char *iso_lispy_function_keys[] =
4452 {
4453 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe00 */
4454 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe08 */
4455 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe10 */
4456 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfe18 */
4457 "iso-lefttab", /* 0xfe20 */
4458 "iso-move-line-up", "iso-move-line-down",
4459 "iso-partial-line-up", "iso-partial-line-down",
4460 "iso-partial-space-left", "iso-partial-space-right",
4461 "iso-set-margin-left", "iso-set-margin-right", /* 0xffe27, 28 */
4462 "iso-release-margin-left", "iso-release-margin-right",
4463 "iso-release-both-margins",
4464 "iso-fast-cursor-left", "iso-fast-cursor-right",
4465 "iso-fast-cursor-up", "iso-fast-cursor-down",
4466 "iso-continuous-underline", "iso-discontinuous-underline", /* 0xfe30, 31 */
4467 "iso-emphasize", "iso-center-object", "iso-enter", /* ... 0xfe34 */
4468 };
4469
4470 #endif /* not HAVE_NTGUI */
4471
4472 Lisp_Object Vlispy_mouse_stem;
4473
4474 #ifdef WINDOWSNT
4475 /* mouse-wheel events are generated by the wheel on devices such as
4476 the MS Intellimouse. The wheel sits in between the left and right
4477 mouse buttons, and is typically used to scroll or zoom the window
4478 underneath the pointer. mouse-wheel events specify the object on
4479 which they operate, and a delta corresponding to the amount and
4480 direction that the wheel is rotated. Clicking the mouse-wheel
4481 generates a mouse-2 event. */
4482 static char *lispy_mouse_wheel_names[] =
4483 {
4484 "mouse-wheel"
4485 };
4486
4487 #endif /* WINDOWSNT */
4488
4489 /* drag-n-drop events are generated when a set of selected files are
4490 dragged from another application and dropped onto an Emacs window. */
4491 static char *lispy_drag_n_drop_names[] =
4492 {
4493 "drag-n-drop"
4494 };
4495
4496 /* Scroll bar parts. */
4497 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
4498 Lisp_Object Qup, Qdown, Qbottom, Qend_scroll;
4499 Lisp_Object Qtop, Qratio;
4500
4501 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
4502 Lisp_Object *scroll_bar_parts[] = {
4503 &Qabove_handle, &Qhandle, &Qbelow_handle,
4504 &Qup, &Qdown, &Qtop, &Qbottom, &Qend_scroll, &Qratio
4505 };
4506
4507 /* User signal events. */
4508 Lisp_Object Qusr1_signal, Qusr2_signal;
4509
4510 Lisp_Object *lispy_user_signals[] =
4511 {
4512 &Qusr1_signal, &Qusr2_signal
4513 };
4514
4515
4516 /* A vector, indexed by button number, giving the down-going location
4517 of currently depressed buttons, both scroll bar and non-scroll bar.
4518
4519 The elements have the form
4520 (BUTTON-NUMBER MODIFIER-MASK . REST)
4521 where REST is the cdr of a position as it would be reported in the event.
4522
4523 The make_lispy_event function stores positions here to tell the
4524 difference between click and drag events, and to store the starting
4525 location to be included in drag events. */
4526
4527 static Lisp_Object button_down_location;
4528
4529 /* Information about the most recent up-going button event: Which
4530 button, what location, and what time. */
4531
4532 static int last_mouse_button;
4533 static int last_mouse_x;
4534 static int last_mouse_y;
4535 static unsigned long button_down_time;
4536
4537 /* The maximum time between clicks to make a double-click,
4538 or Qnil to disable double-click detection,
4539 or Qt for no time limit. */
4540 Lisp_Object Vdouble_click_time;
4541
4542 /* The number of clicks in this multiple-click. */
4543
4544 int double_click_count;
4545
4546 /* Given a struct input_event, build the lisp event which represents
4547 it. If EVENT is 0, build a mouse movement event from the mouse
4548 movement buffer, which should have a movement event in it.
4549
4550 Note that events must be passed to this function in the order they
4551 are received; this function stores the location of button presses
4552 in order to build drag events when the button is released. */
4553
4554 static Lisp_Object
4555 make_lispy_event (event)
4556 struct input_event *event;
4557 {
4558 int i;
4559
4560 switch (SWITCH_ENUM_CAST (event->kind))
4561 {
4562 /* A simple keystroke. */
4563 case ascii_keystroke:
4564 {
4565 Lisp_Object lispy_c;
4566 int c = event->code & 0377;
4567 /* Turn ASCII characters into control characters
4568 when proper. */
4569 if (event->modifiers & ctrl_modifier)
4570 c = make_ctrl_char (c);
4571
4572 /* Add in the other modifier bits. We took care of ctrl_modifier
4573 just above, and the shift key was taken care of by the X code,
4574 and applied to control characters by make_ctrl_char. */
4575 c |= (event->modifiers
4576 & (meta_modifier | alt_modifier
4577 | hyper_modifier | super_modifier));
4578 /* Distinguish Shift-SPC from SPC. */
4579 if ((event->code & 0377) == 040
4580 && event->modifiers & shift_modifier)
4581 c |= shift_modifier;
4582 button_down_time = 0;
4583 XSETFASTINT (lispy_c, c);
4584 return lispy_c;
4585 }
4586
4587 case multibyte_char_keystroke:
4588 {
4589 Lisp_Object lispy_c;
4590
4591 XSETFASTINT (lispy_c, event->code);
4592 return lispy_c;
4593 }
4594
4595 /* A function key. The symbol may need to have modifier prefixes
4596 tacked onto it. */
4597 case non_ascii_keystroke:
4598 button_down_time = 0;
4599
4600 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
4601 if (event->code == lispy_accent_codes[i])
4602 return modify_event_symbol (i,
4603 event->modifiers,
4604 Qfunction_key, Qnil,
4605 lispy_accent_keys, &accent_key_syms,
4606 (sizeof (lispy_accent_keys)
4607 / sizeof (lispy_accent_keys[0])));
4608
4609 /* Handle system-specific keysyms. */
4610 if (event->code & (1 << 28))
4611 {
4612 /* We need to use an alist rather than a vector as the cache
4613 since we can't make a vector long enuf. */
4614 if (NILP (current_kboard->system_key_syms))
4615 current_kboard->system_key_syms = Fcons (Qnil, Qnil);
4616 return modify_event_symbol (event->code,
4617 event->modifiers,
4618 Qfunction_key,
4619 current_kboard->Vsystem_key_alist,
4620 0, &current_kboard->system_key_syms,
4621 (unsigned)-1);
4622 }
4623
4624 #ifdef XK_kana_A
4625 if (event->code >= 0x400 && event->code < 0x500)
4626 return modify_event_symbol (event->code - 0x400,
4627 event->modifiers & ~shift_modifier,
4628 Qfunction_key, Qnil,
4629 lispy_kana_keys, &func_key_syms,
4630 (sizeof (lispy_kana_keys)
4631 / sizeof (lispy_kana_keys[0])));
4632 #endif /* XK_kana_A */
4633
4634 #ifdef ISO_FUNCTION_KEY_OFFSET
4635 if (event->code < FUNCTION_KEY_OFFSET
4636 && event->code >= ISO_FUNCTION_KEY_OFFSET)
4637 return modify_event_symbol (event->code - ISO_FUNCTION_KEY_OFFSET,
4638 event->modifiers,
4639 Qfunction_key, Qnil,
4640 iso_lispy_function_keys, &func_key_syms,
4641 (sizeof (iso_lispy_function_keys)
4642 / sizeof (iso_lispy_function_keys[0])));
4643 else
4644 #endif
4645 return modify_event_symbol (event->code - FUNCTION_KEY_OFFSET,
4646 event->modifiers,
4647 Qfunction_key, Qnil,
4648 lispy_function_keys, &func_key_syms,
4649 (sizeof (lispy_function_keys)
4650 / sizeof (lispy_function_keys[0])));
4651
4652 #ifdef HAVE_MOUSE
4653 /* A mouse click. Figure out where it is, decide whether it's
4654 a press, click or drag, and build the appropriate structure. */
4655 case mouse_click:
4656 #ifndef USE_TOOLKIT_SCROLL_BARS
4657 case scroll_bar_click:
4658 #endif
4659 {
4660 int button = event->code;
4661 int is_double;
4662 Lisp_Object position;
4663 Lisp_Object *start_pos_ptr;
4664 Lisp_Object start_pos;
4665
4666 position = Qnil;
4667
4668 /* Build the position as appropriate for this mouse click. */
4669 if (event->kind == mouse_click)
4670 {
4671 int part;
4672 FRAME_PTR f = XFRAME (event->frame_or_window);
4673 Lisp_Object window;
4674 Lisp_Object posn;
4675 Lisp_Object string_info = Qnil;
4676 int row, column;
4677
4678 /* Ignore mouse events that were made on frame that
4679 have been deleted. */
4680 if (! FRAME_LIVE_P (f))
4681 return Qnil;
4682
4683 /* EVENT->x and EVENT->y are frame-relative pixel
4684 coordinates at this place. Under old redisplay, COLUMN
4685 and ROW are set to frame relative glyph coordinates
4686 which are then used to determine whether this click is
4687 in a menu (non-toolkit version). */
4688 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
4689 &column, &row, NULL, 1);
4690
4691 #ifndef USE_X_TOOLKIT
4692 /* In the non-toolkit version, clicks on the menu bar
4693 are ordinary button events in the event buffer.
4694 Distinguish them, and invoke the menu.
4695
4696 (In the toolkit version, the toolkit handles the menu bar
4697 and Emacs doesn't know about it until after the user
4698 makes a selection.) */
4699 if (row >= 0 && row < FRAME_MENU_BAR_LINES (f)
4700 && (event->modifiers & down_modifier))
4701 {
4702 Lisp_Object items, item;
4703 int hpos;
4704 int i;
4705
4706 #if 0
4707 /* Activate the menu bar on the down event. If the
4708 up event comes in before the menu code can deal with it,
4709 just ignore it. */
4710 if (! (event->modifiers & down_modifier))
4711 return Qnil;
4712 #endif
4713
4714 /* Find the menu bar item under `column'. */
4715 item = Qnil;
4716 items = FRAME_MENU_BAR_ITEMS (f);
4717 for (i = 0; i < XVECTOR (items)->size; i += 4)
4718 {
4719 Lisp_Object pos, string;
4720 string = AREF (items, i + 1);
4721 pos = AREF (items, i + 3);
4722 if (NILP (string))
4723 break;
4724 if (column >= XINT (pos)
4725 && column < XINT (pos) + XSTRING (string)->size)
4726 {
4727 item = AREF (items, i);
4728 break;
4729 }
4730 }
4731
4732 /* ELisp manual 2.4b says (x y) are window relative but
4733 code says they are frame-relative. */
4734 position
4735 = Fcons (event->frame_or_window,
4736 Fcons (Qmenu_bar,
4737 Fcons (Fcons (event->x, event->y),
4738 Fcons (make_number (event->timestamp),
4739 Qnil))));
4740
4741 return Fcons (item, Fcons (position, Qnil));
4742 }
4743 #endif /* not USE_X_TOOLKIT */
4744
4745 /* Set `window' to the window under frame pixel coordinates
4746 event->x/event->y. */
4747 window = window_from_coordinates (f, XINT (event->x),
4748 XINT (event->y), &part, 0);
4749
4750 if (!WINDOWP (window))
4751 {
4752 window = event->frame_or_window;
4753 posn = Qnil;
4754 }
4755 else
4756 {
4757 /* It's a click in window window at frame coordinates
4758 event->x/ event->y. */
4759 struct window *w = XWINDOW (window);
4760
4761 /* Get window relative coordinates. Original code
4762 `rounded' this to glyph boundaries. */
4763 int wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (event->x));
4764 int wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (event->y));
4765
4766 /* Set event coordinates to window-relative coordinates
4767 for constructing the Lisp event below. */
4768 XSETINT (event->x, wx);
4769 XSETINT (event->y, wy);
4770
4771 if (part == 1 || part == 3)
4772 {
4773 /* Mode line or header line. Look for a string under
4774 the mouse that may have a `local-map' property. */
4775 Lisp_Object string;
4776 int charpos;
4777
4778 posn = part == 1 ? Qmode_line : Qheader_line;
4779 string = mode_line_string (w, wx, wy, part == 1, &charpos);
4780 if (STRINGP (string))
4781 string_info = Fcons (string, make_number (charpos));
4782 }
4783 else if (part == 2)
4784 posn = Qvertical_line;
4785 else
4786 {
4787 Lisp_Object object;
4788 struct display_pos p;
4789 buffer_posn_from_coords (w, &wx, &wy, &object, &p);
4790 posn = make_number (CHARPOS (p.pos));
4791 if (STRINGP (object))
4792 string_info
4793 = Fcons (object,
4794 make_number (CHARPOS (p.string_pos)));
4795 }
4796 }
4797
4798 position
4799 = Fcons (window,
4800 Fcons (posn,
4801 Fcons (Fcons (event->x, event->y),
4802 Fcons (make_number (event->timestamp),
4803 (NILP (string_info)
4804 ? Qnil
4805 : Fcons (string_info, Qnil))))));
4806 }
4807 #ifndef USE_TOOLKIT_SCROLL_BARS
4808 else
4809 {
4810 /* It's a scrollbar click. */
4811 Lisp_Object window;
4812 Lisp_Object portion_whole;
4813 Lisp_Object part;
4814
4815 window = event->frame_or_window;
4816 portion_whole = Fcons (event->x, event->y);
4817 part = *scroll_bar_parts[(int) event->part];
4818
4819 position
4820 = Fcons (window,
4821 Fcons (Qvertical_scroll_bar,
4822 Fcons (portion_whole,
4823 Fcons (make_number (event->timestamp),
4824 Fcons (part, Qnil)))));
4825 }
4826 #endif /* not USE_TOOLKIT_SCROLL_BARS */
4827
4828 if (button >= ASIZE (button_down_location))
4829 {
4830 button_down_location = larger_vector (button_down_location,
4831 button + 1, Qnil);
4832 mouse_syms = larger_vector (mouse_syms, button + 1, Qnil);
4833 }
4834
4835 start_pos_ptr = &AREF (button_down_location, button);
4836 start_pos = *start_pos_ptr;
4837 *start_pos_ptr = Qnil;
4838
4839 is_double = (button == last_mouse_button
4840 && XINT (event->x) == last_mouse_x
4841 && XINT (event->y) == last_mouse_y
4842 && button_down_time != 0
4843 && (EQ (Vdouble_click_time, Qt)
4844 || (INTEGERP (Vdouble_click_time)
4845 && ((int)(event->timestamp - button_down_time)
4846 < XINT (Vdouble_click_time)))));
4847 last_mouse_button = button;
4848 last_mouse_x = XINT (event->x);
4849 last_mouse_y = XINT (event->y);
4850
4851 /* If this is a button press, squirrel away the location, so
4852 we can decide later whether it was a click or a drag. */
4853 if (event->modifiers & down_modifier)
4854 {
4855 if (is_double)
4856 {
4857 double_click_count++;
4858 event->modifiers |= ((double_click_count > 2)
4859 ? triple_modifier
4860 : double_modifier);
4861 }
4862 else
4863 double_click_count = 1;
4864 button_down_time = event->timestamp;
4865 *start_pos_ptr = Fcopy_alist (position);
4866 }
4867
4868 /* Now we're releasing a button - check the co-ordinates to
4869 see if this was a click or a drag. */
4870 else if (event->modifiers & up_modifier)
4871 {
4872 /* If we did not see a down before this up, ignore the up.
4873 Probably this happened because the down event chose a
4874 menu item. It would be an annoyance to treat the
4875 release of the button that chose the menu item as a
4876 separate event. */
4877
4878 if (!CONSP (start_pos))
4879 return Qnil;
4880
4881 event->modifiers &= ~up_modifier;
4882 #if 0 /* Formerly we treated an up with no down as a click event. */
4883 if (!CONSP (start_pos))
4884 event->modifiers |= click_modifier;
4885 else
4886 #endif
4887 {
4888 /* The third element of every position should be the (x,y)
4889 pair. */
4890 Lisp_Object down;
4891
4892 down = Fnth (make_number (2), start_pos);
4893 if (EQ (event->x, XCAR (down)) && EQ (event->y, XCDR (down)))
4894 /* Mouse hasn't moved. */
4895 event->modifiers |= click_modifier;
4896 else
4897 {
4898 Lisp_Object window1, window2, posn1, posn2;
4899
4900 /* Avoid generating a drag event if the mouse
4901 hasn't actually moved off the buffer position. */
4902 window1 = Fnth (make_number (0), position);
4903 posn1 = Fnth (make_number (1), position);
4904 window2 = Fnth (make_number (0), start_pos);
4905 posn2 = Fnth (make_number (1), start_pos);
4906
4907 if (EQ (window1, window2) && EQ (posn1, posn2))
4908 event->modifiers |= click_modifier;
4909 else
4910 {
4911 button_down_time = 0;
4912 event->modifiers |= drag_modifier;
4913 }
4914 }
4915
4916 /* Don't check is_double; treat this as multiple
4917 if the down-event was multiple. */
4918 if (double_click_count > 1)
4919 event->modifiers |= ((double_click_count > 2)
4920 ? triple_modifier
4921 : double_modifier);
4922 }
4923 }
4924 else
4925 /* Every mouse event should either have the down_modifier or
4926 the up_modifier set. */
4927 abort ();
4928
4929 {
4930 /* Get the symbol we should use for the mouse click. */
4931 Lisp_Object head;
4932
4933 head = modify_event_symbol (button,
4934 event->modifiers,
4935 Qmouse_click, Vlispy_mouse_stem,
4936 NULL,
4937 &mouse_syms,
4938 XVECTOR (mouse_syms)->size);
4939 if (event->modifiers & drag_modifier)
4940 return Fcons (head,
4941 Fcons (start_pos,
4942 Fcons (position,
4943 Qnil)));
4944 else if (event->modifiers & (double_modifier | triple_modifier))
4945 return Fcons (head,
4946 Fcons (position,
4947 Fcons (make_number (double_click_count),
4948 Qnil)));
4949 else
4950 return Fcons (head,
4951 Fcons (position,
4952 Qnil));
4953 }
4954 }
4955
4956 #if USE_TOOLKIT_SCROLL_BARS
4957
4958 /* We don't have down and up events if using toolkit scroll bars,
4959 so make this always a click event. Store in the `part' of
4960 the Lisp event a symbol which maps to the following actions:
4961
4962 `above_handle' page up
4963 `below_handle' page down
4964 `up' line up
4965 `down' line down
4966 `top' top of buffer
4967 `bottom' bottom of buffer
4968 `handle' thumb has been dragged.
4969 `end-scroll' end of interaction with scroll bar
4970
4971 The incoming input_event contains in its `part' member an
4972 index of type `enum scroll_bar_part' which we can use as an
4973 index in scroll_bar_parts to get the appropriate symbol. */
4974
4975 case scroll_bar_click:
4976 {
4977 Lisp_Object position, head, window, portion_whole, part;
4978
4979 window = event->frame_or_window;
4980 portion_whole = Fcons (event->x, event->y);
4981 part = *scroll_bar_parts[(int) event->part];
4982
4983 position
4984 = Fcons (window,
4985 Fcons (Qvertical_scroll_bar,
4986 Fcons (portion_whole,
4987 Fcons (make_number (event->timestamp),
4988 Fcons (part, Qnil)))));
4989
4990 /* Always treat scroll bar events as clicks. */
4991 event->modifiers |= click_modifier;
4992
4993 /* Get the symbol we should use for the mouse click. */
4994 head = modify_event_symbol (event->code,
4995 event->modifiers,
4996 Qmouse_click,
4997 Vlispy_mouse_stem,
4998 NULL, &mouse_syms,
4999 XVECTOR (mouse_syms)->size);
5000 return Fcons (head, Fcons (position, Qnil));
5001 }
5002
5003 #endif /* USE_TOOLKIT_SCROLL_BARS */
5004
5005 #ifdef WINDOWSNT
5006 case w32_scroll_bar_click:
5007 {
5008 int button = event->code;
5009 int is_double;
5010 Lisp_Object position;
5011 Lisp_Object *start_pos_ptr;
5012 Lisp_Object start_pos;
5013
5014 {
5015 Lisp_Object window;
5016 Lisp_Object portion_whole;
5017 Lisp_Object part;
5018
5019 window = event->frame_or_window;
5020 portion_whole = Fcons (event->x, event->y);
5021 part = *scroll_bar_parts[(int) event->part];
5022
5023 position
5024 = Fcons (window,
5025 Fcons (Qvertical_scroll_bar,
5026 Fcons (portion_whole,
5027 Fcons (make_number (event->timestamp),
5028 Fcons (part, Qnil)))));
5029 }
5030
5031 /* Always treat W32 scroll bar events as clicks. */
5032 event->modifiers |= click_modifier;
5033
5034 {
5035 /* Get the symbol we should use for the mouse click. */
5036 Lisp_Object head;
5037
5038 head = modify_event_symbol (button,
5039 event->modifiers,
5040 Qmouse_click,
5041 Vlispy_mouse_stem,
5042 NULL, &mouse_syms,
5043 XVECTOR (mouse_syms)->size);
5044 return Fcons (head,
5045 Fcons (position,
5046 Qnil));
5047 }
5048 }
5049 case mouse_wheel:
5050 {
5051 int part;
5052 FRAME_PTR f = XFRAME (event->frame_or_window);
5053 Lisp_Object window;
5054 Lisp_Object posn;
5055 Lisp_Object head, position;
5056 int row, column;
5057
5058 /* Ignore mouse events that were made on frame that
5059 have been deleted. */
5060 if (! FRAME_LIVE_P (f))
5061 return Qnil;
5062 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
5063 &column, &row, NULL, 1);
5064 window = window_from_coordinates (f, XINT (event->x),
5065 XINT (event->y), &part, 0);
5066
5067 if (!WINDOWP (window))
5068 {
5069 window = event->frame_or_window;
5070 posn = Qnil;
5071 }
5072 else
5073 {
5074 int pixcolumn, pixrow;
5075 column -= XINT (XWINDOW (window)->left);
5076 row -= XINT (XWINDOW (window)->top);
5077 glyph_to_pixel_coords (XWINDOW(window), column, row,
5078 &pixcolumn, &pixrow);
5079 XSETINT (event->x, pixcolumn);
5080 XSETINT (event->y, pixrow);
5081
5082 if (part == 1)
5083 posn = Qmode_line;
5084 else if (part == 2)
5085 posn = Qvertical_line;
5086 else if (part == 3)
5087 posn = Qheader_line;
5088 else
5089 {
5090 Lisp_Object object;
5091 struct display_pos p;
5092 buffer_posn_from_coords (XWINDOW (window), &column, &row,
5093 &object, &p);
5094 posn = make_number (CHARPOS (p.pos));
5095 }
5096 }
5097
5098 {
5099 Lisp_Object head, position;
5100
5101 position
5102 = Fcons (window,
5103 Fcons (posn,
5104 Fcons (Fcons (event->x, event->y),
5105 Fcons (make_number (event->timestamp),
5106 Qnil))));
5107
5108 head = modify_event_symbol (0, event->modifiers,
5109 Qmouse_wheel, Qnil,
5110 lispy_mouse_wheel_names,
5111 &mouse_wheel_syms, 1);
5112 return Fcons (head,
5113 Fcons (position,
5114 Fcons (make_number (event->code),
5115 Qnil)));
5116 }
5117 }
5118 #endif /* WINDOWSNT */
5119
5120 case drag_n_drop:
5121 {
5122 int part;
5123 FRAME_PTR f;
5124 Lisp_Object window;
5125 Lisp_Object posn;
5126 Lisp_Object files;
5127
5128 /* The frame_or_window field should be a cons of the frame in
5129 which the event occurred and a list of the filenames
5130 dropped. */
5131 if (! CONSP (event->frame_or_window))
5132 abort ();
5133
5134 f = XFRAME (XCAR (event->frame_or_window));
5135 files = XCDR (event->frame_or_window);
5136
5137 /* Ignore mouse events that were made on frames that
5138 have been deleted. */
5139 if (! FRAME_LIVE_P (f))
5140 return Qnil;
5141
5142 window = window_from_coordinates (f, XINT (event->x),
5143 XINT (event->y), &part, 0);
5144
5145 if (!WINDOWP (window))
5146 {
5147 window = XCAR (event->frame_or_window);
5148 posn = Qnil;
5149 }
5150 else
5151 {
5152 /* It's an event in window `window' at frame coordinates
5153 event->x/ event->y. */
5154 struct window *w = XWINDOW (window);
5155
5156 /* Get window relative coordinates. */
5157 int wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (event->x));
5158 int wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (event->y));
5159
5160 /* Set event coordinates to window-relative coordinates
5161 for constructing the Lisp event below. */
5162 XSETINT (event->x, wx);
5163 XSETINT (event->y, wy);
5164
5165 if (part == 1)
5166 posn = Qmode_line;
5167 else if (part == 2)
5168 posn = Qvertical_line;
5169 else if (part == 3)
5170 posn = Qheader_line;
5171 else
5172 {
5173 Lisp_Object object;
5174 struct display_pos p;
5175 buffer_posn_from_coords (w, &wx, &wy, &object, &p);
5176 posn = make_number (CHARPOS (p.pos));
5177 }
5178 }
5179
5180 {
5181 Lisp_Object head, position;
5182
5183 position
5184 = Fcons (window,
5185 Fcons (posn,
5186 Fcons (Fcons (event->x, event->y),
5187 Fcons (make_number (event->timestamp),
5188 Qnil))));
5189
5190 head = modify_event_symbol (0, event->modifiers,
5191 Qdrag_n_drop, Qnil,
5192 lispy_drag_n_drop_names,
5193 &drag_n_drop_syms, 1);
5194 return Fcons (head,
5195 Fcons (position,
5196 Fcons (files,
5197 Qnil)));
5198 }
5199 }
5200 #endif /* HAVE_MOUSE */
5201
5202 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (macintosh)
5203 case MENU_BAR_EVENT:
5204 if (EQ (event->arg, event->frame_or_window))
5205 /* This is the prefix key. We translate this to
5206 `(menu_bar)' because the code in keyboard.c for menu
5207 events, which we use, relies on this. */
5208 return Fcons (Qmenu_bar, Qnil);
5209 return event->arg;
5210 #endif
5211
5212 case TOOL_BAR_EVENT:
5213 if (EQ (event->arg, event->frame_or_window))
5214 /* This is the prefix key. We translate this to
5215 `(tool_bar)' because the code in keyboard.c for menu
5216 events, which we use, relies on this. */
5217 return Fcons (Qtool_bar, Qnil);
5218 else if (SYMBOLP (event->arg))
5219 return apply_modifiers (event->modifiers, event->arg);
5220 return event->arg;
5221
5222 case USER_SIGNAL_EVENT:
5223 /* A user signal. */
5224 return *lispy_user_signals[event->code];
5225
5226 /* The 'kind' field of the event is something we don't recognize. */
5227 default:
5228 abort ();
5229 }
5230 }
5231
5232 #ifdef HAVE_MOUSE
5233
5234 static Lisp_Object
5235 make_lispy_movement (frame, bar_window, part, x, y, time)
5236 FRAME_PTR frame;
5237 Lisp_Object bar_window;
5238 enum scroll_bar_part part;
5239 Lisp_Object x, y;
5240 unsigned long time;
5241 {
5242 /* Is it a scroll bar movement? */
5243 if (frame && ! NILP (bar_window))
5244 {
5245 Lisp_Object part_sym;
5246
5247 part_sym = *scroll_bar_parts[(int) part];
5248 return Fcons (Qscroll_bar_movement,
5249 (Fcons (Fcons (bar_window,
5250 Fcons (Qvertical_scroll_bar,
5251 Fcons (Fcons (x, y),
5252 Fcons (make_number (time),
5253 Fcons (part_sym,
5254 Qnil))))),
5255 Qnil)));
5256 }
5257
5258 /* Or is it an ordinary mouse movement? */
5259 else
5260 {
5261 int area;
5262 Lisp_Object window;
5263 Lisp_Object posn;
5264
5265 if (frame)
5266 /* It's in a frame; which window on that frame? */
5267 window = window_from_coordinates (frame, XINT (x), XINT (y), &area, 0);
5268 else
5269 window = Qnil;
5270
5271 if (WINDOWP (window))
5272 {
5273 struct window *w = XWINDOW (window);
5274 int wx, wy;
5275
5276 /* Get window relative coordinates. */
5277 wx = FRAME_TO_WINDOW_PIXEL_X (w, XINT (x));
5278 wy = FRAME_TO_WINDOW_PIXEL_Y (w, XINT (y));
5279 XSETINT (x, wx);
5280 XSETINT (y, wy);
5281
5282 if (area == 1)
5283 posn = Qmode_line;
5284 else if (area == 2)
5285 posn = Qvertical_line;
5286 else if (area == 3)
5287 posn = Qheader_line;
5288 else
5289 {
5290 Lisp_Object object;
5291 struct display_pos p;
5292 buffer_posn_from_coords (w, &wx, &wy, &object, &p);
5293 posn = make_number (CHARPOS (p.pos));
5294 }
5295 }
5296 else if (frame != 0)
5297 {
5298 XSETFRAME (window, frame);
5299 posn = Qnil;
5300 }
5301 else
5302 {
5303 window = Qnil;
5304 posn = Qnil;
5305 XSETFASTINT (x, 0);
5306 XSETFASTINT (y, 0);
5307 }
5308
5309 return Fcons (Qmouse_movement,
5310 Fcons (Fcons (window,
5311 Fcons (posn,
5312 Fcons (Fcons (x, y),
5313 Fcons (make_number (time),
5314 Qnil)))),
5315 Qnil));
5316 }
5317 }
5318
5319 #endif /* HAVE_MOUSE */
5320
5321 /* Construct a switch frame event. */
5322 static Lisp_Object
5323 make_lispy_switch_frame (frame)
5324 Lisp_Object frame;
5325 {
5326 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
5327 }
5328 \f
5329 /* Manipulating modifiers. */
5330
5331 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
5332
5333 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
5334 SYMBOL's name of the end of the modifiers; the string from this
5335 position is the unmodified symbol name.
5336
5337 This doesn't use any caches. */
5338
5339 static int
5340 parse_modifiers_uncached (symbol, modifier_end)
5341 Lisp_Object symbol;
5342 int *modifier_end;
5343 {
5344 struct Lisp_String *name;
5345 int i;
5346 int modifiers;
5347
5348 CHECK_SYMBOL (symbol, 1);
5349
5350 modifiers = 0;
5351 name = XSYMBOL (symbol)->name;
5352
5353 for (i = 0; i+2 <= STRING_BYTES (name); )
5354 {
5355 int this_mod_end = 0;
5356 int this_mod = 0;
5357
5358 /* See if the name continues with a modifier word.
5359 Check that the word appears, but don't check what follows it.
5360 Set this_mod and this_mod_end to record what we find. */
5361
5362 switch (name->data[i])
5363 {
5364 #define SINGLE_LETTER_MOD(BIT) \
5365 (this_mod_end = i + 1, this_mod = BIT)
5366
5367 case 'A':
5368 SINGLE_LETTER_MOD (alt_modifier);
5369 break;
5370
5371 case 'C':
5372 SINGLE_LETTER_MOD (ctrl_modifier);
5373 break;
5374
5375 case 'H':
5376 SINGLE_LETTER_MOD (hyper_modifier);
5377 break;
5378
5379 case 'M':
5380 SINGLE_LETTER_MOD (meta_modifier);
5381 break;
5382
5383 case 'S':
5384 SINGLE_LETTER_MOD (shift_modifier);
5385 break;
5386
5387 case 's':
5388 SINGLE_LETTER_MOD (super_modifier);
5389 break;
5390
5391 #undef SINGLE_LETTER_MOD
5392 }
5393
5394 /* If we found no modifier, stop looking for them. */
5395 if (this_mod_end == 0)
5396 break;
5397
5398 /* Check there is a dash after the modifier, so that it
5399 really is a modifier. */
5400 if (this_mod_end >= STRING_BYTES (name)
5401 || name->data[this_mod_end] != '-')
5402 break;
5403
5404 /* This modifier is real; look for another. */
5405 modifiers |= this_mod;
5406 i = this_mod_end + 1;
5407 }
5408
5409 /* Should we include the `click' modifier? */
5410 if (! (modifiers & (down_modifier | drag_modifier
5411 | double_modifier | triple_modifier))
5412 && i + 7 == STRING_BYTES (name)
5413 && strncmp (name->data + i, "mouse-", 6) == 0
5414 && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
5415 modifiers |= click_modifier;
5416
5417 if (modifier_end)
5418 *modifier_end = i;
5419
5420 return modifiers;
5421 }
5422
5423 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
5424 prepended to the string BASE[0..BASE_LEN-1].
5425 This doesn't use any caches. */
5426 static Lisp_Object
5427 apply_modifiers_uncached (modifiers, base, base_len, base_len_byte)
5428 int modifiers;
5429 char *base;
5430 int base_len, base_len_byte;
5431 {
5432 /* Since BASE could contain nulls, we can't use intern here; we have
5433 to use Fintern, which expects a genuine Lisp_String, and keeps a
5434 reference to it. */
5435 char *new_mods
5436 = (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
5437 int mod_len;
5438
5439 {
5440 char *p = new_mods;
5441
5442 /* Only the event queue may use the `up' modifier; it should always
5443 be turned into a click or drag event before presented to lisp code. */
5444 if (modifiers & up_modifier)
5445 abort ();
5446
5447 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
5448 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
5449 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
5450 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
5451 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
5452 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
5453 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
5454 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
5455 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
5456 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
5457 /* The click modifier is denoted by the absence of other modifiers. */
5458
5459 *p = '\0';
5460
5461 mod_len = p - new_mods;
5462 }
5463
5464 {
5465 Lisp_Object new_name;
5466
5467 new_name = make_uninit_multibyte_string (mod_len + base_len,
5468 mod_len + base_len_byte);
5469 bcopy (new_mods, XSTRING (new_name)->data, mod_len);
5470 bcopy (base, XSTRING (new_name)->data + mod_len, base_len_byte);
5471
5472 return Fintern (new_name, Qnil);
5473 }
5474 }
5475
5476
5477 static char *modifier_names[] =
5478 {
5479 "up", "down", "drag", "click", "double", "triple", 0, 0,
5480 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5481 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
5482 };
5483 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
5484
5485 static Lisp_Object modifier_symbols;
5486
5487 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
5488 static Lisp_Object
5489 lispy_modifier_list (modifiers)
5490 int modifiers;
5491 {
5492 Lisp_Object modifier_list;
5493 int i;
5494
5495 modifier_list = Qnil;
5496 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
5497 if (modifiers & (1<<i))
5498 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
5499 modifier_list);
5500
5501 return modifier_list;
5502 }
5503
5504
5505 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
5506 where UNMODIFIED is the unmodified form of SYMBOL,
5507 MASK is the set of modifiers present in SYMBOL's name.
5508 This is similar to parse_modifiers_uncached, but uses the cache in
5509 SYMBOL's Qevent_symbol_element_mask property, and maintains the
5510 Qevent_symbol_elements property. */
5511
5512 Lisp_Object
5513 parse_modifiers (symbol)
5514 Lisp_Object symbol;
5515 {
5516 Lisp_Object elements;
5517
5518 elements = Fget (symbol, Qevent_symbol_element_mask);
5519 if (CONSP (elements))
5520 return elements;
5521 else
5522 {
5523 int end;
5524 int modifiers = parse_modifiers_uncached (symbol, &end);
5525 Lisp_Object unmodified;
5526 Lisp_Object mask;
5527
5528 unmodified = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
5529 STRING_BYTES (XSYMBOL (symbol)->name) - end),
5530 Qnil);
5531
5532 if (modifiers & ~(((EMACS_INT)1 << VALBITS) - 1))
5533 abort ();
5534 XSETFASTINT (mask, modifiers);
5535 elements = Fcons (unmodified, Fcons (mask, Qnil));
5536
5537 /* Cache the parsing results on SYMBOL. */
5538 Fput (symbol, Qevent_symbol_element_mask,
5539 elements);
5540 Fput (symbol, Qevent_symbol_elements,
5541 Fcons (unmodified, lispy_modifier_list (modifiers)));
5542
5543 /* Since we know that SYMBOL is modifiers applied to unmodified,
5544 it would be nice to put that in unmodified's cache.
5545 But we can't, since we're not sure that parse_modifiers is
5546 canonical. */
5547
5548 return elements;
5549 }
5550 }
5551
5552 /* Apply the modifiers MODIFIERS to the symbol BASE.
5553 BASE must be unmodified.
5554
5555 This is like apply_modifiers_uncached, but uses BASE's
5556 Qmodifier_cache property, if present. It also builds
5557 Qevent_symbol_elements properties, since it has that info anyway.
5558
5559 apply_modifiers copies the value of BASE's Qevent_kind property to
5560 the modified symbol. */
5561 static Lisp_Object
5562 apply_modifiers (modifiers, base)
5563 int modifiers;
5564 Lisp_Object base;
5565 {
5566 Lisp_Object cache, index, entry, new_symbol;
5567
5568 /* Mask out upper bits. We don't know where this value's been. */
5569 modifiers &= ((EMACS_INT)1 << VALBITS) - 1;
5570
5571 /* The click modifier never figures into cache indices. */
5572 cache = Fget (base, Qmodifier_cache);
5573 XSETFASTINT (index, (modifiers & ~click_modifier));
5574 entry = assq_no_quit (index, cache);
5575
5576 if (CONSP (entry))
5577 new_symbol = XCDR (entry);
5578 else
5579 {
5580 /* We have to create the symbol ourselves. */
5581 new_symbol = apply_modifiers_uncached (modifiers,
5582 XSYMBOL (base)->name->data,
5583 XSYMBOL (base)->name->size,
5584 STRING_BYTES (XSYMBOL (base)->name));
5585
5586 /* Add the new symbol to the base's cache. */
5587 entry = Fcons (index, new_symbol);
5588 Fput (base, Qmodifier_cache, Fcons (entry, cache));
5589
5590 /* We have the parsing info now for free, so add it to the caches. */
5591 XSETFASTINT (index, modifiers);
5592 Fput (new_symbol, Qevent_symbol_element_mask,
5593 Fcons (base, Fcons (index, Qnil)));
5594 Fput (new_symbol, Qevent_symbol_elements,
5595 Fcons (base, lispy_modifier_list (modifiers)));
5596 }
5597
5598 /* Make sure this symbol is of the same kind as BASE.
5599
5600 You'd think we could just set this once and for all when we
5601 intern the symbol above, but reorder_modifiers may call us when
5602 BASE's property isn't set right; we can't assume that just
5603 because it has a Qmodifier_cache property it must have its
5604 Qevent_kind set right as well. */
5605 if (NILP (Fget (new_symbol, Qevent_kind)))
5606 {
5607 Lisp_Object kind;
5608
5609 kind = Fget (base, Qevent_kind);
5610 if (! NILP (kind))
5611 Fput (new_symbol, Qevent_kind, kind);
5612 }
5613
5614 return new_symbol;
5615 }
5616
5617
5618 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
5619 return a symbol with the modifiers placed in the canonical order.
5620 Canonical order is alphabetical, except for down and drag, which
5621 always come last. The 'click' modifier is never written out.
5622
5623 Fdefine_key calls this to make sure that (for example) C-M-foo
5624 and M-C-foo end up being equivalent in the keymap. */
5625
5626 Lisp_Object
5627 reorder_modifiers (symbol)
5628 Lisp_Object symbol;
5629 {
5630 /* It's hopefully okay to write the code this way, since everything
5631 will soon be in caches, and no consing will be done at all. */
5632 Lisp_Object parsed;
5633
5634 parsed = parse_modifiers (symbol);
5635 return apply_modifiers ((int) XINT (XCAR (XCDR (parsed))),
5636 XCAR (parsed));
5637 }
5638
5639
5640 /* For handling events, we often want to produce a symbol whose name
5641 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
5642 to some base, like the name of a function key or mouse button.
5643 modify_event_symbol produces symbols of this sort.
5644
5645 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
5646 is the name of the i'th symbol. TABLE_SIZE is the number of elements
5647 in the table.
5648
5649 Alternatively, NAME_ALIST_OR_STEM is either an alist mapping codes
5650 into symbol names, or a string specifying a name stem used to
5651 construct a symbol name or the form `STEM-N', where N is the decimal
5652 representation of SYMBOL_NUM. NAME_ALIST_OR_STEM is used if it is
5653 non-nil; otherwise NAME_TABLE is used.
5654
5655 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
5656 persist between calls to modify_event_symbol that it can use to
5657 store a cache of the symbols it's generated for this NAME_TABLE
5658 before. The object stored there may be a vector or an alist.
5659
5660 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
5661
5662 MODIFIERS is a set of modifier bits (as given in struct input_events)
5663 whose prefixes should be applied to the symbol name.
5664
5665 SYMBOL_KIND is the value to be placed in the event_kind property of
5666 the returned symbol.
5667
5668 The symbols we create are supposed to have an
5669 `event-symbol-elements' property, which lists the modifiers present
5670 in the symbol's name. */
5671
5672 static Lisp_Object
5673 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist_or_stem,
5674 name_table, symbol_table, table_size)
5675 int symbol_num;
5676 unsigned modifiers;
5677 Lisp_Object symbol_kind;
5678 Lisp_Object name_alist_or_stem;
5679 char **name_table;
5680 Lisp_Object *symbol_table;
5681 unsigned int table_size;
5682 {
5683 Lisp_Object value;
5684 Lisp_Object symbol_int;
5685
5686 /* Get rid of the "vendor-specific" bit here. */
5687 XSETINT (symbol_int, symbol_num & 0xffffff);
5688
5689 /* Is this a request for a valid symbol? */
5690 if (symbol_num < 0 || symbol_num >= table_size)
5691 return Qnil;
5692
5693 if (CONSP (*symbol_table))
5694 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
5695
5696 /* If *symbol_table doesn't seem to be initialized properly, fix that.
5697 *symbol_table should be a lisp vector TABLE_SIZE elements long,
5698 where the Nth element is the symbol for NAME_TABLE[N], or nil if
5699 we've never used that symbol before. */
5700 else
5701 {
5702 if (! VECTORP (*symbol_table)
5703 || XVECTOR (*symbol_table)->size != table_size)
5704 {
5705 Lisp_Object size;
5706
5707 XSETFASTINT (size, table_size);
5708 *symbol_table = Fmake_vector (size, Qnil);
5709 }
5710
5711 value = XVECTOR (*symbol_table)->contents[symbol_num];
5712 }
5713
5714 /* Have we already used this symbol before? */
5715 if (NILP (value))
5716 {
5717 /* No; let's create it. */
5718 if (CONSP (name_alist_or_stem))
5719 value = Fcdr_safe (Fassq (symbol_int, name_alist_or_stem));
5720 else if (STRINGP (name_alist_or_stem))
5721 {
5722 int len = STRING_BYTES (XSTRING (name_alist_or_stem));
5723 char *buf = (char *) alloca (len + 50);
5724 sprintf (buf, "%s-%d", XSTRING (name_alist_or_stem)->data,
5725 XINT (symbol_int) + 1);
5726 value = intern (buf);
5727 }
5728 else if (name_table != 0 && name_table[symbol_num])
5729 value = intern (name_table[symbol_num]);
5730
5731 #ifdef HAVE_WINDOW_SYSTEM
5732 if (NILP (value))
5733 {
5734 char *name = x_get_keysym_name (symbol_num);
5735 if (name)
5736 value = intern (name);
5737 }
5738 #endif
5739
5740 if (NILP (value))
5741 {
5742 char buf[20];
5743 sprintf (buf, "key-%d", symbol_num);
5744 value = intern (buf);
5745 }
5746
5747 if (CONSP (*symbol_table))
5748 *symbol_table = Fcons (Fcons (symbol_int, value), *symbol_table);
5749 else
5750 XVECTOR (*symbol_table)->contents[symbol_num] = value;
5751
5752 /* Fill in the cache entries for this symbol; this also
5753 builds the Qevent_symbol_elements property, which the user
5754 cares about. */
5755 apply_modifiers (modifiers & click_modifier, value);
5756 Fput (value, Qevent_kind, symbol_kind);
5757 }
5758
5759 /* Apply modifiers to that symbol. */
5760 return apply_modifiers (modifiers, value);
5761 }
5762 \f
5763 /* Convert a list that represents an event type,
5764 such as (ctrl meta backspace), into the usual representation of that
5765 event type as a number or a symbol. */
5766
5767 DEFUN ("event-convert-list", Fevent_convert_list, Sevent_convert_list, 1, 1, 0,
5768 "Convert the event description list EVENT-DESC to an event type.\n\
5769 EVENT-DESC should contain one base event type (a character or symbol)\n\
5770 and zero or more modifier names (control, meta, hyper, super, shift, alt,\n\
5771 drag, down, double or triple). The base must be last.\n\
5772 The return value is an event type (a character or symbol) which\n\
5773 has the same base event type and all the specified modifiers.")
5774 (event_desc)
5775 Lisp_Object event_desc;
5776 {
5777 Lisp_Object base;
5778 int modifiers = 0;
5779 Lisp_Object rest;
5780
5781 base = Qnil;
5782 rest = event_desc;
5783 while (CONSP (rest))
5784 {
5785 Lisp_Object elt;
5786 int this = 0;
5787
5788 elt = XCAR (rest);
5789 rest = XCDR (rest);
5790
5791 /* Given a symbol, see if it is a modifier name. */
5792 if (SYMBOLP (elt) && CONSP (rest))
5793 this = parse_solitary_modifier (elt);
5794
5795 if (this != 0)
5796 modifiers |= this;
5797 else if (!NILP (base))
5798 error ("Two bases given in one event");
5799 else
5800 base = elt;
5801
5802 }
5803
5804 /* Let the symbol A refer to the character A. */
5805 if (SYMBOLP (base) && XSYMBOL (base)->name->size == 1)
5806 XSETINT (base, XSYMBOL (base)->name->data[0]);
5807
5808 if (INTEGERP (base))
5809 {
5810 /* Turn (shift a) into A. */
5811 if ((modifiers & shift_modifier) != 0
5812 && (XINT (base) >= 'a' && XINT (base) <= 'z'))
5813 {
5814 XSETINT (base, XINT (base) - ('a' - 'A'));
5815 modifiers &= ~shift_modifier;
5816 }
5817
5818 /* Turn (control a) into C-a. */
5819 if (modifiers & ctrl_modifier)
5820 return make_number ((modifiers & ~ctrl_modifier)
5821 | make_ctrl_char (XINT (base)));
5822 else
5823 return make_number (modifiers | XINT (base));
5824 }
5825 else if (SYMBOLP (base))
5826 return apply_modifiers (modifiers, base);
5827 else
5828 {
5829 error ("Invalid base event");
5830 return Qnil;
5831 }
5832 }
5833
5834 /* Try to recognize SYMBOL as a modifier name.
5835 Return the modifier flag bit, or 0 if not recognized. */
5836
5837 static int
5838 parse_solitary_modifier (symbol)
5839 Lisp_Object symbol;
5840 {
5841 struct Lisp_String *name = XSYMBOL (symbol)->name;
5842
5843 switch (name->data[0])
5844 {
5845 #define SINGLE_LETTER_MOD(BIT) \
5846 if (STRING_BYTES (name) == 1) \
5847 return BIT;
5848
5849 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
5850 if (LEN == STRING_BYTES (name) \
5851 && ! strncmp (name->data, NAME, LEN)) \
5852 return BIT;
5853
5854 case 'A':
5855 SINGLE_LETTER_MOD (alt_modifier);
5856 break;
5857
5858 case 'a':
5859 MULTI_LETTER_MOD (alt_modifier, "alt", 3);
5860 break;
5861
5862 case 'C':
5863 SINGLE_LETTER_MOD (ctrl_modifier);
5864 break;
5865
5866 case 'c':
5867 MULTI_LETTER_MOD (ctrl_modifier, "ctrl", 4);
5868 MULTI_LETTER_MOD (ctrl_modifier, "control", 7);
5869 break;
5870
5871 case 'H':
5872 SINGLE_LETTER_MOD (hyper_modifier);
5873 break;
5874
5875 case 'h':
5876 MULTI_LETTER_MOD (hyper_modifier, "hyper", 5);
5877 break;
5878
5879 case 'M':
5880 SINGLE_LETTER_MOD (meta_modifier);
5881 break;
5882
5883 case 'm':
5884 MULTI_LETTER_MOD (meta_modifier, "meta", 4);
5885 break;
5886
5887 case 'S':
5888 SINGLE_LETTER_MOD (shift_modifier);
5889 break;
5890
5891 case 's':
5892 MULTI_LETTER_MOD (shift_modifier, "shift", 5);
5893 MULTI_LETTER_MOD (super_modifier, "super", 5);
5894 SINGLE_LETTER_MOD (super_modifier);
5895 break;
5896
5897 case 'd':
5898 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
5899 MULTI_LETTER_MOD (down_modifier, "down", 4);
5900 MULTI_LETTER_MOD (double_modifier, "double", 6);
5901 break;
5902
5903 case 't':
5904 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
5905 break;
5906
5907 #undef SINGLE_LETTER_MOD
5908 #undef MULTI_LETTER_MOD
5909 }
5910
5911 return 0;
5912 }
5913
5914 /* Return 1 if EVENT is a list whose elements are all integers or symbols.
5915 Such a list is not valid as an event,
5916 but it can be a Lucid-style event type list. */
5917
5918 int
5919 lucid_event_type_list_p (object)
5920 Lisp_Object object;
5921 {
5922 Lisp_Object tail;
5923
5924 if (! CONSP (object))
5925 return 0;
5926
5927 if (EQ (XCAR (object), Qhelp_echo)
5928 || EQ (XCAR (object), Qvertical_line)
5929 || EQ (XCAR (object), Qmode_line)
5930 || EQ (XCAR (object), Qheader_line))
5931 return 0;
5932
5933 for (tail = object; CONSP (tail); tail = XCDR (tail))
5934 {
5935 Lisp_Object elt;
5936 elt = XCAR (tail);
5937 if (! (INTEGERP (elt) || SYMBOLP (elt)))
5938 return 0;
5939 }
5940
5941 return NILP (tail);
5942 }
5943 \f
5944 /* Store into *addr a value nonzero if terminal input chars are available.
5945 Serves the purpose of ioctl (0, FIONREAD, addr)
5946 but works even if FIONREAD does not exist.
5947 (In fact, this may actually read some input.)
5948
5949 If DO_TIMERS_NOW is nonzero, actually run timer events that are ripe. */
5950
5951 static void
5952 get_input_pending (addr, do_timers_now)
5953 int *addr;
5954 int do_timers_now;
5955 {
5956 /* First of all, have we already counted some input? */
5957 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
5958
5959 /* If input is being read as it arrives, and we have none, there is none. */
5960 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
5961 return;
5962
5963 /* Try to read some input and see how much we get. */
5964 gobble_input (0);
5965 *addr = !NILP (Vquit_flag) || readable_events (do_timers_now);
5966 }
5967
5968 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
5969
5970 void
5971 gobble_input (expected)
5972 int expected;
5973 {
5974 #ifndef VMS
5975 #ifdef SIGIO
5976 if (interrupt_input)
5977 {
5978 SIGMASKTYPE mask;
5979 mask = sigblock (sigmask (SIGIO));
5980 read_avail_input (expected);
5981 sigsetmask (mask);
5982 }
5983 else
5984 #ifdef POLL_FOR_INPUT
5985 if (read_socket_hook && !interrupt_input && poll_suppress_count == 0)
5986 {
5987 SIGMASKTYPE mask;
5988 mask = sigblock (sigmask (SIGALRM));
5989 read_avail_input (expected);
5990 sigsetmask (mask);
5991 }
5992 else
5993 #endif
5994 #endif
5995 read_avail_input (expected);
5996 #endif
5997 }
5998
5999 /* Put a buffer_switch_event in the buffer
6000 so that read_key_sequence will notice the new current buffer. */
6001
6002 void
6003 record_asynch_buffer_change ()
6004 {
6005 struct input_event event;
6006 Lisp_Object tem;
6007
6008 event.kind = buffer_switch_event;
6009 event.frame_or_window = Qnil;
6010 event.arg = Qnil;
6011
6012 #ifdef subprocesses
6013 /* We don't need a buffer-switch event unless Emacs is waiting for input.
6014 The purpose of the event is to make read_key_sequence look up the
6015 keymaps again. If we aren't in read_key_sequence, we don't need one,
6016 and the event could cause trouble by messing up (input-pending-p). */
6017 tem = Fwaiting_for_user_input_p ();
6018 if (NILP (tem))
6019 return;
6020 #else
6021 /* We never need these events if we have no asynchronous subprocesses. */
6022 return;
6023 #endif
6024
6025 /* Make sure no interrupt happens while storing the event. */
6026 #ifdef SIGIO
6027 if (interrupt_input)
6028 {
6029 SIGMASKTYPE mask;
6030 mask = sigblock (sigmask (SIGIO));
6031 kbd_buffer_store_event (&event);
6032 sigsetmask (mask);
6033 }
6034 else
6035 #endif
6036 {
6037 stop_polling ();
6038 kbd_buffer_store_event (&event);
6039 start_polling ();
6040 }
6041 }
6042 \f
6043 #ifndef VMS
6044
6045 /* Read any terminal input already buffered up by the system
6046 into the kbd_buffer, but do not wait.
6047
6048 EXPECTED should be nonzero if the caller knows there is some input.
6049
6050 Except on VMS, all input is read by this function.
6051 If interrupt_input is nonzero, this function MUST be called
6052 only when SIGIO is blocked.
6053
6054 Returns the number of keyboard chars read, or -1 meaning
6055 this is a bad time to try to read input. */
6056
6057 static int
6058 read_avail_input (expected)
6059 int expected;
6060 {
6061 struct input_event buf[KBD_BUFFER_SIZE];
6062 register int i;
6063 int nread;
6064
6065 if (read_socket_hook)
6066 /* No need for FIONREAD or fcntl; just say don't wait. */
6067 nread = (*read_socket_hook) (input_fd, buf, KBD_BUFFER_SIZE, expected);
6068 else
6069 {
6070 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
6071 the kbd_buffer can really hold. That may prevent loss
6072 of characters on some systems when input is stuffed at us. */
6073 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
6074 int n_to_read;
6075
6076 /* Determine how many characters we should *try* to read. */
6077 #ifdef WINDOWSNT
6078 return 0;
6079 #else /* not WINDOWSNT */
6080 #ifdef MSDOS
6081 n_to_read = dos_keysns ();
6082 if (n_to_read == 0)
6083 return 0;
6084 #else /* not MSDOS */
6085 #ifdef FIONREAD
6086 /* Find out how much input is available. */
6087 if (ioctl (input_fd, FIONREAD, &n_to_read) < 0)
6088 /* Formerly simply reported no input, but that sometimes led to
6089 a failure of Emacs to terminate.
6090 SIGHUP seems appropriate if we can't reach the terminal. */
6091 /* ??? Is it really right to send the signal just to this process
6092 rather than to the whole process group?
6093 Perhaps on systems with FIONREAD Emacs is alone in its group. */
6094 kill (getpid (), SIGHUP);
6095 if (n_to_read == 0)
6096 return 0;
6097 if (n_to_read > sizeof cbuf)
6098 n_to_read = sizeof cbuf;
6099 #else /* no FIONREAD */
6100 #if defined (USG) || defined (DGUX)
6101 /* Read some input if available, but don't wait. */
6102 n_to_read = sizeof cbuf;
6103 fcntl (input_fd, F_SETFL, O_NDELAY);
6104 #else
6105 you lose;
6106 #endif
6107 #endif
6108 #endif /* not MSDOS */
6109 #endif /* not WINDOWSNT */
6110
6111 /* Now read; for one reason or another, this will not block.
6112 NREAD is set to the number of chars read. */
6113 do
6114 {
6115 #ifdef MSDOS
6116 cbuf[0] = dos_keyread ();
6117 nread = 1;
6118 #else
6119 nread = emacs_read (input_fd, cbuf, n_to_read);
6120 #endif
6121 /* POSIX infers that processes which are not in the session leader's
6122 process group won't get SIGHUP's at logout time. BSDI adheres to
6123 this part standard and returns -1 from read (0) with errno==EIO
6124 when the control tty is taken away.
6125 Jeffrey Honig <jch@bsdi.com> says this is generally safe. */
6126 if (nread == -1 && errno == EIO)
6127 kill (0, SIGHUP);
6128 #if defined (AIX) && (! defined (aix386) && defined (_BSD))
6129 /* The kernel sometimes fails to deliver SIGHUP for ptys.
6130 This looks incorrect, but it isn't, because _BSD causes
6131 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
6132 and that causes a value other than 0 when there is no input. */
6133 if (nread == 0)
6134 kill (0, SIGHUP);
6135 #endif
6136 }
6137 while (
6138 /* We used to retry the read if it was interrupted.
6139 But this does the wrong thing when O_NDELAY causes
6140 an EAGAIN error. Does anybody know of a situation
6141 where a retry is actually needed? */
6142 #if 0
6143 nread < 0 && (errno == EAGAIN
6144 #ifdef EFAULT
6145 || errno == EFAULT
6146 #endif
6147 #ifdef EBADSLT
6148 || errno == EBADSLT
6149 #endif
6150 )
6151 #else
6152 0
6153 #endif
6154 );
6155
6156 #ifndef FIONREAD
6157 #if defined (USG) || defined (DGUX)
6158 fcntl (input_fd, F_SETFL, 0);
6159 #endif /* USG or DGUX */
6160 #endif /* no FIONREAD */
6161 for (i = 0; i < nread; i++)
6162 {
6163 buf[i].kind = ascii_keystroke;
6164 buf[i].modifiers = 0;
6165 if (meta_key == 1 && (cbuf[i] & 0x80))
6166 buf[i].modifiers = meta_modifier;
6167 if (meta_key != 2)
6168 cbuf[i] &= ~0x80;
6169
6170 buf[i].code = cbuf[i];
6171 buf[i].frame_or_window = selected_frame;
6172 buf[i].arg = Qnil;
6173 }
6174 }
6175
6176 /* Scan the chars for C-g and store them in kbd_buffer. */
6177 for (i = 0; i < nread; i++)
6178 {
6179 kbd_buffer_store_event (&buf[i]);
6180 /* Don't look at input that follows a C-g too closely.
6181 This reduces lossage due to autorepeat on C-g. */
6182 if (buf[i].kind == ascii_keystroke
6183 && buf[i].code == quit_char)
6184 break;
6185 }
6186
6187 return nread;
6188 }
6189 #endif /* not VMS */
6190 \f
6191 #ifdef SIGIO /* for entire page */
6192 /* Note SIGIO has been undef'd if FIONREAD is missing. */
6193
6194 SIGTYPE
6195 input_available_signal (signo)
6196 int signo;
6197 {
6198 /* Must preserve main program's value of errno. */
6199 int old_errno = errno;
6200 #ifdef BSD4_1
6201 extern int select_alarmed;
6202 #endif
6203
6204 #if defined (USG) && !defined (POSIX_SIGNALS)
6205 /* USG systems forget handlers when they are used;
6206 must reestablish each time */
6207 signal (signo, input_available_signal);
6208 #endif /* USG */
6209
6210 #ifdef BSD4_1
6211 sigisheld (SIGIO);
6212 #endif
6213
6214 if (input_available_clear_time)
6215 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
6216
6217 while (1)
6218 {
6219 int nread;
6220 nread = read_avail_input (1);
6221 /* -1 means it's not ok to read the input now.
6222 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
6223 0 means there was no keyboard input available. */
6224 if (nread <= 0)
6225 break;
6226
6227 #ifdef BSD4_1
6228 select_alarmed = 1; /* Force the select emulator back to life */
6229 #endif
6230 }
6231
6232 #ifdef BSD4_1
6233 sigfree ();
6234 #endif
6235 errno = old_errno;
6236 }
6237 #endif /* SIGIO */
6238
6239 /* Send ourselves a SIGIO.
6240
6241 This function exists so that the UNBLOCK_INPUT macro in
6242 blockinput.h can have some way to take care of input we put off
6243 dealing with, without assuming that every file which uses
6244 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
6245 void
6246 reinvoke_input_signal ()
6247 {
6248 #ifdef SIGIO
6249 kill (getpid (), SIGIO);
6250 #endif
6251 }
6252
6253
6254 \f
6255 /* Return the prompt-string of a sparse keymap.
6256 This is the first element which is a string.
6257 Return nil if there is none. */
6258
6259 Lisp_Object
6260 map_prompt (map)
6261 Lisp_Object map;
6262 {
6263 while (CONSP (map))
6264 {
6265 register Lisp_Object tem;
6266 tem = Fcar (map);
6267 if (STRINGP (tem))
6268 return tem;
6269 map = Fcdr (map);
6270 }
6271 return Qnil;
6272 }
6273
6274 static void menu_bar_item P_ ((Lisp_Object, Lisp_Object));
6275 static void menu_bar_one_keymap P_ ((Lisp_Object));
6276
6277 /* These variables hold the vector under construction within
6278 menu_bar_items and its subroutines, and the current index
6279 for storing into that vector. */
6280 static Lisp_Object menu_bar_items_vector;
6281 static int menu_bar_items_index;
6282
6283 /* Return a vector of menu items for a menu bar, appropriate
6284 to the current buffer. Each item has three elements in the vector:
6285 KEY STRING MAPLIST.
6286
6287 OLD is an old vector we can optionally reuse, or nil. */
6288
6289 Lisp_Object
6290 menu_bar_items (old)
6291 Lisp_Object old;
6292 {
6293 /* The number of keymaps we're scanning right now, and the number of
6294 keymaps we have allocated space for. */
6295 int nmaps;
6296
6297 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
6298 in the current keymaps, or nil where it is not a prefix. */
6299 Lisp_Object *maps;
6300
6301 Lisp_Object def, tail;
6302
6303 Lisp_Object result;
6304
6305 int mapno;
6306 Lisp_Object oquit;
6307
6308 int i;
6309
6310 struct gcpro gcpro1;
6311
6312 /* In order to build the menus, we need to call the keymap
6313 accessors. They all call QUIT. But this function is called
6314 during redisplay, during which a quit is fatal. So inhibit
6315 quitting while building the menus.
6316 We do this instead of specbind because (1) errors will clear it anyway
6317 and (2) this avoids risk of specpdl overflow. */
6318 oquit = Vinhibit_quit;
6319 Vinhibit_quit = Qt;
6320
6321 if (!NILP (old))
6322 menu_bar_items_vector = old;
6323 else
6324 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
6325 menu_bar_items_index = 0;
6326
6327 GCPRO1 (menu_bar_items_vector);
6328
6329 /* Build our list of keymaps.
6330 If we recognize a function key and replace its escape sequence in
6331 keybuf with its symbol, or if the sequence starts with a mouse
6332 click and we need to switch buffers, we jump back here to rebuild
6333 the initial keymaps from the current buffer. */
6334 {
6335 Lisp_Object *tmaps;
6336
6337 /* Should overriding-terminal-local-map and overriding-local-map apply? */
6338 if (!NILP (Voverriding_local_map_menu_flag))
6339 {
6340 /* Yes, use them (if non-nil) as well as the global map. */
6341 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
6342 nmaps = 0;
6343 if (!NILP (current_kboard->Voverriding_terminal_local_map))
6344 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
6345 if (!NILP (Voverriding_local_map))
6346 maps[nmaps++] = Voverriding_local_map;
6347 }
6348 else
6349 {
6350 /* No, so use major and minor mode keymaps and keymap property. */
6351 int extra_maps = 2;
6352 Lisp_Object map = get_local_map (PT, current_buffer, Qkeymap);
6353 if (!NILP (map))
6354 extra_maps = 3;
6355 nmaps = current_minor_maps (NULL, &tmaps);
6356 maps = (Lisp_Object *) alloca ((nmaps + extra_maps)
6357 * sizeof (maps[0]));
6358 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
6359 if (!NILP (map))
6360 maps[nmaps++] = map;
6361 maps[nmaps++] = get_local_map (PT, current_buffer, Qlocal_map);
6362 }
6363 maps[nmaps++] = current_global_map;
6364 }
6365
6366 /* Look up in each map the dummy prefix key `menu-bar'. */
6367
6368 result = Qnil;
6369
6370 for (mapno = nmaps - 1; mapno >= 0; mapno--)
6371 if (!NILP (maps[mapno]))
6372 {
6373 def = get_keymap (access_keymap (maps[mapno], Qmenu_bar, 1, 0, 1),
6374 0, 1);
6375 if (CONSP (def))
6376 menu_bar_one_keymap (def);
6377 }
6378
6379 /* Move to the end those items that should be at the end. */
6380
6381 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCDR (tail))
6382 {
6383 int i;
6384 int end = menu_bar_items_index;
6385
6386 for (i = 0; i < end; i += 4)
6387 if (EQ (XCAR (tail), XVECTOR (menu_bar_items_vector)->contents[i]))
6388 {
6389 Lisp_Object tem0, tem1, tem2, tem3;
6390 /* Move the item at index I to the end,
6391 shifting all the others forward. */
6392 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
6393 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
6394 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
6395 tem3 = XVECTOR (menu_bar_items_vector)->contents[i + 3];
6396 if (end > i + 4)
6397 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
6398 &XVECTOR (menu_bar_items_vector)->contents[i],
6399 (end - i - 4) * sizeof (Lisp_Object));
6400 XVECTOR (menu_bar_items_vector)->contents[end - 4] = tem0;
6401 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem1;
6402 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem2;
6403 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem3;
6404 break;
6405 }
6406 }
6407
6408 /* Add nil, nil, nil, nil at the end. */
6409 i = menu_bar_items_index;
6410 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
6411 {
6412 Lisp_Object tem;
6413 tem = Fmake_vector (make_number (2 * i), Qnil);
6414 bcopy (XVECTOR (menu_bar_items_vector)->contents,
6415 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
6416 menu_bar_items_vector = tem;
6417 }
6418 /* Add this item. */
6419 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6420 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6421 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6422 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
6423 menu_bar_items_index = i;
6424
6425 Vinhibit_quit = oquit;
6426 UNGCPRO;
6427 return menu_bar_items_vector;
6428 }
6429 \f
6430 /* Scan one map KEYMAP, accumulating any menu items it defines
6431 in menu_bar_items_vector. */
6432
6433 static Lisp_Object menu_bar_one_keymap_changed_items;
6434
6435 static void
6436 menu_bar_one_keymap (keymap)
6437 Lisp_Object keymap;
6438 {
6439 Lisp_Object tail, item;
6440
6441 menu_bar_one_keymap_changed_items = Qnil;
6442
6443 /* Loop over all keymap entries that have menu strings. */
6444 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
6445 {
6446 item = XCAR (tail);
6447 if (CONSP (item))
6448 menu_bar_item (XCAR (item), XCDR (item));
6449 else if (VECTORP (item))
6450 {
6451 /* Loop over the char values represented in the vector. */
6452 int len = XVECTOR (item)->size;
6453 int c;
6454 for (c = 0; c < len; c++)
6455 {
6456 Lisp_Object character;
6457 XSETFASTINT (character, c);
6458 menu_bar_item (character, XVECTOR (item)->contents[c]);
6459 }
6460 }
6461 }
6462 }
6463
6464 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
6465 If there's already an item for KEY, add this DEF to it. */
6466
6467 Lisp_Object item_properties;
6468
6469 static void
6470 menu_bar_item (key, item)
6471 Lisp_Object key, item;
6472 {
6473 struct gcpro gcpro1;
6474 int i;
6475 Lisp_Object tem;
6476
6477 if (EQ (item, Qundefined))
6478 {
6479 /* If a map has an explicit `undefined' as definition,
6480 discard any previously made menu bar item. */
6481
6482 for (i = 0; i < menu_bar_items_index; i += 4)
6483 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
6484 {
6485 if (menu_bar_items_index > i + 4)
6486 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 4],
6487 &XVECTOR (menu_bar_items_vector)->contents[i],
6488 (menu_bar_items_index - i - 4) * sizeof (Lisp_Object));
6489 menu_bar_items_index -= 4;
6490 }
6491 }
6492
6493 /* If this keymap has already contributed to this KEY,
6494 don't contribute to it a second time. */
6495 tem = Fmemq (key, menu_bar_one_keymap_changed_items);
6496 if (!NILP (tem) || NILP (item))
6497 return;
6498
6499 menu_bar_one_keymap_changed_items
6500 = Fcons (key, menu_bar_one_keymap_changed_items);
6501
6502 /* We add to menu_bar_one_keymap_changed_items before doing the
6503 parse_menu_item, so that if it turns out it wasn't a menu item,
6504 it still correctly hides any further menu item. */
6505 GCPRO1 (key);
6506 i = parse_menu_item (item, 0, 1);
6507 UNGCPRO;
6508 if (!i)
6509 return;
6510
6511 item = XVECTOR (item_properties)->contents[ITEM_PROPERTY_DEF];
6512
6513 /* Find any existing item for this KEY. */
6514 for (i = 0; i < menu_bar_items_index; i += 4)
6515 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
6516 break;
6517
6518 /* If we did not find this KEY, add it at the end. */
6519 if (i == menu_bar_items_index)
6520 {
6521 /* If vector is too small, get a bigger one. */
6522 if (i + 4 > XVECTOR (menu_bar_items_vector)->size)
6523 {
6524 Lisp_Object tem;
6525 tem = Fmake_vector (make_number (2 * i), Qnil);
6526 bcopy (XVECTOR (menu_bar_items_vector)->contents,
6527 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
6528 menu_bar_items_vector = tem;
6529 }
6530
6531 /* Add this item. */
6532 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
6533 XVECTOR (menu_bar_items_vector)->contents[i++]
6534 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
6535 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (item, Qnil);
6536 XVECTOR (menu_bar_items_vector)->contents[i++] = make_number (0);
6537 menu_bar_items_index = i;
6538 }
6539 /* We did find an item for this KEY. Add ITEM to its list of maps. */
6540 else
6541 {
6542 Lisp_Object old;
6543 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
6544 XVECTOR (menu_bar_items_vector)->contents[i + 2] = Fcons (item, old);
6545 }
6546 }
6547 \f
6548 /* This is used as the handler when calling menu_item_eval_property. */
6549 static Lisp_Object
6550 menu_item_eval_property_1 (arg)
6551 Lisp_Object arg;
6552 {
6553 /* If we got a quit from within the menu computation,
6554 quit all the way out of it. This takes care of C-] in the debugger. */
6555 if (CONSP (arg) && EQ (XCAR (arg), Qquit))
6556 Fsignal (Qquit, Qnil);
6557
6558 return Qnil;
6559 }
6560
6561 /* Evaluate an expression and return the result (or nil if something
6562 went wrong). Used to evaluate dynamic parts of menu items. */
6563 Lisp_Object
6564 menu_item_eval_property (sexpr)
6565 Lisp_Object sexpr;
6566 {
6567 int count = specpdl_ptr - specpdl;
6568 Lisp_Object val;
6569 specbind (Qinhibit_redisplay, Qt);
6570 val = internal_condition_case_1 (Feval, sexpr, Qerror,
6571 menu_item_eval_property_1);
6572 return unbind_to (count, val);
6573 }
6574
6575 /* This function parses a menu item and leaves the result in the
6576 vector item_properties.
6577 ITEM is a key binding, a possible menu item.
6578 If NOTREAL is nonzero, only check for equivalent key bindings, don't
6579 evaluate dynamic expressions in the menu item.
6580 INMENUBAR is > 0 when this is considered for an entry in a menu bar
6581 top level.
6582 INMENUBAR is < 0 when this is considered for an entry in a keyboard menu.
6583 parse_menu_item returns true if the item is a menu item and false
6584 otherwise. */
6585
6586 int
6587 parse_menu_item (item, notreal, inmenubar)
6588 Lisp_Object item;
6589 int notreal, inmenubar;
6590 {
6591 Lisp_Object def, tem, item_string, start;
6592 Lisp_Object cachelist;
6593 Lisp_Object filter;
6594 Lisp_Object keyhint;
6595 int i;
6596 int newcache = 0;
6597
6598 cachelist = Qnil;
6599 filter = Qnil;
6600 keyhint = Qnil;
6601
6602 if (!CONSP (item))
6603 return 0;
6604
6605 /* Create item_properties vector if necessary. */
6606 if (NILP (item_properties))
6607 item_properties
6608 = Fmake_vector (make_number (ITEM_PROPERTY_ENABLE + 1), Qnil);
6609
6610 /* Initialize optional entries. */
6611 for (i = ITEM_PROPERTY_DEF; i < ITEM_PROPERTY_ENABLE; i++)
6612 AREF (item_properties, i) = Qnil;
6613 AREF (item_properties, ITEM_PROPERTY_ENABLE) = Qt;
6614
6615 /* Save the item here to protect it from GC. */
6616 AREF (item_properties, ITEM_PROPERTY_ITEM) = item;
6617
6618 item_string = XCAR (item);
6619
6620 start = item;
6621 item = XCDR (item);
6622 if (STRINGP (item_string))
6623 {
6624 /* Old format menu item. */
6625 AREF (item_properties, ITEM_PROPERTY_NAME) = item_string;
6626
6627 /* Maybe help string. */
6628 if (CONSP (item) && STRINGP (XCAR (item)))
6629 {
6630 AREF (item_properties, ITEM_PROPERTY_HELP) = XCAR (item);
6631 start = item;
6632 item = XCDR (item);
6633 }
6634
6635 /* Maybe key binding cache. */
6636 if (CONSP (item) && CONSP (XCAR (item))
6637 && (NILP (XCAR (XCAR (item)))
6638 || VECTORP (XCAR (XCAR (item)))))
6639 {
6640 cachelist = XCAR (item);
6641 item = XCDR (item);
6642 }
6643
6644 /* This is the real definition--the function to run. */
6645 AREF (item_properties, ITEM_PROPERTY_DEF) = item;
6646
6647 /* Get enable property, if any. */
6648 if (SYMBOLP (item))
6649 {
6650 tem = Fget (item, Qmenu_enable);
6651 if (!NILP (tem))
6652 AREF (item_properties, ITEM_PROPERTY_ENABLE) = tem;
6653 }
6654 }
6655 else if (EQ (item_string, Qmenu_item) && CONSP (item))
6656 {
6657 /* New format menu item. */
6658 AREF (item_properties, ITEM_PROPERTY_NAME) = XCAR (item);
6659 start = XCDR (item);
6660 if (CONSP (start))
6661 {
6662 /* We have a real binding. */
6663 AREF (item_properties, ITEM_PROPERTY_DEF) = XCAR (start);
6664
6665 item = XCDR (start);
6666 /* Is there a cache list with key equivalences. */
6667 if (CONSP (item) && CONSP (XCAR (item)))
6668 {
6669 cachelist = XCAR (item);
6670 item = XCDR (item);
6671 }
6672
6673 /* Parse properties. */
6674 while (CONSP (item) && CONSP (XCDR (item)))
6675 {
6676 tem = XCAR (item);
6677 item = XCDR (item);
6678
6679 if (EQ (tem, QCenable))
6680 AREF (item_properties, ITEM_PROPERTY_ENABLE) = XCAR (item);
6681 else if (EQ (tem, QCvisible) && !notreal)
6682 {
6683 /* If got a visible property and that evaluates to nil
6684 then ignore this item. */
6685 tem = menu_item_eval_property (XCAR (item));
6686 if (NILP (tem))
6687 return 0;
6688 }
6689 else if (EQ (tem, QChelp))
6690 AREF (item_properties, ITEM_PROPERTY_HELP) = XCAR (item);
6691 else if (EQ (tem, QCfilter))
6692 filter = item;
6693 else if (EQ (tem, QCkey_sequence))
6694 {
6695 tem = XCAR (item);
6696 if (NILP (cachelist)
6697 && (SYMBOLP (tem) || STRINGP (tem) || VECTORP (tem)))
6698 /* Be GC protected. Set keyhint to item instead of tem. */
6699 keyhint = item;
6700 }
6701 else if (EQ (tem, QCkeys))
6702 {
6703 tem = XCAR (item);
6704 if (CONSP (tem) || (STRINGP (tem) && NILP (cachelist)))
6705 AREF (item_properties, ITEM_PROPERTY_KEYEQ) = tem;
6706 }
6707 else if (EQ (tem, QCbutton) && CONSP (XCAR (item)))
6708 {
6709 Lisp_Object type;
6710 tem = XCAR (item);
6711 type = XCAR (tem);
6712 if (EQ (type, QCtoggle) || EQ (type, QCradio))
6713 {
6714 AREF (item_properties, ITEM_PROPERTY_SELECTED)
6715 = XCDR (tem);
6716 AREF (item_properties, ITEM_PROPERTY_TYPE)
6717 = type;
6718 }
6719 }
6720 item = XCDR (item);
6721 }
6722 }
6723 else if (inmenubar || !NILP (start))
6724 return 0;
6725 }
6726 else
6727 return 0; /* not a menu item */
6728
6729 /* If item string is not a string, evaluate it to get string.
6730 If we don't get a string, skip this item. */
6731 item_string = AREF (item_properties, ITEM_PROPERTY_NAME);
6732 if (!(STRINGP (item_string) || notreal))
6733 {
6734 item_string = menu_item_eval_property (item_string);
6735 if (!STRINGP (item_string))
6736 return 0;
6737 AREF (item_properties, ITEM_PROPERTY_NAME) = item_string;
6738 }
6739
6740 /* If got a filter apply it on definition. */
6741 def = AREF (item_properties, ITEM_PROPERTY_DEF);
6742 if (!NILP (filter))
6743 {
6744 def = menu_item_eval_property (list2 (XCAR (filter),
6745 list2 (Qquote, def)));
6746
6747 AREF (item_properties, ITEM_PROPERTY_DEF) = def;
6748 }
6749
6750 /* If we got no definition, this item is just unselectable text which
6751 is OK in a submenu but not in the menubar. */
6752 if (NILP (def))
6753 return (inmenubar ? 0 : 1);
6754
6755 /* Enable or disable selection of item. */
6756 tem = AREF (item_properties, ITEM_PROPERTY_ENABLE);
6757 if (!EQ (tem, Qt))
6758 {
6759 if (notreal)
6760 tem = Qt;
6761 else
6762 tem = menu_item_eval_property (tem);
6763 if (inmenubar && NILP (tem))
6764 return 0; /* Ignore disabled items in menu bar. */
6765 AREF (item_properties, ITEM_PROPERTY_ENABLE) = tem;
6766 }
6767
6768 /* See if this is a separate pane or a submenu. */
6769 def = AREF (item_properties, ITEM_PROPERTY_DEF);
6770 tem = get_keymap (def, 0, 1);
6771 /* For a subkeymap, just record its details and exit. */
6772 if (CONSP (tem))
6773 {
6774 AREF (item_properties, ITEM_PROPERTY_MAP) = tem;
6775 AREF (item_properties, ITEM_PROPERTY_DEF) = tem;
6776 return 1;
6777 }
6778
6779 /* At the top level in the menu bar, do likewise for commands also.
6780 The menu bar does not display equivalent key bindings anyway.
6781 ITEM_PROPERTY_DEF is already set up properly. */
6782 if (inmenubar > 0)
6783 return 1;
6784
6785 /* This is a command. See if there is an equivalent key binding. */
6786 if (NILP (cachelist))
6787 {
6788 /* We have to create a cachelist. */
6789 CHECK_IMPURE (start);
6790 XCDR (start) = Fcons (Fcons (Qnil, Qnil), XCDR (start));
6791 cachelist = XCAR (XCDR (start));
6792 newcache = 1;
6793 tem = AREF (item_properties, ITEM_PROPERTY_KEYEQ);
6794 if (!NILP (keyhint))
6795 {
6796 XCAR (cachelist) = XCAR (keyhint);
6797 newcache = 0;
6798 }
6799 else if (STRINGP (tem))
6800 {
6801 XCDR (cachelist) = Fsubstitute_command_keys (tem);
6802 XCAR (cachelist) = Qt;
6803 }
6804 }
6805
6806 tem = XCAR (cachelist);
6807 if (!EQ (tem, Qt))
6808 {
6809 int chkcache = 0;
6810 Lisp_Object prefix;
6811
6812 if (!NILP (tem))
6813 tem = Fkey_binding (tem, Qnil);
6814
6815 prefix = AREF (item_properties, ITEM_PROPERTY_KEYEQ);
6816 if (CONSP (prefix))
6817 {
6818 def = XCAR (prefix);
6819 prefix = XCDR (prefix);
6820 }
6821 else
6822 def = AREF (item_properties, ITEM_PROPERTY_DEF);
6823
6824 if (!update_menu_bindings)
6825 chkcache = 0;
6826 else if (NILP (XCAR (cachelist))) /* Have no saved key. */
6827 {
6828 if (newcache /* Always check first time. */
6829 /* Should we check everything when precomputing key
6830 bindings? */
6831 /* If something had no key binding before, don't recheck it
6832 because that is too slow--except if we have a list of
6833 rebound commands in Vdefine_key_rebound_commands, do
6834 recheck any command that appears in that list. */
6835 || (CONSP (Vdefine_key_rebound_commands)
6836 && !NILP (Fmemq (def, Vdefine_key_rebound_commands))))
6837 chkcache = 1;
6838 }
6839 /* We had a saved key. Is it still bound to the command? */
6840 else if (NILP (tem)
6841 || (!EQ (tem, def)
6842 /* If the command is an alias for another
6843 (such as lmenu.el set it up), check if the
6844 original command matches the cached command. */
6845 && !(SYMBOLP (def) && EQ (tem, XSYMBOL (def)->function))))
6846 chkcache = 1; /* Need to recompute key binding. */
6847
6848 if (chkcache)
6849 {
6850 /* Recompute equivalent key binding. If the command is an alias
6851 for another (such as lmenu.el set it up), see if the original
6852 command name has equivalent keys. Otherwise look up the
6853 specified command itself. We don't try both, because that
6854 makes lmenu menus slow. */
6855 if (SYMBOLP (def)
6856 && SYMBOLP (XSYMBOL (def)->function)
6857 && ! NILP (Fget (def, Qmenu_alias)))
6858 def = XSYMBOL (def)->function;
6859 tem = Fwhere_is_internal (def, Qnil, Qt, Qnil);
6860 XCAR (cachelist) = tem;
6861 if (NILP (tem))
6862 {
6863 XCDR (cachelist) = Qnil;
6864 chkcache = 0;
6865 }
6866 }
6867 else if (!NILP (keyhint) && !NILP (XCAR (cachelist)))
6868 {
6869 tem = XCAR (cachelist);
6870 chkcache = 1;
6871 }
6872
6873 newcache = chkcache;
6874 if (chkcache)
6875 {
6876 tem = Fkey_description (tem);
6877 if (CONSP (prefix))
6878 {
6879 if (STRINGP (XCAR (prefix)))
6880 tem = concat2 (XCAR (prefix), tem);
6881 if (STRINGP (XCDR (prefix)))
6882 tem = concat2 (tem, XCDR (prefix));
6883 }
6884 XCDR (cachelist) = tem;
6885 }
6886 }
6887
6888 tem = XCDR (cachelist);
6889 if (newcache && !NILP (tem))
6890 {
6891 tem = concat3 (build_string (" ("), tem, build_string (")"));
6892 XCDR (cachelist) = tem;
6893 }
6894
6895 /* If we only want to precompute equivalent key bindings, stop here. */
6896 if (notreal)
6897 return 1;
6898
6899 /* If we have an equivalent key binding, use that. */
6900 AREF (item_properties, ITEM_PROPERTY_KEYEQ) = tem;
6901
6902 /* Include this when menu help is implemented.
6903 tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP];
6904 if (!(NILP (tem) || STRINGP (tem)))
6905 {
6906 tem = menu_item_eval_property (tem);
6907 if (!STRINGP (tem))
6908 tem = Qnil;
6909 XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP] = tem;
6910 }
6911 */
6912
6913 /* Handle radio buttons or toggle boxes. */
6914 tem = AREF (item_properties, ITEM_PROPERTY_SELECTED);
6915 if (!NILP (tem))
6916 AREF (item_properties, ITEM_PROPERTY_SELECTED)
6917 = menu_item_eval_property (tem);
6918
6919 return 1;
6920 }
6921
6922
6923 \f
6924 /***********************************************************************
6925 Tool-bars
6926 ***********************************************************************/
6927
6928 /* A vector holding tool bar items while they are parsed in function
6929 tool_bar_items runs Each item occupies TOOL_BAR_ITEM_NSCLOTS elements
6930 in the vector. */
6931
6932 static Lisp_Object tool_bar_items_vector;
6933
6934 /* A vector holding the result of parse_tool_bar_item. Layout is like
6935 the one for a single item in tool_bar_items_vector. */
6936
6937 static Lisp_Object tool_bar_item_properties;
6938
6939 /* Next free index in tool_bar_items_vector. */
6940
6941 static int ntool_bar_items;
6942
6943 /* The symbols `tool-bar', and `:image'. */
6944
6945 extern Lisp_Object Qtool_bar;
6946 Lisp_Object QCimage;
6947
6948 /* Function prototypes. */
6949
6950 static void init_tool_bar_items P_ ((Lisp_Object));
6951 static void process_tool_bar_item P_ ((Lisp_Object, Lisp_Object));
6952 static int parse_tool_bar_item P_ ((Lisp_Object, Lisp_Object));
6953 static void append_tool_bar_item P_ ((void));
6954
6955
6956 /* Return a vector of tool bar items for keymaps currently in effect.
6957 Reuse vector REUSE if non-nil. Return in *NITEMS the number of
6958 tool bar items found. */
6959
6960 Lisp_Object
6961 tool_bar_items (reuse, nitems)
6962 Lisp_Object reuse;
6963 int *nitems;
6964 {
6965 Lisp_Object *maps;
6966 int nmaps, i;
6967 Lisp_Object oquit;
6968 Lisp_Object *tmaps;
6969 extern Lisp_Object Voverriding_local_map_menu_flag;
6970 extern Lisp_Object Voverriding_local_map;
6971
6972 *nitems = 0;
6973
6974 /* In order to build the menus, we need to call the keymap
6975 accessors. They all call QUIT. But this function is called
6976 during redisplay, during which a quit is fatal. So inhibit
6977 quitting while building the menus. We do this instead of
6978 specbind because (1) errors will clear it anyway and (2) this
6979 avoids risk of specpdl overflow. */
6980 oquit = Vinhibit_quit;
6981 Vinhibit_quit = Qt;
6982
6983 /* Initialize tool_bar_items_vector and protect it from GC. */
6984 init_tool_bar_items (reuse);
6985
6986 /* Build list of keymaps in maps. Set nmaps to the number of maps
6987 to process. */
6988
6989 /* Should overriding-terminal-local-map and overriding-local-map apply? */
6990 if (!NILP (Voverriding_local_map_menu_flag))
6991 {
6992 /* Yes, use them (if non-nil) as well as the global map. */
6993 maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
6994 nmaps = 0;
6995 if (!NILP (current_kboard->Voverriding_terminal_local_map))
6996 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
6997 if (!NILP (Voverriding_local_map))
6998 maps[nmaps++] = Voverriding_local_map;
6999 }
7000 else
7001 {
7002 /* No, so use major and minor mode keymaps and keymap property. */
7003 int extra_maps = 2;
7004 Lisp_Object map = get_local_map (PT, current_buffer, Qkeymap);
7005 if (!NILP (map))
7006 extra_maps = 3;
7007 nmaps = current_minor_maps (NULL, &tmaps);
7008 maps = (Lisp_Object *) alloca ((nmaps + extra_maps)
7009 * sizeof (maps[0]));
7010 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
7011 if (!NILP (map))
7012 maps[nmaps++] = map;
7013 maps[nmaps++] = get_local_map (PT, current_buffer, Qlocal_map);
7014 }
7015
7016 /* Add global keymap at the end. */
7017 maps[nmaps++] = current_global_map;
7018
7019 /* Process maps in reverse order and look up in each map the prefix
7020 key `tool-bar'. */
7021 for (i = nmaps - 1; i >= 0; --i)
7022 if (!NILP (maps[i]))
7023 {
7024 Lisp_Object keymap;
7025
7026 keymap = get_keymap (access_keymap (maps[i], Qtool_bar, 1, 0, 1), 0, 1);
7027 if (CONSP (keymap))
7028 {
7029 Lisp_Object tail;
7030
7031 /* KEYMAP is a list `(keymap (KEY . BINDING) ...)'. */
7032 for (tail = keymap; CONSP (tail); tail = XCDR (tail))
7033 {
7034 Lisp_Object keydef = XCAR (tail);
7035 if (CONSP (keydef))
7036 process_tool_bar_item (XCAR (keydef), XCDR (keydef));
7037 }
7038 }
7039 }
7040
7041 Vinhibit_quit = oquit;
7042 *nitems = ntool_bar_items / TOOL_BAR_ITEM_NSLOTS;
7043 return tool_bar_items_vector;
7044 }
7045
7046
7047 /* Process the definition of KEY which is DEF. */
7048
7049 static void
7050 process_tool_bar_item (key, def)
7051 Lisp_Object key, def;
7052 {
7053 int i;
7054 extern Lisp_Object Qundefined;
7055 struct gcpro gcpro1, gcpro2;
7056
7057 /* Protect KEY and DEF from GC because parse_tool_bar_item may call
7058 eval. */
7059 GCPRO2 (key, def);
7060
7061 if (EQ (def, Qundefined))
7062 {
7063 /* If a map has an explicit `undefined' as definition,
7064 discard any previously made item. */
7065 for (i = 0; i < ntool_bar_items; i += TOOL_BAR_ITEM_NSLOTS)
7066 {
7067 Lisp_Object *v = XVECTOR (tool_bar_items_vector)->contents + i;
7068
7069 if (EQ (key, v[TOOL_BAR_ITEM_KEY]))
7070 {
7071 if (ntool_bar_items > i + TOOL_BAR_ITEM_NSLOTS)
7072 bcopy (v + TOOL_BAR_ITEM_NSLOTS, v,
7073 ((ntool_bar_items - i - TOOL_BAR_ITEM_NSLOTS)
7074 * sizeof (Lisp_Object)));
7075 ntool_bar_items -= TOOL_BAR_ITEM_NSLOTS;
7076 break;
7077 }
7078 }
7079 }
7080 else if (parse_tool_bar_item (key, def))
7081 /* Append a new tool bar item to tool_bar_items_vector. Accept
7082 more than one definition for the same key. */
7083 append_tool_bar_item ();
7084
7085 UNGCPRO;
7086 }
7087
7088
7089 /* Parse a tool bar item specification ITEM for key KEY and return the
7090 result in tool_bar_item_properties. Value is zero if ITEM is
7091 invalid.
7092
7093 ITEM is a list `(menu-item CAPTION BINDING PROPS...)'.
7094
7095 CAPTION is the caption of the item, If it's not a string, it is
7096 evaluated to get a string.
7097
7098 BINDING is the tool bar item's binding. Tool-bar items with keymaps
7099 as binding are currently ignored.
7100
7101 The following properties are recognized:
7102
7103 - `:enable FORM'.
7104
7105 FORM is evaluated and specifies whether the tool bar item is
7106 enabled or disabled.
7107
7108 - `:visible FORM'
7109
7110 FORM is evaluated and specifies whether the tool bar item is visible.
7111
7112 - `:filter FUNCTION'
7113
7114 FUNCTION is invoked with one parameter `(quote BINDING)'. Its
7115 result is stored as the new binding.
7116
7117 - `:button (TYPE SELECTED)'
7118
7119 TYPE must be one of `:radio' or `:toggle'. SELECTED is evaluated
7120 and specifies whether the button is selected (pressed) or not.
7121
7122 - `:image IMAGES'
7123
7124 IMAGES is either a single image specification or a vector of four
7125 image specifications. See enum tool_bar_item_images.
7126
7127 - `:help HELP-STRING'.
7128
7129 Gives a help string to display for the tool bar item. */
7130
7131 static int
7132 parse_tool_bar_item (key, item)
7133 Lisp_Object key, item;
7134 {
7135 /* Access slot with index IDX of vector tool_bar_item_properties. */
7136 #define PROP(IDX) XVECTOR (tool_bar_item_properties)->contents[IDX]
7137
7138 Lisp_Object filter = Qnil;
7139 Lisp_Object caption;
7140 extern Lisp_Object QCenable, QCvisible, QChelp, QCfilter;
7141 extern Lisp_Object QCbutton, QCtoggle, QCradio;
7142 int i;
7143
7144 /* Defininition looks like `(menu-item CAPTION BINDING PROPS...)'.
7145 Rule out items that aren't lists, don't start with
7146 `menu-item' or whose rest following `tool-bar-item' is not a
7147 list. */
7148 if (!CONSP (item)
7149 || !EQ (XCAR (item), Qmenu_item)
7150 || (item = XCDR (item),
7151 !CONSP (item)))
7152 return 0;
7153
7154 /* Create tool_bar_item_properties vector if necessary. Reset it to
7155 defaults. */
7156 if (VECTORP (tool_bar_item_properties))
7157 {
7158 for (i = 0; i < TOOL_BAR_ITEM_NSLOTS; ++i)
7159 PROP (i) = Qnil;
7160 }
7161 else
7162 tool_bar_item_properties
7163 = Fmake_vector (make_number (TOOL_BAR_ITEM_NSLOTS), Qnil);
7164
7165 /* Set defaults. */
7166 PROP (TOOL_BAR_ITEM_KEY) = key;
7167 PROP (TOOL_BAR_ITEM_ENABLED_P) = Qt;
7168
7169 /* Get the caption of the item. If the caption is not a string,
7170 evaluate it to get a string. If we don't get a string, skip this
7171 item. */
7172 caption = XCAR (item);
7173 if (!STRINGP (caption))
7174 {
7175 caption = menu_item_eval_property (caption);
7176 if (!STRINGP (caption))
7177 return 0;
7178 }
7179 PROP (TOOL_BAR_ITEM_CAPTION) = caption;
7180
7181 /* Give up if rest following the caption is not a list. */
7182 item = XCDR (item);
7183 if (!CONSP (item))
7184 return 0;
7185
7186 /* Store the binding. */
7187 PROP (TOOL_BAR_ITEM_BINDING) = XCAR (item);
7188 item = XCDR (item);
7189
7190 /* Ignore cached key binding, if any. */
7191 if (CONSP (item) && CONSP (XCAR (item)))
7192 item = XCDR (item);
7193
7194 /* Process the rest of the properties. */
7195 for (; CONSP (item) && CONSP (XCDR (item)); item = XCDR (XCDR (item)))
7196 {
7197 Lisp_Object key, value;
7198
7199 key = XCAR (item);
7200 value = XCAR (XCDR (item));
7201
7202 if (EQ (key, QCenable))
7203 /* `:enable FORM'. */
7204 PROP (TOOL_BAR_ITEM_ENABLED_P) = value;
7205 else if (EQ (key, QCvisible))
7206 {
7207 /* `:visible FORM'. If got a visible property and that
7208 evaluates to nil then ignore this item. */
7209 if (NILP (menu_item_eval_property (value)))
7210 return 0;
7211 }
7212 else if (EQ (key, QChelp))
7213 /* `:help HELP-STRING'. */
7214 PROP (TOOL_BAR_ITEM_HELP) = value;
7215 else if (EQ (key, QCfilter))
7216 /* ':filter FORM'. */
7217 filter = value;
7218 else if (EQ (key, QCbutton) && CONSP (value))
7219 {
7220 /* `:button (TYPE . SELECTED)'. */
7221 Lisp_Object type, selected;
7222
7223 type = XCAR (value);
7224 selected = XCDR (value);
7225 if (EQ (type, QCtoggle) || EQ (type, QCradio))
7226 {
7227 PROP (TOOL_BAR_ITEM_SELECTED_P) = selected;
7228 PROP (TOOL_BAR_ITEM_TYPE) = type;
7229 }
7230 }
7231 else if (EQ (key, QCimage)
7232 && (CONSP (value)
7233 || (VECTORP (value) && XVECTOR (value)->size == 4)))
7234 /* Value is either a single image specification or a vector
7235 of 4 such specifications for the different buttion states. */
7236 PROP (TOOL_BAR_ITEM_IMAGES) = value;
7237 }
7238
7239 /* If got a filter apply it on binding. */
7240 if (!NILP (filter))
7241 PROP (TOOL_BAR_ITEM_BINDING)
7242 = menu_item_eval_property (list2 (filter,
7243 list2 (Qquote,
7244 PROP (TOOL_BAR_ITEM_BINDING))));
7245
7246 /* See if the binding is a keymap. Give up if it is. */
7247 if (CONSP (get_keymap (PROP (TOOL_BAR_ITEM_BINDING), 0, 1)))
7248 return 0;
7249
7250 /* Enable or disable selection of item. */
7251 if (!EQ (PROP (TOOL_BAR_ITEM_ENABLED_P), Qt))
7252 PROP (TOOL_BAR_ITEM_ENABLED_P)
7253 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_ENABLED_P));
7254
7255 /* Handle radio buttons or toggle boxes. */
7256 if (!NILP (PROP (TOOL_BAR_ITEM_SELECTED_P)))
7257 PROP (TOOL_BAR_ITEM_SELECTED_P)
7258 = menu_item_eval_property (PROP (TOOL_BAR_ITEM_SELECTED_P));
7259
7260 return 1;
7261
7262 #undef PROP
7263 }
7264
7265
7266 /* Initialize tool_bar_items_vector. REUSE, if non-nil, is a vector
7267 that can be reused. */
7268
7269 static void
7270 init_tool_bar_items (reuse)
7271 Lisp_Object reuse;
7272 {
7273 if (VECTORP (reuse))
7274 tool_bar_items_vector = reuse;
7275 else
7276 tool_bar_items_vector = Fmake_vector (make_number (64), Qnil);
7277 ntool_bar_items = 0;
7278 }
7279
7280
7281 /* Append parsed tool bar item properties from
7282 tool_bar_item_properties */
7283
7284 static void
7285 append_tool_bar_item ()
7286 {
7287 Lisp_Object *to, *from;
7288
7289 /* Enlarge tool_bar_items_vector if necessary. */
7290 if (ntool_bar_items + TOOL_BAR_ITEM_NSLOTS
7291 >= XVECTOR (tool_bar_items_vector)->size)
7292 {
7293 Lisp_Object new_vector;
7294 int old_size = XVECTOR (tool_bar_items_vector)->size;
7295
7296 new_vector = Fmake_vector (make_number (2 * old_size), Qnil);
7297 bcopy (XVECTOR (tool_bar_items_vector)->contents,
7298 XVECTOR (new_vector)->contents,
7299 old_size * sizeof (Lisp_Object));
7300 tool_bar_items_vector = new_vector;
7301 }
7302
7303 /* Append entries from tool_bar_item_properties to the end of
7304 tool_bar_items_vector. */
7305 to = XVECTOR (tool_bar_items_vector)->contents + ntool_bar_items;
7306 from = XVECTOR (tool_bar_item_properties)->contents;
7307 bcopy (from, to, TOOL_BAR_ITEM_NSLOTS * sizeof *to);
7308 ntool_bar_items += TOOL_BAR_ITEM_NSLOTS;
7309 }
7310
7311
7312
7313
7314 \f
7315 /* Read a character using menus based on maps in the array MAPS.
7316 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
7317 Return t if we displayed a menu but the user rejected it.
7318
7319 PREV_EVENT is the previous input event, or nil if we are reading
7320 the first event of a key sequence.
7321
7322 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
7323 if we used a mouse menu to read the input, or zero otherwise. If
7324 USED_MOUSE_MENU is null, we don't dereference it.
7325
7326 The prompting is done based on the prompt-string of the map
7327 and the strings associated with various map elements.
7328
7329 This can be done with X menus or with menus put in the minibuf.
7330 These are done in different ways, depending on how the input will be read.
7331 Menus using X are done after auto-saving in read-char, getting the input
7332 event from Fx_popup_menu; menus using the minibuf use read_char recursively
7333 and do auto-saving in the inner call of read_char. */
7334
7335 static Lisp_Object
7336 read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
7337 int nmaps;
7338 Lisp_Object *maps;
7339 Lisp_Object prev_event;
7340 int *used_mouse_menu;
7341 {
7342 int mapno;
7343 register Lisp_Object name;
7344
7345 if (used_mouse_menu)
7346 *used_mouse_menu = 0;
7347
7348 /* Use local over global Menu maps */
7349
7350 if (! menu_prompting)
7351 return Qnil;
7352
7353 /* Optionally disregard all but the global map. */
7354 if (inhibit_local_menu_bar_menus)
7355 {
7356 maps += (nmaps - 1);
7357 nmaps = 1;
7358 }
7359
7360 /* Get the menu name from the first map that has one (a prompt string). */
7361 for (mapno = 0; mapno < nmaps; mapno++)
7362 {
7363 name = map_prompt (maps[mapno]);
7364 if (!NILP (name))
7365 break;
7366 }
7367
7368 /* If we don't have any menus, just read a character normally. */
7369 if (mapno >= nmaps)
7370 return Qnil;
7371
7372 #ifdef HAVE_MENUS
7373 /* If we got to this point via a mouse click,
7374 use a real menu for mouse selection. */
7375 if (EVENT_HAS_PARAMETERS (prev_event)
7376 && !EQ (XCAR (prev_event), Qmenu_bar)
7377 && !EQ (XCAR (prev_event), Qtool_bar))
7378 {
7379 /* Display the menu and get the selection. */
7380 Lisp_Object *realmaps
7381 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
7382 Lisp_Object value;
7383 int nmaps1 = 0;
7384
7385 /* Use the maps that are not nil. */
7386 for (mapno = 0; mapno < nmaps; mapno++)
7387 if (!NILP (maps[mapno]))
7388 realmaps[nmaps1++] = maps[mapno];
7389
7390 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
7391 if (CONSP (value))
7392 {
7393 Lisp_Object tem;
7394
7395 record_menu_key (XCAR (value));
7396
7397 /* If we got multiple events, unread all but
7398 the first.
7399 There is no way to prevent those unread events
7400 from showing up later in last_nonmenu_event.
7401 So turn symbol and integer events into lists,
7402 to indicate that they came from a mouse menu,
7403 so that when present in last_nonmenu_event
7404 they won't confuse things. */
7405 for (tem = XCDR (value); !NILP (tem);
7406 tem = XCDR (tem))
7407 {
7408 record_menu_key (XCAR (tem));
7409 if (SYMBOLP (XCAR (tem))
7410 || INTEGERP (XCAR (tem)))
7411 XCAR (tem)
7412 = Fcons (XCAR (tem), Qnil);
7413 }
7414
7415 /* If we got more than one event, put all but the first
7416 onto this list to be read later.
7417 Return just the first event now. */
7418 Vunread_command_events
7419 = nconc2 (XCDR (value), Vunread_command_events);
7420 value = XCAR (value);
7421 }
7422 else if (NILP (value))
7423 value = Qt;
7424 if (used_mouse_menu)
7425 *used_mouse_menu = 1;
7426 return value;
7427 }
7428 #endif /* HAVE_MENUS */
7429 return Qnil ;
7430 }
7431
7432 /* Buffer in use so far for the minibuf prompts for menu keymaps.
7433 We make this bigger when necessary, and never free it. */
7434 static char *read_char_minibuf_menu_text;
7435 /* Size of that buffer. */
7436 static int read_char_minibuf_menu_width;
7437
7438 static Lisp_Object
7439 read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
7440 int commandflag ;
7441 int nmaps;
7442 Lisp_Object *maps;
7443 {
7444 int mapno;
7445 register Lisp_Object name;
7446 int nlength;
7447 int width = FRAME_WIDTH (SELECTED_FRAME ()) - 4;
7448 int idx = -1;
7449 int nobindings = 1;
7450 Lisp_Object rest, vector;
7451 char *menu;
7452
7453 vector = Qnil;
7454
7455 if (! menu_prompting)
7456 return Qnil;
7457
7458 /* Make sure we have a big enough buffer for the menu text. */
7459 if (read_char_minibuf_menu_text == 0)
7460 {
7461 read_char_minibuf_menu_width = width + 4;
7462 read_char_minibuf_menu_text = (char *) xmalloc (width + 4);
7463 }
7464 else if (width + 4 > read_char_minibuf_menu_width)
7465 {
7466 read_char_minibuf_menu_width = width + 4;
7467 read_char_minibuf_menu_text
7468 = (char *) xrealloc (read_char_minibuf_menu_text, width + 4);
7469 }
7470 menu = read_char_minibuf_menu_text;
7471
7472 /* Get the menu name from the first map that has one (a prompt string). */
7473 for (mapno = 0; mapno < nmaps; mapno++)
7474 {
7475 name = map_prompt (maps[mapno]);
7476 if (!NILP (name))
7477 break;
7478 }
7479
7480 /* If we don't have any menus, just read a character normally. */
7481 if (mapno >= nmaps)
7482 return Qnil;
7483
7484 /* Prompt string always starts with map's prompt, and a space. */
7485 strcpy (menu, XSTRING (name)->data);
7486 nlength = STRING_BYTES (XSTRING (name));
7487 menu[nlength++] = ':';
7488 menu[nlength++] = ' ';
7489 menu[nlength] = 0;
7490
7491 /* Start prompting at start of first map. */
7492 mapno = 0;
7493 rest = maps[mapno];
7494
7495 /* Present the documented bindings, a line at a time. */
7496 while (1)
7497 {
7498 int notfirst = 0;
7499 int i = nlength;
7500 Lisp_Object obj;
7501 int ch;
7502 Lisp_Object orig_defn_macro;
7503
7504 /* Loop over elements of map. */
7505 while (i < width)
7506 {
7507 Lisp_Object elt;
7508
7509 /* If reached end of map, start at beginning of next map. */
7510 if (NILP (rest))
7511 {
7512 mapno++;
7513 /* At end of last map, wrap around to first map if just starting,
7514 or end this line if already have something on it. */
7515 if (mapno == nmaps)
7516 {
7517 mapno = 0;
7518 if (notfirst || nobindings) break;
7519 }
7520 rest = maps[mapno];
7521 }
7522
7523 /* Look at the next element of the map. */
7524 if (idx >= 0)
7525 elt = XVECTOR (vector)->contents[idx];
7526 else
7527 elt = Fcar_safe (rest);
7528
7529 if (idx < 0 && VECTORP (elt))
7530 {
7531 /* If we found a dense table in the keymap,
7532 advanced past it, but start scanning its contents. */
7533 rest = Fcdr_safe (rest);
7534 vector = elt;
7535 idx = 0;
7536 }
7537 else
7538 {
7539 /* An ordinary element. */
7540 Lisp_Object event, tem;
7541
7542 if (idx < 0)
7543 {
7544 event = Fcar_safe (elt); /* alist */
7545 elt = Fcdr_safe (elt);
7546 }
7547 else
7548 {
7549 XSETINT (event, idx); /* vector */
7550 }
7551
7552 /* Ignore the element if it has no prompt string. */
7553 if (INTEGERP (event) && parse_menu_item (elt, 0, -1))
7554 {
7555 /* 1 if the char to type matches the string. */
7556 int char_matches;
7557 Lisp_Object upcased_event, downcased_event;
7558 Lisp_Object desc = Qnil;
7559 Lisp_Object s
7560 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_NAME];
7561
7562 upcased_event = Fupcase (event);
7563 downcased_event = Fdowncase (event);
7564 char_matches = (XINT (upcased_event) == XSTRING (s)->data[0]
7565 || XINT (downcased_event) == XSTRING (s)->data[0]);
7566 if (! char_matches)
7567 desc = Fsingle_key_description (event, Qnil);
7568
7569 tem
7570 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_KEYEQ];
7571 if (!NILP (tem))
7572 /* Insert equivalent keybinding. */
7573 s = concat2 (s, tem);
7574
7575 tem
7576 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_TYPE];
7577 if (EQ (tem, QCradio) || EQ (tem, QCtoggle))
7578 {
7579 /* Insert button prefix. */
7580 Lisp_Object selected
7581 = XVECTOR (item_properties)->contents[ITEM_PROPERTY_SELECTED];
7582 if (EQ (tem, QCradio))
7583 tem = build_string (NILP (selected) ? "(*) " : "( ) ");
7584 else
7585 tem = build_string (NILP (selected) ? "[X] " : "[ ] ");
7586 s = concat2 (tem, s);
7587 }
7588
7589
7590 /* If we have room for the prompt string, add it to this line.
7591 If this is the first on the line, always add it. */
7592 if ((XSTRING (s)->size + i + 2
7593 + (char_matches ? 0 : XSTRING (desc)->size + 3))
7594 < width
7595 || !notfirst)
7596 {
7597 int thiswidth;
7598
7599 /* Punctuate between strings. */
7600 if (notfirst)
7601 {
7602 strcpy (menu + i, ", ");
7603 i += 2;
7604 }
7605 notfirst = 1;
7606 nobindings = 0 ;
7607
7608 /* If the char to type doesn't match the string's
7609 first char, explicitly show what char to type. */
7610 if (! char_matches)
7611 {
7612 /* Add as much of string as fits. */
7613 thiswidth = XSTRING (desc)->size;
7614 if (thiswidth + i > width)
7615 thiswidth = width - i;
7616 bcopy (XSTRING (desc)->data, menu + i, thiswidth);
7617 i += thiswidth;
7618 strcpy (menu + i, " = ");
7619 i += 3;
7620 }
7621
7622 /* Add as much of string as fits. */
7623 thiswidth = XSTRING (s)->size;
7624 if (thiswidth + i > width)
7625 thiswidth = width - i;
7626 bcopy (XSTRING (s)->data, menu + i, thiswidth);
7627 i += thiswidth;
7628 menu[i] = 0;
7629 }
7630 else
7631 {
7632 /* If this element does not fit, end the line now,
7633 and save the element for the next line. */
7634 strcpy (menu + i, "...");
7635 break;
7636 }
7637 }
7638
7639 /* Move past this element. */
7640 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
7641 /* Handle reaching end of dense table. */
7642 idx = -1;
7643 if (idx >= 0)
7644 idx++;
7645 else
7646 rest = Fcdr_safe (rest);
7647 }
7648 }
7649
7650 /* Prompt with that and read response. */
7651 message2_nolog (menu, strlen (menu),
7652 ! NILP (current_buffer->enable_multibyte_characters));
7653
7654 /* Make believe its not a keyboard macro in case the help char
7655 is pressed. Help characters are not recorded because menu prompting
7656 is not used on replay.
7657 */
7658 orig_defn_macro = current_kboard->defining_kbd_macro;
7659 current_kboard->defining_kbd_macro = Qnil;
7660 do
7661 obj = read_char (commandflag, 0, 0, Qt, 0);
7662 while (BUFFERP (obj));
7663 current_kboard->defining_kbd_macro = orig_defn_macro;
7664
7665 if (!INTEGERP (obj))
7666 return obj;
7667 else
7668 ch = XINT (obj);
7669
7670 if (! EQ (obj, menu_prompt_more_char)
7671 && (!INTEGERP (menu_prompt_more_char)
7672 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
7673 {
7674 if (!NILP (current_kboard->defining_kbd_macro))
7675 store_kbd_macro_char (obj);
7676 return obj;
7677 }
7678 /* Help char - go round again */
7679 }
7680 }
7681 \f
7682 /* Reading key sequences. */
7683
7684 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
7685 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
7686 keymap, or nil otherwise. Return the index of the first keymap in
7687 which KEY has any binding, or NMAPS if no map has a binding.
7688
7689 If KEY is a meta ASCII character, treat it like meta-prefix-char
7690 followed by the corresponding non-meta character. Keymaps in
7691 CURRENT with non-prefix bindings for meta-prefix-char become nil in
7692 NEXT.
7693
7694 If KEY has no bindings in any of the CURRENT maps, NEXT is left
7695 unmodified.
7696
7697 NEXT may be the same array as CURRENT. */
7698
7699 static int
7700 follow_key (key, nmaps, current, defs, next)
7701 Lisp_Object key;
7702 Lisp_Object *current, *defs, *next;
7703 int nmaps;
7704 {
7705 int i, first_binding;
7706 int did_meta = 0;
7707
7708 first_binding = nmaps;
7709 for (i = nmaps - 1; i >= 0; i--)
7710 {
7711 if (! NILP (current[i]))
7712 {
7713 Lisp_Object map;
7714 if (did_meta)
7715 map = defs[i];
7716 else
7717 map = current[i];
7718
7719 defs[i] = access_keymap (map, key, 1, 0, 1);
7720 if (! NILP (defs[i]))
7721 first_binding = i;
7722 }
7723 else
7724 defs[i] = Qnil;
7725 }
7726
7727 /* Given the set of bindings we've found, produce the next set of maps. */
7728 if (first_binding < nmaps)
7729 for (i = 0; i < nmaps; i++)
7730 next[i] = NILP (defs[i]) ? Qnil : get_keymap (defs[i], 0, 1);
7731
7732 return first_binding;
7733 }
7734
7735 /* Read a sequence of keys that ends with a non prefix character,
7736 storing it in KEYBUF, a buffer of size BUFSIZE.
7737 Prompt with PROMPT.
7738 Return the length of the key sequence stored.
7739 Return -1 if the user rejected a command menu.
7740
7741 Echo starting immediately unless `prompt' is 0.
7742
7743 Where a key sequence ends depends on the currently active keymaps.
7744 These include any minor mode keymaps active in the current buffer,
7745 the current buffer's local map, and the global map.
7746
7747 If a key sequence has no other bindings, we check Vfunction_key_map
7748 to see if some trailing subsequence might be the beginning of a
7749 function key's sequence. If so, we try to read the whole function
7750 key, and substitute its symbolic name into the key sequence.
7751
7752 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
7753 `double-' events into similar click events, if that would make them
7754 bound. We try to turn `triple-' events first into `double-' events,
7755 then into clicks.
7756
7757 If we get a mouse click in a mode line, vertical divider, or other
7758 non-text area, we treat the click as if it were prefixed by the
7759 symbol denoting that area - `mode-line', `vertical-line', or
7760 whatever.
7761
7762 If the sequence starts with a mouse click, we read the key sequence
7763 with respect to the buffer clicked on, not the current buffer.
7764
7765 If the user switches frames in the midst of a key sequence, we put
7766 off the switch-frame event until later; the next call to
7767 read_char will return it.
7768
7769 If FIX_CURRENT_BUFFER is nonzero, we restore current_buffer
7770 from the selected window's buffer. */
7771
7772 static int
7773 read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
7774 can_return_switch_frame, fix_current_buffer)
7775 Lisp_Object *keybuf;
7776 int bufsize;
7777 Lisp_Object prompt;
7778 int dont_downcase_last;
7779 int can_return_switch_frame;
7780 int fix_current_buffer;
7781 {
7782 volatile Lisp_Object from_string;
7783 volatile int count = specpdl_ptr - specpdl;
7784
7785 /* How many keys there are in the current key sequence. */
7786 volatile int t;
7787
7788 /* The length of the echo buffer when we started reading, and
7789 the length of this_command_keys when we started reading. */
7790 volatile int echo_start;
7791 volatile int keys_start;
7792
7793 /* The number of keymaps we're scanning right now, and the number of
7794 keymaps we have allocated space for. */
7795 volatile int nmaps;
7796 volatile int nmaps_allocated = 0;
7797
7798 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
7799 the current keymaps. */
7800 Lisp_Object *volatile defs = NULL;
7801
7802 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
7803 in the current keymaps, or nil where it is not a prefix. */
7804 Lisp_Object *volatile submaps = NULL;
7805
7806 /* The local map to start out with at start of key sequence. */
7807 volatile Lisp_Object orig_local_map;
7808
7809 /* The map from the `keymap' property to start out with at start of
7810 key sequence. */
7811 volatile Lisp_Object orig_keymap;
7812
7813 /* 1 if we have already considered switching to the local-map property
7814 of the place where a mouse click occurred. */
7815 volatile int localized_local_map = 0;
7816
7817 /* The index in defs[] of the first keymap that has a binding for
7818 this key sequence. In other words, the lowest i such that
7819 defs[i] is non-nil. */
7820 volatile int first_binding;
7821
7822 /* If t < mock_input, then KEYBUF[t] should be read as the next
7823 input key.
7824
7825 We use this to recover after recognizing a function key. Once we
7826 realize that a suffix of the current key sequence is actually a
7827 function key's escape sequence, we replace the suffix with the
7828 function key's binding from Vfunction_key_map. Now keybuf
7829 contains a new and different key sequence, so the echo area,
7830 this_command_keys, and the submaps and defs arrays are wrong. In
7831 this situation, we set mock_input to t, set t to 0, and jump to
7832 restart_sequence; the loop will read keys from keybuf up until
7833 mock_input, thus rebuilding the state; and then it will resume
7834 reading characters from the keyboard. */
7835 volatile int mock_input = 0;
7836
7837 /* If the sequence is unbound in submaps[], then
7838 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
7839 and fkey_map is its binding.
7840
7841 These might be > t, indicating that all function key scanning
7842 should hold off until t reaches them. We do this when we've just
7843 recognized a function key, to avoid searching for the function
7844 key's again in Vfunction_key_map. */
7845 volatile int fkey_start = 0, fkey_end = 0;
7846 volatile Lisp_Object fkey_map;
7847
7848 /* Likewise, for key_translation_map. */
7849 volatile int keytran_start = 0, keytran_end = 0;
7850 volatile Lisp_Object keytran_map;
7851
7852 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
7853 we put it off for later. While we're reading, we keep the event here. */
7854 volatile Lisp_Object delayed_switch_frame;
7855
7856 /* See the comment below... */
7857 #if defined (GOBBLE_FIRST_EVENT)
7858 Lisp_Object first_event;
7859 #endif
7860
7861 volatile Lisp_Object original_uppercase;
7862 volatile int original_uppercase_position = -1;
7863
7864 /* Gets around Microsoft compiler limitations. */
7865 int dummyflag = 0;
7866
7867 struct buffer *starting_buffer;
7868
7869 /* Nonzero if we seem to have got the beginning of a binding
7870 in function_key_map. */
7871 volatile int function_key_possible = 0;
7872 volatile int key_translation_possible = 0;
7873
7874 /* List of events for which a fake prefix key has been generated. */
7875 volatile Lisp_Object fake_prefixed_keys = Qnil;
7876
7877 /* Save the status of key translation before each step,
7878 so that we can restore this after downcasing. */
7879 Lisp_Object prev_fkey_map;
7880 int prev_fkey_start;
7881 int prev_fkey_end;
7882
7883 Lisp_Object prev_keytran_map;
7884 int prev_keytran_start;
7885 int prev_keytran_end;
7886
7887 #if defined (GOBBLE_FIRST_EVENT)
7888 int junk;
7889 #endif
7890
7891 struct gcpro gcpro1;
7892
7893 GCPRO1 (fake_prefixed_keys);
7894 raw_keybuf_count = 0;
7895
7896 last_nonmenu_event = Qnil;
7897
7898 delayed_switch_frame = Qnil;
7899 fkey_map = Vfunction_key_map;
7900 keytran_map = Vkey_translation_map;
7901
7902 /* If there is no function-key-map, turn off function key scanning. */
7903 if (!KEYMAPP (Vfunction_key_map))
7904 fkey_start = fkey_end = bufsize + 1;
7905
7906 /* If there is no key-translation-map, turn off scanning. */
7907 if (!KEYMAPP (Vkey_translation_map))
7908 keytran_start = keytran_end = bufsize + 1;
7909
7910 if (INTERACTIVE)
7911 {
7912 if (!NILP (prompt))
7913 echo_prompt (prompt);
7914 else if (cursor_in_echo_area
7915 && (FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
7916 && NILP (Fzerop (Vecho_keystrokes)))
7917 /* This doesn't put in a dash if the echo buffer is empty, so
7918 you don't always see a dash hanging out in the minibuffer. */
7919 echo_dash ();
7920 }
7921
7922 /* Record the initial state of the echo area and this_command_keys;
7923 we will need to restore them if we replay a key sequence. */
7924 if (INTERACTIVE)
7925 echo_start = echo_length ();
7926 keys_start = this_command_key_count;
7927 this_single_command_key_start = keys_start;
7928
7929 #if defined (GOBBLE_FIRST_EVENT)
7930 /* This doesn't quite work, because some of the things that read_char
7931 does cannot safely be bypassed. It seems too risky to try to make
7932 this work right. */
7933
7934 /* Read the first char of the sequence specially, before setting
7935 up any keymaps, in case a filter runs and switches buffers on us. */
7936 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
7937 &junk);
7938 #endif /* GOBBLE_FIRST_EVENT */
7939
7940 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
7941 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
7942 from_string = Qnil;
7943
7944 /* We jump here when the key sequence has been thoroughly changed, and
7945 we need to rescan it starting from the beginning. When we jump here,
7946 keybuf[0..mock_input] holds the sequence we should reread. */
7947 replay_sequence:
7948
7949 starting_buffer = current_buffer;
7950 function_key_possible = 0;
7951 key_translation_possible = 0;
7952
7953 /* Build our list of keymaps.
7954 If we recognize a function key and replace its escape sequence in
7955 keybuf with its symbol, or if the sequence starts with a mouse
7956 click and we need to switch buffers, we jump back here to rebuild
7957 the initial keymaps from the current buffer. */
7958 {
7959 Lisp_Object *maps;
7960
7961 if (!NILP (current_kboard->Voverriding_terminal_local_map)
7962 || !NILP (Voverriding_local_map))
7963 {
7964 if (3 > nmaps_allocated)
7965 {
7966 submaps = (Lisp_Object *) alloca (3 * sizeof (submaps[0]));
7967 defs = (Lisp_Object *) alloca (3 * sizeof (defs[0]));
7968 nmaps_allocated = 3;
7969 }
7970 nmaps = 0;
7971 if (!NILP (current_kboard->Voverriding_terminal_local_map))
7972 submaps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
7973 if (!NILP (Voverriding_local_map))
7974 submaps[nmaps++] = Voverriding_local_map;
7975 }
7976 else
7977 {
7978 int extra_maps = 2;
7979 nmaps = current_minor_maps (0, &maps);
7980 if (!NILP (orig_keymap))
7981 extra_maps = 3;
7982 if (nmaps + extra_maps > nmaps_allocated)
7983 {
7984 submaps = (Lisp_Object *) alloca ((nmaps+extra_maps)
7985 * sizeof (submaps[0]));
7986 defs = (Lisp_Object *) alloca ((nmaps+extra_maps)
7987 * sizeof (defs[0]));
7988 nmaps_allocated = nmaps + extra_maps;
7989 }
7990 bcopy (maps, (void *) submaps, nmaps * sizeof (submaps[0]));
7991 if (!NILP (orig_keymap))
7992 submaps[nmaps++] = orig_keymap;
7993 submaps[nmaps++] = orig_local_map;
7994 }
7995 submaps[nmaps++] = current_global_map;
7996 }
7997
7998 /* Find an accurate initial value for first_binding. */
7999 for (first_binding = 0; first_binding < nmaps; first_binding++)
8000 if (! NILP (submaps[first_binding]))
8001 break;
8002
8003 /* Start from the beginning in keybuf. */
8004 t = 0;
8005
8006 /* These are no-ops the first time through, but if we restart, they
8007 revert the echo area and this_command_keys to their original state. */
8008 this_command_key_count = keys_start;
8009 if (INTERACTIVE && t < mock_input)
8010 echo_truncate (echo_start);
8011
8012 /* If the best binding for the current key sequence is a keymap, or
8013 we may be looking at a function key's escape sequence, keep on
8014 reading. */
8015 while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
8016 || (first_binding >= nmaps
8017 && fkey_start < t
8018 /* mock input is never part of a function key's sequence. */
8019 && mock_input <= fkey_start)
8020 || (first_binding >= nmaps
8021 && keytran_start < t && key_translation_possible)
8022 /* Don't return in the middle of a possible function key sequence,
8023 if the only bindings we found were via case conversion.
8024 Thus, if ESC O a has a function-key-map translation
8025 and ESC o has a binding, don't return after ESC O,
8026 so that we can translate ESC O plus the next character. */
8027 )
8028 {
8029 Lisp_Object key;
8030 int used_mouse_menu = 0;
8031
8032 /* Where the last real key started. If we need to throw away a
8033 key that has expanded into more than one element of keybuf
8034 (say, a mouse click on the mode line which is being treated
8035 as [mode-line (mouse-...)], then we backtrack to this point
8036 of keybuf. */
8037 volatile int last_real_key_start;
8038
8039 /* These variables are analogous to echo_start and keys_start;
8040 while those allow us to restart the entire key sequence,
8041 echo_local_start and keys_local_start allow us to throw away
8042 just one key. */
8043 volatile int echo_local_start, keys_local_start, local_first_binding;
8044
8045 if (t >= bufsize)
8046 error ("Key sequence too long");
8047
8048 if (INTERACTIVE)
8049 echo_local_start = echo_length ();
8050 keys_local_start = this_command_key_count;
8051 local_first_binding = first_binding;
8052
8053 replay_key:
8054 /* These are no-ops, unless we throw away a keystroke below and
8055 jumped back up to replay_key; in that case, these restore the
8056 variables to their original state, allowing us to replay the
8057 loop. */
8058 if (INTERACTIVE && t < mock_input)
8059 echo_truncate (echo_local_start);
8060 this_command_key_count = keys_local_start;
8061 first_binding = local_first_binding;
8062
8063 /* By default, assume each event is "real". */
8064 last_real_key_start = t;
8065
8066 /* Does mock_input indicate that we are re-reading a key sequence? */
8067 if (t < mock_input)
8068 {
8069 key = keybuf[t];
8070 add_command_key (key);
8071 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
8072 && NILP (Fzerop (Vecho_keystrokes)))
8073 echo_char (key);
8074 }
8075
8076 /* If not, we should actually read a character. */
8077 else
8078 {
8079 {
8080 #ifdef MULTI_KBOARD
8081 KBOARD *interrupted_kboard = current_kboard;
8082 struct frame *interrupted_frame = SELECTED_FRAME ();
8083 if (setjmp (wrong_kboard_jmpbuf))
8084 {
8085 if (!NILP (delayed_switch_frame))
8086 {
8087 interrupted_kboard->kbd_queue
8088 = Fcons (delayed_switch_frame,
8089 interrupted_kboard->kbd_queue);
8090 delayed_switch_frame = Qnil;
8091 }
8092 while (t > 0)
8093 interrupted_kboard->kbd_queue
8094 = Fcons (keybuf[--t], interrupted_kboard->kbd_queue);
8095
8096 /* If the side queue is non-empty, ensure it begins with a
8097 switch-frame, so we'll replay it in the right context. */
8098 if (CONSP (interrupted_kboard->kbd_queue)
8099 && (key = XCAR (interrupted_kboard->kbd_queue),
8100 !(EVENT_HAS_PARAMETERS (key)
8101 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)),
8102 Qswitch_frame))))
8103 {
8104 Lisp_Object frame;
8105 XSETFRAME (frame, interrupted_frame);
8106 interrupted_kboard->kbd_queue
8107 = Fcons (make_lispy_switch_frame (frame),
8108 interrupted_kboard->kbd_queue);
8109 }
8110 mock_input = 0;
8111 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
8112 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8113 goto replay_sequence;
8114 }
8115 #endif
8116 key = read_char (NILP (prompt), nmaps,
8117 (Lisp_Object *) submaps, last_nonmenu_event,
8118 &used_mouse_menu);
8119 }
8120
8121 /* read_char returns t when it shows a menu and the user rejects it.
8122 Just return -1. */
8123 if (EQ (key, Qt))
8124 {
8125 unbind_to (count, Qnil);
8126 UNGCPRO;
8127 return -1;
8128 }
8129
8130 /* read_char returns -1 at the end of a macro.
8131 Emacs 18 handles this by returning immediately with a
8132 zero, so that's what we'll do. */
8133 if (INTEGERP (key) && XINT (key) == -1)
8134 {
8135 t = 0;
8136 /* The Microsoft C compiler can't handle the goto that
8137 would go here. */
8138 dummyflag = 1;
8139 break;
8140 }
8141
8142 /* If the current buffer has been changed from under us, the
8143 keymap may have changed, so replay the sequence. */
8144 if (BUFFERP (key))
8145 {
8146 mock_input = t;
8147 /* Reset the current buffer from the selected window
8148 in case something changed the former and not the latter.
8149 This is to be more consistent with the behavior
8150 of the command_loop_1. */
8151 if (fix_current_buffer)
8152 {
8153 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8154 Fkill_emacs (Qnil);
8155 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
8156 Fset_buffer (XWINDOW (selected_window)->buffer);
8157 }
8158
8159 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
8160 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8161 goto replay_sequence;
8162 }
8163
8164 /* If we have a quit that was typed in another frame, and
8165 quit_throw_to_read_char switched buffers,
8166 replay to get the right keymap. */
8167 if (XINT (key) == quit_char && current_buffer != starting_buffer)
8168 {
8169 GROW_RAW_KEYBUF;
8170 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
8171 keybuf[t++] = key;
8172 mock_input = t;
8173 Vquit_flag = Qnil;
8174 orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
8175 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8176 goto replay_sequence;
8177 }
8178
8179 Vquit_flag = Qnil;
8180
8181 if (EVENT_HAS_PARAMETERS (key)
8182 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)), Qswitch_frame))
8183 {
8184 /* If we're at the beginning of a key sequence, and the caller
8185 says it's okay, go ahead and return this event. If we're
8186 in the midst of a key sequence, delay it until the end. */
8187 if (t > 0 || !can_return_switch_frame)
8188 {
8189 delayed_switch_frame = key;
8190 goto replay_key;
8191 }
8192 }
8193
8194 GROW_RAW_KEYBUF;
8195 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
8196 }
8197
8198 /* Clicks in non-text areas get prefixed by the symbol
8199 in their CHAR-ADDRESS field. For example, a click on
8200 the mode line is prefixed by the symbol `mode-line'.
8201
8202 Furthermore, key sequences beginning with mouse clicks
8203 are read using the keymaps of the buffer clicked on, not
8204 the current buffer. So we may have to switch the buffer
8205 here.
8206
8207 When we turn one event into two events, we must make sure
8208 that neither of the two looks like the original--so that,
8209 if we replay the events, they won't be expanded again.
8210 If not for this, such reexpansion could happen either here
8211 or when user programs play with this-command-keys. */
8212 if (EVENT_HAS_PARAMETERS (key))
8213 {
8214 Lisp_Object kind;
8215
8216 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
8217 if (EQ (kind, Qmouse_click))
8218 {
8219 Lisp_Object window, posn;
8220
8221 window = POSN_WINDOW (EVENT_START (key));
8222 posn = POSN_BUFFER_POSN (EVENT_START (key));
8223
8224 if (CONSP (posn)
8225 || (!NILP (fake_prefixed_keys)
8226 && !NILP (Fmemq (key, fake_prefixed_keys))))
8227 {
8228 /* We're looking a second time at an event for which
8229 we generated a fake prefix key. Set
8230 last_real_key_start appropriately. */
8231 if (t > 0)
8232 last_real_key_start = t - 1;
8233 }
8234
8235 /* Key sequences beginning with mouse clicks are
8236 read using the keymaps in the buffer clicked on,
8237 not the current buffer. If we're at the
8238 beginning of a key sequence, switch buffers. */
8239 if (last_real_key_start == 0
8240 && WINDOWP (window)
8241 && BUFFERP (XWINDOW (window)->buffer)
8242 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
8243 {
8244 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
8245 keybuf[t] = key;
8246 mock_input = t + 1;
8247
8248 /* Arrange to go back to the original buffer once we're
8249 done reading the key sequence. Note that we can't
8250 use save_excursion_{save,restore} here, because they
8251 save point as well as the current buffer; we don't
8252 want to save point, because redisplay may change it,
8253 to accommodate a Fset_window_start or something. We
8254 don't want to do this at the top of the function,
8255 because we may get input from a subprocess which
8256 wants to change the selected window and stuff (say,
8257 emacsclient). */
8258 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
8259
8260 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8261 Fkill_emacs (Qnil);
8262 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
8263 orig_local_map = get_local_map (PT, current_buffer,
8264 Qlocal_map);
8265 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
8266 goto replay_sequence;
8267 }
8268
8269 /* For a mouse click, get the local text-property keymap
8270 of the place clicked on, rather than point. */
8271 if (last_real_key_start == 0
8272 && CONSP (XCDR (key))
8273 && ! localized_local_map)
8274 {
8275 Lisp_Object map_here, start, pos;
8276
8277 localized_local_map = 1;
8278 start = EVENT_START (key);
8279
8280 if (CONSP (start) && CONSP (XCDR (start)))
8281 {
8282 pos = POSN_BUFFER_POSN (start);
8283 if (INTEGERP (pos)
8284 && XINT (pos) >= BEG && XINT (pos) <= Z)
8285 {
8286 map_here = get_local_map (XINT (pos),
8287 current_buffer, Qlocal_map);
8288 if (!EQ (map_here, orig_local_map))
8289 {
8290 orig_local_map = map_here;
8291 keybuf[t] = key;
8292 mock_input = t + 1;
8293
8294 goto replay_sequence;
8295 }
8296 map_here = get_local_map (XINT (pos),
8297 current_buffer, Qkeymap);
8298 if (!EQ (map_here, orig_keymap))
8299 {
8300 orig_keymap = map_here;
8301 keybuf[t] = key;
8302 mock_input = t + 1;
8303
8304 goto replay_sequence;
8305 }
8306 }
8307 }
8308 }
8309
8310 /* Expand mode-line and scroll-bar events into two events:
8311 use posn as a fake prefix key. */
8312 if (SYMBOLP (posn)
8313 && (NILP (fake_prefixed_keys)
8314 || NILP (Fmemq (key, fake_prefixed_keys))))
8315 {
8316 if (t + 1 >= bufsize)
8317 error ("Key sequence too long");
8318
8319 keybuf[t] = posn;
8320 keybuf[t + 1] = key;
8321 mock_input = t + 2;
8322
8323 /* Record that a fake prefix key has been generated
8324 for KEY. Don't modify the event; this would
8325 prevent proper action when the event is pushed
8326 back tino unread-command-events. */
8327 fake_prefixed_keys = Fcons (key, fake_prefixed_keys);
8328
8329 /* If on a mode line string with a local keymap,
8330 reconsider the key sequence with that keymap. */
8331 if (CONSP (POSN_STRING (EVENT_START (key))))
8332 {
8333 Lisp_Object string, pos, map, map2;
8334
8335 string = POSN_STRING (EVENT_START (key));
8336 pos = XCDR (string);
8337 string = XCAR (string);
8338 if (XINT (pos) >= 0
8339 && XINT (pos) < XSTRING (string)->size)
8340 {
8341 map = Fget_text_property (pos, Qlocal_map, string);
8342 if (!NILP (map))
8343 orig_local_map = map;
8344 map2 = Fget_text_property (pos, Qkeymap, string);
8345 if (!NILP (map2))
8346 orig_keymap = map2;
8347 if (!NILP (map) || !NILP (map2))
8348 goto replay_sequence;
8349 }
8350 }
8351
8352 goto replay_key;
8353 }
8354 else if (CONSP (POSN_STRING (EVENT_START (key)))
8355 && NILP (from_string))
8356 {
8357 /* For a click on a string, i.e. overlay string or a
8358 string displayed via the `display' property,
8359 consider `local-map' and `keymap' properties of
8360 that string. */
8361 Lisp_Object string, pos, map, map2;
8362
8363 string = POSN_STRING (EVENT_START (key));
8364 pos = XCDR (string);
8365 string = XCAR (string);
8366 if (XINT (pos) >= 0
8367 && XINT (pos) < XSTRING (string)->size)
8368 {
8369 map = Fget_text_property (pos, Qlocal_map, string);
8370 if (!NILP (map))
8371 orig_local_map = map;
8372 map2 = Fget_text_property (pos, Qkeymap, string);
8373 if (!NILP (map2))
8374 orig_keymap = map2;
8375
8376 if (!NILP (map) || !NILP (map2))
8377 {
8378 from_string = string;
8379 goto replay_sequence;
8380 }
8381 }
8382 }
8383 }
8384 else if (CONSP (XCDR (key))
8385 && CONSP (EVENT_START (key))
8386 && CONSP (XCDR (EVENT_START (key))))
8387 {
8388 Lisp_Object posn;
8389
8390 posn = POSN_BUFFER_POSN (EVENT_START (key));
8391 /* Handle menu-bar events:
8392 insert the dummy prefix event `menu-bar'. */
8393 if (EQ (posn, Qmenu_bar) || EQ (posn, Qtool_bar))
8394 {
8395 if (t + 1 >= bufsize)
8396 error ("Key sequence too long");
8397 keybuf[t] = posn;
8398 keybuf[t+1] = key;
8399
8400 /* Zap the position in key, so we know that we've
8401 expanded it, and don't try to do so again. */
8402 POSN_BUFFER_POSN (EVENT_START (key))
8403 = Fcons (posn, Qnil);
8404
8405 mock_input = t + 2;
8406 goto replay_sequence;
8407 }
8408 else if (CONSP (posn))
8409 {
8410 /* We're looking at the second event of a
8411 sequence which we expanded before. Set
8412 last_real_key_start appropriately. */
8413 if (last_real_key_start == t && t > 0)
8414 last_real_key_start = t - 1;
8415 }
8416 }
8417 }
8418
8419 /* We have finally decided that KEY is something we might want
8420 to look up. */
8421 first_binding = (follow_key (key,
8422 nmaps - first_binding,
8423 submaps + first_binding,
8424 defs + first_binding,
8425 submaps + first_binding)
8426 + first_binding);
8427
8428 /* If KEY wasn't bound, we'll try some fallbacks. */
8429 if (first_binding >= nmaps)
8430 {
8431 Lisp_Object head;
8432
8433 head = EVENT_HEAD (key);
8434 if (help_char_p (head) && t > 0)
8435 {
8436 read_key_sequence_cmd = Vprefix_help_command;
8437 keybuf[t++] = key;
8438 last_nonmenu_event = key;
8439 /* The Microsoft C compiler can't handle the goto that
8440 would go here. */
8441 dummyflag = 1;
8442 break;
8443 }
8444
8445 if (SYMBOLP (head))
8446 {
8447 Lisp_Object breakdown;
8448 int modifiers;
8449
8450 breakdown = parse_modifiers (head);
8451 modifiers = XINT (XCAR (XCDR (breakdown)));
8452 /* Attempt to reduce an unbound mouse event to a simpler
8453 event that is bound:
8454 Drags reduce to clicks.
8455 Double-clicks reduce to clicks.
8456 Triple-clicks reduce to double-clicks, then to clicks.
8457 Down-clicks are eliminated.
8458 Double-downs reduce to downs, then are eliminated.
8459 Triple-downs reduce to double-downs, then to downs,
8460 then are eliminated. */
8461 if (modifiers & (down_modifier | drag_modifier
8462 | double_modifier | triple_modifier))
8463 {
8464 while (modifiers & (down_modifier | drag_modifier
8465 | double_modifier | triple_modifier))
8466 {
8467 Lisp_Object new_head, new_click;
8468 if (modifiers & triple_modifier)
8469 modifiers ^= (double_modifier | triple_modifier);
8470 else if (modifiers & double_modifier)
8471 modifiers &= ~double_modifier;
8472 else if (modifiers & drag_modifier)
8473 modifiers &= ~drag_modifier;
8474 else
8475 {
8476 /* Dispose of this `down' event by simply jumping
8477 back to replay_key, to get another event.
8478
8479 Note that if this event came from mock input,
8480 then just jumping back to replay_key will just
8481 hand it to us again. So we have to wipe out any
8482 mock input.
8483
8484 We could delete keybuf[t] and shift everything
8485 after that to the left by one spot, but we'd also
8486 have to fix up any variable that points into
8487 keybuf, and shifting isn't really necessary
8488 anyway.
8489
8490 Adding prefixes for non-textual mouse clicks
8491 creates two characters of mock input, and both
8492 must be thrown away. If we're only looking at
8493 the prefix now, we can just jump back to
8494 replay_key. On the other hand, if we've already
8495 processed the prefix, and now the actual click
8496 itself is giving us trouble, then we've lost the
8497 state of the keymaps we want to backtrack to, and
8498 we need to replay the whole sequence to rebuild
8499 it.
8500
8501 Beyond that, only function key expansion could
8502 create more than two keys, but that should never
8503 generate mouse events, so it's okay to zero
8504 mock_input in that case too.
8505
8506 Isn't this just the most wonderful code ever? */
8507 if (t == last_real_key_start)
8508 {
8509 mock_input = 0;
8510 goto replay_key;
8511 }
8512 else
8513 {
8514 mock_input = last_real_key_start;
8515 goto replay_sequence;
8516 }
8517 }
8518
8519 new_head
8520 = apply_modifiers (modifiers, XCAR (breakdown));
8521 new_click
8522 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
8523
8524 /* Look for a binding for this new key. follow_key
8525 promises that it didn't munge submaps the
8526 last time we called it, since key was unbound. */
8527 first_binding
8528 = (follow_key (new_click,
8529 nmaps - local_first_binding,
8530 submaps + local_first_binding,
8531 defs + local_first_binding,
8532 submaps + local_first_binding)
8533 + local_first_binding);
8534
8535 /* If that click is bound, go for it. */
8536 if (first_binding < nmaps)
8537 {
8538 key = new_click;
8539 break;
8540 }
8541 /* Otherwise, we'll leave key set to the drag event. */
8542 }
8543 }
8544 }
8545 }
8546
8547 keybuf[t++] = key;
8548 /* Normally, last_nonmenu_event gets the previous key we read.
8549 But when a mouse popup menu is being used,
8550 we don't update last_nonmenu_event; it continues to hold the mouse
8551 event that preceded the first level of menu. */
8552 if (!used_mouse_menu)
8553 last_nonmenu_event = key;
8554
8555 /* Record what part of this_command_keys is the current key sequence. */
8556 this_single_command_key_start = this_command_key_count - t;
8557
8558 prev_fkey_map = fkey_map;
8559 prev_fkey_start = fkey_start;
8560 prev_fkey_end = fkey_end;
8561
8562 prev_keytran_map = keytran_map;
8563 prev_keytran_start = keytran_start;
8564 prev_keytran_end = keytran_end;
8565
8566 /* If the sequence is unbound, see if we can hang a function key
8567 off the end of it. We only want to scan real keyboard input
8568 for function key sequences, so if mock_input says that we're
8569 re-reading old events, don't examine it. */
8570 if (first_binding >= nmaps
8571 && t >= mock_input)
8572 {
8573 Lisp_Object fkey_next;
8574
8575 /* Continue scan from fkey_end until we find a bound suffix.
8576 If we fail, increment fkey_start
8577 and start fkey_end from there. */
8578 while (fkey_end < t)
8579 {
8580 Lisp_Object key;
8581
8582 key = keybuf[fkey_end++];
8583 fkey_next
8584 = access_keymap (fkey_map, key, 1, 0, 1);
8585
8586 /* Handle symbol with autoload definition. */
8587 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
8588 && CONSP (XSYMBOL (fkey_next)->function)
8589 && EQ (XCAR (XSYMBOL (fkey_next)->function), Qautoload))
8590 do_autoload (XSYMBOL (fkey_next)->function,
8591 fkey_next);
8592
8593 /* Handle a symbol whose function definition is a keymap
8594 or an array. */
8595 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
8596 && (!NILP (Farrayp (XSYMBOL (fkey_next)->function))
8597 || KEYMAPP (XSYMBOL (fkey_next)->function)))
8598 fkey_next = XSYMBOL (fkey_next)->function;
8599
8600 #if 0 /* I didn't turn this on, because it might cause trouble
8601 for the mapping of return into C-m and tab into C-i. */
8602 /* Optionally don't map function keys into other things.
8603 This enables the user to redefine kp- keys easily. */
8604 if (SYMBOLP (key) && !NILP (Vinhibit_function_key_mapping))
8605 fkey_next = Qnil;
8606 #endif
8607
8608 /* If the function key map gives a function, not an
8609 array, then call the function with no args and use
8610 its value instead. */
8611 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
8612 && fkey_end == t)
8613 {
8614 struct gcpro gcpro1, gcpro2, gcpro3;
8615 Lisp_Object tem;
8616 tem = fkey_next;
8617
8618 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
8619 fkey_next = call1 (fkey_next, prompt);
8620 UNGCPRO;
8621 /* If the function returned something invalid,
8622 barf--don't ignore it.
8623 (To ignore it safely, we would need to gcpro a bunch of
8624 other variables.) */
8625 if (! (VECTORP (fkey_next) || STRINGP (fkey_next)))
8626 error ("Function in key-translation-map returns invalid key sequence");
8627 }
8628
8629 function_key_possible = ! NILP (fkey_next);
8630
8631 /* If keybuf[fkey_start..fkey_end] is bound in the
8632 function key map and it's a suffix of the current
8633 sequence (i.e. fkey_end == t), replace it with
8634 the binding and restart with fkey_start at the end. */
8635 if ((VECTORP (fkey_next) || STRINGP (fkey_next))
8636 && fkey_end == t)
8637 {
8638 int len = XFASTINT (Flength (fkey_next));
8639
8640 t = fkey_start + len;
8641 if (t >= bufsize)
8642 error ("Key sequence too long");
8643
8644 if (VECTORP (fkey_next))
8645 bcopy (XVECTOR (fkey_next)->contents,
8646 keybuf + fkey_start,
8647 (t - fkey_start) * sizeof (keybuf[0]));
8648 else if (STRINGP (fkey_next))
8649 {
8650 int i;
8651
8652 for (i = 0; i < len; i++)
8653 XSETFASTINT (keybuf[fkey_start + i],
8654 XSTRING (fkey_next)->data[i]);
8655 }
8656
8657 mock_input = t;
8658 fkey_start = fkey_end = t;
8659 fkey_map = Vfunction_key_map;
8660
8661 /* Do pass the results through key-translation-map.
8662 But don't retranslate what key-translation-map
8663 has already translated. */
8664 keytran_end = keytran_start;
8665 keytran_map = Vkey_translation_map;
8666
8667 goto replay_sequence;
8668 }
8669
8670 fkey_map = get_keymap (fkey_next, 0, 1);
8671
8672 /* If we no longer have a bound suffix, try a new positions for
8673 fkey_start. */
8674 if (!CONSP (fkey_map))
8675 {
8676 fkey_end = ++fkey_start;
8677 fkey_map = Vfunction_key_map;
8678 function_key_possible = 0;
8679 }
8680 }
8681 }
8682
8683 /* Look for this sequence in key-translation-map. */
8684 {
8685 Lisp_Object keytran_next;
8686
8687 /* Scan from keytran_end until we find a bound suffix. */
8688 while (keytran_end < t)
8689 {
8690 Lisp_Object key;
8691
8692 key = keybuf[keytran_end++];
8693 keytran_next
8694 = access_keymap (keytran_map, key, 1, 0, 1);
8695
8696 /* Handle symbol with autoload definition. */
8697 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
8698 && CONSP (XSYMBOL (keytran_next)->function)
8699 && EQ (XCAR (XSYMBOL (keytran_next)->function), Qautoload))
8700 do_autoload (XSYMBOL (keytran_next)->function,
8701 keytran_next);
8702
8703 /* Handle a symbol whose function definition is a keymap
8704 or an array. */
8705 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
8706 && (!NILP (Farrayp (XSYMBOL (keytran_next)->function))
8707 || KEYMAPP (XSYMBOL (keytran_next)->function)))
8708 keytran_next = XSYMBOL (keytran_next)->function;
8709
8710 /* If the key translation map gives a function, not an
8711 array, then call the function with one arg and use
8712 its value instead. */
8713 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
8714 && keytran_end == t)
8715 {
8716 struct gcpro gcpro1, gcpro2, gcpro3;
8717 Lisp_Object tem;
8718 tem = keytran_next;
8719
8720 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
8721 keytran_next = call1 (keytran_next, prompt);
8722 UNGCPRO;
8723 /* If the function returned something invalid,
8724 barf--don't ignore it.
8725 (To ignore it safely, we would need to gcpro a bunch of
8726 other variables.) */
8727 if (! (VECTORP (keytran_next) || STRINGP (keytran_next)))
8728 error ("Function in key-translation-map returns invalid key sequence");
8729 }
8730
8731 key_translation_possible = ! NILP (keytran_next);
8732
8733 /* If keybuf[keytran_start..keytran_end] is bound in the
8734 key translation map and it's a suffix of the current
8735 sequence (i.e. keytran_end == t), replace it with
8736 the binding and restart with keytran_start at the end. */
8737 if ((VECTORP (keytran_next) || STRINGP (keytran_next))
8738 && keytran_end == t)
8739 {
8740 int len = XFASTINT (Flength (keytran_next));
8741
8742 t = keytran_start + len;
8743 if (t >= bufsize)
8744 error ("Key sequence too long");
8745
8746 if (VECTORP (keytran_next))
8747 bcopy (XVECTOR (keytran_next)->contents,
8748 keybuf + keytran_start,
8749 (t - keytran_start) * sizeof (keybuf[0]));
8750 else if (STRINGP (keytran_next))
8751 {
8752 int i;
8753
8754 for (i = 0; i < len; i++)
8755 XSETFASTINT (keybuf[keytran_start + i],
8756 XSTRING (keytran_next)->data[i]);
8757 }
8758
8759 mock_input = t;
8760 keytran_start = keytran_end = t;
8761 keytran_map = Vkey_translation_map;
8762
8763 /* Don't pass the results of key-translation-map
8764 through function-key-map. */
8765 fkey_start = fkey_end = t;
8766 fkey_map = Vfunction_key_map;
8767
8768 goto replay_sequence;
8769 }
8770
8771 keytran_map = get_keymap (keytran_next, 0, 1);
8772
8773 /* If we no longer have a bound suffix, try a new positions for
8774 keytran_start. */
8775 if (!CONSP (keytran_map))
8776 {
8777 keytran_end = ++keytran_start;
8778 keytran_map = Vkey_translation_map;
8779 key_translation_possible = 0;
8780 }
8781 }
8782 }
8783
8784 /* If KEY is not defined in any of the keymaps,
8785 and cannot be part of a function key or translation,
8786 and is an upper case letter
8787 use the corresponding lower-case letter instead. */
8788 if (first_binding == nmaps && ! function_key_possible
8789 && ! key_translation_possible
8790 && INTEGERP (key)
8791 && ((((XINT (key) & 0x3ffff)
8792 < XCHAR_TABLE (current_buffer->downcase_table)->size)
8793 && UPPERCASEP (XINT (key) & 0x3ffff))
8794 || (XINT (key) & shift_modifier)))
8795 {
8796 Lisp_Object new_key;
8797
8798 original_uppercase = key;
8799 original_uppercase_position = t - 1;
8800
8801 if (XINT (key) & shift_modifier)
8802 XSETINT (new_key, XINT (key) & ~shift_modifier);
8803 else
8804 XSETINT (new_key, (DOWNCASE (XINT (key) & 0x3ffff)
8805 | (XINT (key) & ~0x3ffff)));
8806
8807 /* We have to do this unconditionally, regardless of whether
8808 the lower-case char is defined in the keymaps, because they
8809 might get translated through function-key-map. */
8810 keybuf[t - 1] = new_key;
8811 mock_input = t;
8812
8813 fkey_map = prev_fkey_map;
8814 fkey_start = prev_fkey_start;
8815 fkey_end = prev_fkey_end;
8816
8817 keytran_map = prev_keytran_map;
8818 keytran_start = prev_keytran_start;
8819 keytran_end = prev_keytran_end;
8820
8821 goto replay_sequence;
8822 }
8823 /* If KEY is not defined in any of the keymaps,
8824 and cannot be part of a function key or translation,
8825 and is a shifted function key,
8826 use the corresponding unshifted function key instead. */
8827 if (first_binding == nmaps && ! function_key_possible
8828 && ! key_translation_possible
8829 && SYMBOLP (key))
8830 {
8831 Lisp_Object breakdown;
8832 int modifiers;
8833
8834 breakdown = parse_modifiers (key);
8835 modifiers = XINT (XCAR (XCDR (breakdown)));
8836 if (modifiers & shift_modifier)
8837 {
8838 Lisp_Object new_key;
8839
8840 original_uppercase = key;
8841 original_uppercase_position = t - 1;
8842
8843 modifiers &= ~shift_modifier;
8844 new_key = apply_modifiers (modifiers,
8845 XCAR (breakdown));
8846
8847 keybuf[t - 1] = new_key;
8848 mock_input = t;
8849
8850 fkey_map = prev_fkey_map;
8851 fkey_start = prev_fkey_start;
8852 fkey_end = prev_fkey_end;
8853
8854 keytran_map = prev_keytran_map;
8855 keytran_start = prev_keytran_start;
8856 keytran_end = prev_keytran_end;
8857
8858 goto replay_sequence;
8859 }
8860 }
8861 }
8862
8863 if (!dummyflag)
8864 read_key_sequence_cmd = (first_binding < nmaps
8865 ? defs[first_binding]
8866 : Qnil);
8867
8868 unread_switch_frame = delayed_switch_frame;
8869 unbind_to (count, Qnil);
8870
8871 /* Don't downcase the last character if the caller says don't.
8872 Don't downcase it if the result is undefined, either. */
8873 if ((dont_downcase_last || first_binding >= nmaps)
8874 && t - 1 == original_uppercase_position)
8875 keybuf[t - 1] = original_uppercase;
8876
8877 /* Occasionally we fabricate events, perhaps by expanding something
8878 according to function-key-map, or by adding a prefix symbol to a
8879 mouse click in the scroll bar or modeline. In this cases, return
8880 the entire generated key sequence, even if we hit an unbound
8881 prefix or a definition before the end. This means that you will
8882 be able to push back the event properly, and also means that
8883 read-key-sequence will always return a logical unit.
8884
8885 Better ideas? */
8886 for (; t < mock_input; t++)
8887 {
8888 if ((FLOATP (Vecho_keystrokes) || INTEGERP (Vecho_keystrokes))
8889 && NILP (Fzerop (Vecho_keystrokes)))
8890 echo_char (keybuf[t]);
8891 add_command_key (keybuf[t]);
8892 }
8893
8894
8895
8896 UNGCPRO;
8897 return t;
8898 }
8899
8900 #if 0 /* This doc string is too long for some compilers.
8901 This commented-out definition serves for DOC. */
8902 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
8903 "Read a sequence of keystrokes and return as a string or vector.\n\
8904 The sequence is sufficient to specify a non-prefix command in the\n\
8905 current local and global maps.\n\
8906 \n\
8907 First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
8908 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
8909 as a continuation of the previous key.\n\
8910 \n\
8911 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not\n\
8912 convert the last event to lower case. (Normally any upper case event\n\
8913 is converted to lower case if the original event is undefined and the lower\n\
8914 case equivalent is defined.) A non-nil value is appropriate for reading\n\
8915 a key sequence to be defined.\n\
8916 \n\
8917 A C-g typed while in this function is treated like any other character,\n\
8918 and `quit-flag' is not set.\n\
8919 \n\
8920 If the key sequence starts with a mouse click, then the sequence is read\n\
8921 using the keymaps of the buffer of the window clicked in, not the buffer\n\
8922 of the selected window as normal.\n\
8923 ""\n\
8924 `read-key-sequence' drops unbound button-down events, since you normally\n\
8925 only care about the click or drag events which follow them. If a drag\n\
8926 or multi-click event is unbound, but the corresponding click event would\n\
8927 be bound, `read-key-sequence' turns the event into a click event at the\n\
8928 drag's starting position. This means that you don't have to distinguish\n\
8929 between click and drag, double, or triple events unless you want to.\n\
8930 \n\
8931 `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
8932 lines separating windows, and scroll bars with imaginary keys\n\
8933 `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
8934 \n\
8935 Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this\n\
8936 function will process a switch-frame event if the user switches frames\n\
8937 before typing anything. If the user switches frames in the middle of a\n\
8938 key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME\n\
8939 is nil, then the event will be put off until after the current key sequence.\n\
8940 \n\
8941 `read-key-sequence' checks `function-key-map' for function key\n\
8942 sequences, where they wouldn't conflict with ordinary bindings. See\n\
8943 `function-key-map' for more details.\n\
8944 \n\
8945 The optional fifth argument COMMAND-LOOP, if non-nil, means\n\
8946 that this key sequence is being read by something that will\n\
8947 read commands one after another. It should be nil if the caller\n\
8948 will read just one key sequence.")
8949 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame, command-loop)
8950 #endif
8951
8952 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 5, 0,
8953 0)
8954 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
8955 command_loop)
8956 Lisp_Object prompt, continue_echo, dont_downcase_last;
8957 Lisp_Object can_return_switch_frame, command_loop;
8958 {
8959 Lisp_Object keybuf[30];
8960 register int i;
8961 struct gcpro gcpro1;
8962 int count = specpdl_ptr - specpdl;
8963
8964 if (!NILP (prompt))
8965 CHECK_STRING (prompt, 0);
8966 QUIT;
8967
8968 specbind (Qinput_method_exit_on_first_char,
8969 (NILP (command_loop) ? Qt : Qnil));
8970 specbind (Qinput_method_use_echo_area,
8971 (NILP (command_loop) ? Qt : Qnil));
8972
8973 bzero (keybuf, sizeof keybuf);
8974 GCPRO1 (keybuf[0]);
8975 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
8976
8977 if (NILP (continue_echo))
8978 {
8979 this_command_key_count = 0;
8980 this_single_command_key_start = 0;
8981 }
8982
8983 #ifdef HAVE_X_WINDOWS
8984 if (display_hourglass_p)
8985 cancel_hourglass ();
8986 #endif
8987
8988 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
8989 prompt, ! NILP (dont_downcase_last),
8990 ! NILP (can_return_switch_frame), 0);
8991
8992 #if 0 /* The following is fine for code reading a key sequence and
8993 then proceeding with a lenghty compuation, but it's not good
8994 for code reading keys in a loop, like an input method. */
8995 #ifdef HAVE_X_WINDOWS
8996 if (display_hourglass_p)
8997 start_hourglass ();
8998 #endif
8999 #endif
9000
9001 if (i == -1)
9002 {
9003 Vquit_flag = Qt;
9004 QUIT;
9005 }
9006 UNGCPRO;
9007 return unbind_to (count, make_event_array (i, keybuf));
9008 }
9009
9010 DEFUN ("read-key-sequence-vector", Fread_key_sequence_vector,
9011 Sread_key_sequence_vector, 1, 5, 0,
9012 "Like `read-key-sequence' but always return a vector.")
9013 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame,
9014 command_loop)
9015 Lisp_Object prompt, continue_echo, dont_downcase_last;
9016 Lisp_Object can_return_switch_frame, command_loop;
9017 {
9018 Lisp_Object keybuf[30];
9019 register int i;
9020 struct gcpro gcpro1;
9021 int count = specpdl_ptr - specpdl;
9022
9023 if (!NILP (prompt))
9024 CHECK_STRING (prompt, 0);
9025 QUIT;
9026
9027 specbind (Qinput_method_exit_on_first_char,
9028 (NILP (command_loop) ? Qt : Qnil));
9029 specbind (Qinput_method_use_echo_area,
9030 (NILP (command_loop) ? Qt : Qnil));
9031
9032 bzero (keybuf, sizeof keybuf);
9033 GCPRO1 (keybuf[0]);
9034 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
9035
9036 if (NILP (continue_echo))
9037 {
9038 this_command_key_count = 0;
9039 this_single_command_key_start = 0;
9040 }
9041
9042 #ifdef HAVE_X_WINDOWS
9043 if (display_hourglass_p)
9044 cancel_hourglass ();
9045 #endif
9046
9047 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
9048 prompt, ! NILP (dont_downcase_last),
9049 ! NILP (can_return_switch_frame), 0);
9050
9051 #ifdef HAVE_X_WINDOWS
9052 if (display_hourglass_p)
9053 start_hourglass ();
9054 #endif
9055
9056 if (i == -1)
9057 {
9058 Vquit_flag = Qt;
9059 QUIT;
9060 }
9061 UNGCPRO;
9062 return unbind_to (count, Fvector (i, keybuf));
9063 }
9064 \f
9065 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 4, 0,
9066 "Execute CMD as an editor command.\n\
9067 CMD must be a symbol that satisfies the `commandp' predicate.\n\
9068 Optional second arg RECORD-FLAG non-nil\n\
9069 means unconditionally put this command in `command-history'.\n\
9070 Otherwise, that is done only if an arg is read using the minibuffer.\n\
9071 The argument KEYS specifies the value to use instead of (this-command-keys)\n\
9072 when reading the arguments; if it is nil, (this-command-keys) is used.\n\
9073 The argument SPECIAL, if non-nil, means that this command is executing\n\
9074 a special event, so ignore the prefix argument and don't clear it.")
9075 (cmd, record_flag, keys, special)
9076 Lisp_Object cmd, record_flag, keys, special;
9077 {
9078 register Lisp_Object final;
9079 register Lisp_Object tem;
9080 Lisp_Object prefixarg;
9081 struct backtrace backtrace;
9082 extern int debug_on_next_call;
9083
9084 debug_on_next_call = 0;
9085
9086 if (NILP (special))
9087 {
9088 prefixarg = current_kboard->Vprefix_arg;
9089 Vcurrent_prefix_arg = prefixarg;
9090 current_kboard->Vprefix_arg = Qnil;
9091 }
9092 else
9093 prefixarg = Qnil;
9094
9095 if (SYMBOLP (cmd))
9096 {
9097 tem = Fget (cmd, Qdisabled);
9098 if (!NILP (tem) && !NILP (Vrun_hooks))
9099 {
9100 tem = Fsymbol_value (Qdisabled_command_hook);
9101 if (!NILP (tem))
9102 return call1 (Vrun_hooks, Qdisabled_command_hook);
9103 }
9104 }
9105
9106 while (1)
9107 {
9108 final = Findirect_function (cmd);
9109
9110 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
9111 {
9112 struct gcpro gcpro1, gcpro2;
9113
9114 GCPRO2 (cmd, prefixarg);
9115 do_autoload (final, cmd);
9116 UNGCPRO;
9117 }
9118 else
9119 break;
9120 }
9121
9122 if (STRINGP (final) || VECTORP (final))
9123 {
9124 /* If requested, place the macro in the command history. For
9125 other sorts of commands, call-interactively takes care of
9126 this. */
9127 if (!NILP (record_flag))
9128 {
9129 Vcommand_history
9130 = Fcons (Fcons (Qexecute_kbd_macro,
9131 Fcons (final, Fcons (prefixarg, Qnil))),
9132 Vcommand_history);
9133
9134 /* Don't keep command history around forever. */
9135 if (NUMBERP (Vhistory_length) && XINT (Vhistory_length) > 0)
9136 {
9137 tem = Fnthcdr (Vhistory_length, Vcommand_history);
9138 if (CONSP (tem))
9139 XCDR (tem) = Qnil;
9140 }
9141 }
9142
9143 return Fexecute_kbd_macro (final, prefixarg);
9144 }
9145
9146 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
9147 {
9148 backtrace.next = backtrace_list;
9149 backtrace_list = &backtrace;
9150 backtrace.function = &Qcall_interactively;
9151 backtrace.args = &cmd;
9152 backtrace.nargs = 1;
9153 backtrace.evalargs = 0;
9154
9155 tem = Fcall_interactively (cmd, record_flag, keys);
9156
9157 backtrace_list = backtrace.next;
9158 return tem;
9159 }
9160 return Qnil;
9161 }
9162
9163
9164 \f
9165 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
9166 1, 1, "P",
9167 "Read function name, then read its arguments and call it.")
9168 (prefixarg)
9169 Lisp_Object prefixarg;
9170 {
9171 Lisp_Object function;
9172 char buf[40];
9173 Lisp_Object saved_keys;
9174 Lisp_Object bindings, value;
9175 struct gcpro gcpro1, gcpro2;
9176
9177 saved_keys = Fvector (this_command_key_count,
9178 XVECTOR (this_command_keys)->contents);
9179 buf[0] = 0;
9180 GCPRO2 (saved_keys, prefixarg);
9181
9182 if (EQ (prefixarg, Qminus))
9183 strcpy (buf, "- ");
9184 else if (CONSP (prefixarg) && XINT (XCAR (prefixarg)) == 4)
9185 strcpy (buf, "C-u ");
9186 else if (CONSP (prefixarg) && INTEGERP (XCAR (prefixarg)))
9187 {
9188 if (sizeof (int) == sizeof (EMACS_INT))
9189 sprintf (buf, "%d ", XINT (XCAR (prefixarg)));
9190 else if (sizeof (long) == sizeof (EMACS_INT))
9191 sprintf (buf, "%ld ", (long) XINT (XCAR (prefixarg)));
9192 else
9193 abort ();
9194 }
9195 else if (INTEGERP (prefixarg))
9196 {
9197 if (sizeof (int) == sizeof (EMACS_INT))
9198 sprintf (buf, "%d ", XINT (prefixarg));
9199 else if (sizeof (long) == sizeof (EMACS_INT))
9200 sprintf (buf, "%ld ", (long) XINT (prefixarg));
9201 else
9202 abort ();
9203 }
9204
9205 /* This isn't strictly correct if execute-extended-command
9206 is bound to anything else. Perhaps it should use
9207 this_command_keys? */
9208 strcat (buf, "M-x ");
9209
9210 /* Prompt with buf, and then read a string, completing from and
9211 restricting to the set of all defined commands. Don't provide
9212 any initial input. Save the command read on the extended-command
9213 history list. */
9214 function = Fcompleting_read (build_string (buf),
9215 Vobarray, Qcommandp,
9216 Qt, Qnil, Qextended_command_history, Qnil,
9217 Qnil);
9218
9219 if (STRINGP (function) && XSTRING (function)->size == 0)
9220 error ("No command name given");
9221
9222 /* Set this_command_keys to the concatenation of saved_keys and
9223 function, followed by a RET. */
9224 {
9225 struct Lisp_String *str;
9226 Lisp_Object *keys;
9227 int i;
9228
9229 this_command_key_count = 0;
9230 this_single_command_key_start = 0;
9231
9232 keys = XVECTOR (saved_keys)->contents;
9233 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
9234 add_command_key (keys[i]);
9235
9236 str = XSTRING (function);
9237 for (i = 0; i < str->size; i++)
9238 add_command_key (Faref (function, make_number (i)));
9239
9240 add_command_key (make_number ('\015'));
9241 }
9242
9243 UNGCPRO;
9244
9245 function = Fintern (function, Qnil);
9246 current_kboard->Vprefix_arg = prefixarg;
9247 Vthis_command = function;
9248 real_this_command = function;
9249
9250 /* If enabled, show which key runs this command. */
9251 if (!NILP (Vsuggest_key_bindings)
9252 && NILP (Vexecuting_macro)
9253 && SYMBOLP (function))
9254 bindings = Fwhere_is_internal (function, Voverriding_local_map,
9255 Qt, Qnil);
9256 else
9257 bindings = Qnil;
9258
9259 value = Qnil;
9260 GCPRO2 (bindings, value);
9261 value = Fcommand_execute (function, Qt, Qnil, Qnil);
9262
9263 /* If the command has a key binding, print it now. */
9264 if (!NILP (bindings)
9265 && ! (VECTORP (bindings) && EQ (Faref (bindings, make_number (0)),
9266 Qmouse_movement)))
9267 {
9268 /* But first wait, and skip the message if there is input. */
9269 int delay_time;
9270 if (!NILP (echo_area_buffer[0]))
9271 /* This command displayed something in the echo area;
9272 so wait a few seconds, then display our suggestion message. */
9273 delay_time = (NUMBERP (Vsuggest_key_bindings)
9274 ? XINT (Vsuggest_key_bindings) : 2);
9275 else
9276 /* This command left the echo area empty,
9277 so display our message immediately. */
9278 delay_time = 0;
9279
9280 if (!NILP (Fsit_for (make_number (delay_time), Qnil, Qnil))
9281 && ! CONSP (Vunread_command_events))
9282 {
9283 Lisp_Object binding;
9284 char *newmessage;
9285 int message_p = push_message ();
9286 int count = BINDING_STACK_SIZE ();
9287
9288 record_unwind_protect (push_message_unwind, Qnil);
9289 binding = Fkey_description (bindings);
9290
9291 newmessage
9292 = (char *) alloca (XSYMBOL (function)->name->size
9293 + STRING_BYTES (XSTRING (binding))
9294 + 100);
9295 sprintf (newmessage, "You can run the command `%s' with %s",
9296 XSYMBOL (function)->name->data,
9297 XSTRING (binding)->data);
9298 message2_nolog (newmessage,
9299 strlen (newmessage),
9300 STRING_MULTIBYTE (binding));
9301 if (!NILP (Fsit_for ((NUMBERP (Vsuggest_key_bindings)
9302 ? Vsuggest_key_bindings : make_number (2)),
9303 Qnil, Qnil))
9304 && message_p)
9305 restore_message ();
9306
9307 unbind_to (count, Qnil);
9308 }
9309 }
9310
9311 RETURN_UNGCPRO (value);
9312 }
9313
9314 /* Find the set of keymaps now active.
9315 Store into *MAPS_P a vector holding the various maps
9316 and return the number of them. The vector was malloc'd
9317 and the caller should free it. */
9318
9319 int
9320 current_active_maps (maps_p)
9321 Lisp_Object **maps_p;
9322 {
9323 Lisp_Object *tmaps, *maps;
9324 int nmaps;
9325
9326 /* Should overriding-terminal-local-map and overriding-local-map apply? */
9327 if (!NILP (Voverriding_local_map_menu_flag))
9328 {
9329 /* Yes, use them (if non-nil) as well as the global map. */
9330 maps = (Lisp_Object *) xmalloc (3 * sizeof (maps[0]));
9331 nmaps = 0;
9332 if (!NILP (current_kboard->Voverriding_terminal_local_map))
9333 maps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
9334 if (!NILP (Voverriding_local_map))
9335 maps[nmaps++] = Voverriding_local_map;
9336 }
9337 else
9338 {
9339 /* No, so use major and minor mode keymaps and keymap property. */
9340 int extra_maps = 2;
9341 Lisp_Object map = get_local_map (PT, current_buffer, Qkeymap);
9342 if (!NILP (map))
9343 extra_maps = 3;
9344 nmaps = current_minor_maps (NULL, &tmaps);
9345 maps = (Lisp_Object *) alloca ((nmaps + extra_maps)
9346 * sizeof (maps[0]));
9347 bcopy (tmaps, maps, nmaps * sizeof (maps[0]));
9348 if (!NILP (map))
9349 maps[nmaps++] = map;
9350 maps[nmaps++] = get_local_map (PT, current_buffer, Qlocal_map);
9351 }
9352 maps[nmaps++] = current_global_map;
9353
9354 *maps_p = maps;
9355 return nmaps;
9356 }
9357 \f
9358 /* Return nonzero if input events are pending. */
9359
9360 int
9361 detect_input_pending ()
9362 {
9363 if (!input_pending)
9364 get_input_pending (&input_pending, 0);
9365
9366 return input_pending;
9367 }
9368
9369 /* Return nonzero if input events are pending, and run any pending timers. */
9370
9371 int
9372 detect_input_pending_run_timers (do_display)
9373 int do_display;
9374 {
9375 int old_timers_run = timers_run;
9376
9377 if (!input_pending)
9378 get_input_pending (&input_pending, 1);
9379
9380 if (old_timers_run != timers_run && do_display)
9381 {
9382 redisplay_preserve_echo_area (8);
9383 /* The following fixes a bug when using lazy-lock with
9384 lazy-lock-defer-on-the-fly set to t, i.e. when fontifying
9385 from an idle timer function. The symptom of the bug is that
9386 the cursor sometimes doesn't become visible until the next X
9387 event is processed. --gerd. */
9388 if (rif)
9389 rif->flush_display (NULL);
9390 }
9391
9392 return input_pending;
9393 }
9394
9395 /* This is called in some cases before a possible quit.
9396 It cases the next call to detect_input_pending to recompute input_pending.
9397 So calling this function unnecessarily can't do any harm. */
9398
9399 void
9400 clear_input_pending ()
9401 {
9402 input_pending = 0;
9403 }
9404
9405 /* Return nonzero if there are pending requeued events.
9406 This isn't used yet. The hope is to make wait_reading_process_input
9407 call it, and return return if it runs Lisp code that unreads something.
9408 The problem is, kbd_buffer_get_event needs to be fixed to know what
9409 to do in that case. It isn't trivial. */
9410
9411 int
9412 requeued_events_pending_p ()
9413 {
9414 return (!NILP (Vunread_command_events) || unread_command_char != -1);
9415 }
9416
9417
9418 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
9419 "T if command input is currently available with no waiting.\n\
9420 Actually, the value is nil only if we can be sure that no input is available.")
9421 ()
9422 {
9423 if (!NILP (Vunread_command_events) || unread_command_char != -1)
9424 return (Qt);
9425
9426 get_input_pending (&input_pending, 1);
9427 return input_pending > 0 ? Qt : Qnil;
9428 }
9429
9430 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
9431 "Return vector of last 100 events, not counting those from keyboard macros.")
9432 ()
9433 {
9434 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
9435 Lisp_Object val;
9436
9437 if (total_keys < NUM_RECENT_KEYS)
9438 return Fvector (total_keys, keys);
9439 else
9440 {
9441 val = Fvector (NUM_RECENT_KEYS, keys);
9442 bcopy (keys + recent_keys_index,
9443 XVECTOR (val)->contents,
9444 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
9445 bcopy (keys,
9446 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
9447 recent_keys_index * sizeof (Lisp_Object));
9448 return val;
9449 }
9450 }
9451
9452 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
9453 "Return the key sequence that invoked this command.\n\
9454 The value is a string or a vector.")
9455 ()
9456 {
9457 return make_event_array (this_command_key_count,
9458 XVECTOR (this_command_keys)->contents);
9459 }
9460
9461 DEFUN ("this-command-keys-vector", Fthis_command_keys_vector, Sthis_command_keys_vector, 0, 0, 0,
9462 "Return the key sequence that invoked this command, as a vector.")
9463 ()
9464 {
9465 return Fvector (this_command_key_count,
9466 XVECTOR (this_command_keys)->contents);
9467 }
9468
9469 DEFUN ("this-single-command-keys", Fthis_single_command_keys,
9470 Sthis_single_command_keys, 0, 0, 0,
9471 "Return the key sequence that invoked this command.\n\
9472 Unlike `this-command-keys', this function's value\n\
9473 does not include prefix arguments.\n\
9474 The value is always a vector.")
9475 ()
9476 {
9477 return Fvector (this_command_key_count
9478 - this_single_command_key_start,
9479 (XVECTOR (this_command_keys)->contents
9480 + this_single_command_key_start));
9481 }
9482
9483 DEFUN ("this-single-command-raw-keys", Fthis_single_command_raw_keys,
9484 Sthis_single_command_raw_keys, 0, 0, 0,
9485 "Return the raw events that were read for this command.\n\
9486 Unlike `this-single-command-keys', this function's value\n\
9487 shows the events before all translations (except for input methods).\n\
9488 The value is always a vector.")
9489 ()
9490 {
9491 return Fvector (raw_keybuf_count,
9492 (XVECTOR (raw_keybuf)->contents));
9493 }
9494
9495 DEFUN ("reset-this-command-lengths", Freset_this_command_lengths,
9496 Sreset_this_command_lengths, 0, 0, 0,
9497 "Used for complicated reasons in `universal-argument-other-key'.\n\
9498 \n\
9499 `universal-argument-other-key' rereads the event just typed.\n\
9500 It then gets translated through `function-key-map'.\n\
9501 The translated event gets included in the echo area and in\n\
9502 the value of `this-command-keys' in addition to the raw original event.\n\
9503 That is not right.\n\
9504 \n\
9505 Calling this function directs the translated event to replace\n\
9506 the original event, so that only one version of the event actually\n\
9507 appears in the echo area and in the value of `this-command-keys.'.")
9508 ()
9509 {
9510 before_command_restore_flag = 1;
9511 before_command_key_count_1 = before_command_key_count;
9512 before_command_echo_length_1 = before_command_echo_length;
9513 return Qnil;
9514 }
9515
9516 DEFUN ("clear-this-command-keys", Fclear_this_command_keys,
9517 Sclear_this_command_keys, 0, 0, 0,
9518 "Clear out the vector that `this-command-keys' returns.\n\
9519 Clear vector containing last 100 events.")
9520 ()
9521 {
9522 int i;
9523
9524 this_command_key_count = 0;
9525
9526 for (i = 0; i < XVECTOR (recent_keys)->size; ++i)
9527 XVECTOR (recent_keys)->contents[i] = Qnil;
9528 total_keys = 0;
9529 recent_keys_index = 0;
9530 return Qnil;
9531 }
9532
9533 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
9534 "Return the current depth in recursive edits.")
9535 ()
9536 {
9537 Lisp_Object temp;
9538 XSETFASTINT (temp, command_loop_level + minibuf_level);
9539 return temp;
9540 }
9541
9542 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
9543 "FOpen dribble file: ",
9544 "Start writing all keyboard characters to a dribble file called FILE.\n\
9545 If FILE is nil, close any open dribble file.")
9546 (file)
9547 Lisp_Object file;
9548 {
9549 if (dribble)
9550 {
9551 fclose (dribble);
9552 dribble = 0;
9553 }
9554 if (!NILP (file))
9555 {
9556 file = Fexpand_file_name (file, Qnil);
9557 dribble = fopen (XSTRING (file)->data, "w");
9558 if (dribble == 0)
9559 report_file_error ("Opening dribble", Fcons (file, Qnil));
9560 }
9561 return Qnil;
9562 }
9563
9564 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
9565 "Discard the contents of the terminal input buffer.\n\
9566 Also cancel any kbd macro being defined.")
9567 ()
9568 {
9569 current_kboard->defining_kbd_macro = Qnil;
9570 update_mode_lines++;
9571
9572 Vunread_command_events = Qnil;
9573 unread_command_char = -1;
9574
9575 discard_tty_input ();
9576
9577 kbd_fetch_ptr = kbd_store_ptr;
9578 Ffillarray (kbd_buffer_gcpro, Qnil);
9579 input_pending = 0;
9580
9581 return Qnil;
9582 }
9583 \f
9584 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
9585 "Stop Emacs and return to superior process. You can resume later.\n\
9586 If `cannot-suspend' is non-nil, or if the system doesn't support job\n\
9587 control, run a subshell instead.\n\n\
9588 If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
9589 to be read as terminal input by Emacs's parent, after suspension.\n\
9590 \n\
9591 Before suspending, run the normal hook `suspend-hook'.\n\
9592 After resumption run the normal hook `suspend-resume-hook'.\n\
9593 \n\
9594 Some operating systems cannot stop the Emacs process and resume it later.\n\
9595 On such systems, Emacs starts a subshell instead of suspending.")
9596 (stuffstring)
9597 Lisp_Object stuffstring;
9598 {
9599 int count = specpdl_ptr - specpdl;
9600 int old_height, old_width;
9601 int width, height;
9602 struct gcpro gcpro1;
9603
9604 if (!NILP (stuffstring))
9605 CHECK_STRING (stuffstring, 0);
9606
9607 /* Run the functions in suspend-hook. */
9608 if (!NILP (Vrun_hooks))
9609 call1 (Vrun_hooks, intern ("suspend-hook"));
9610
9611 GCPRO1 (stuffstring);
9612 get_frame_size (&old_width, &old_height);
9613 reset_sys_modes ();
9614 /* sys_suspend can get an error if it tries to fork a subshell
9615 and the system resources aren't available for that. */
9616 record_unwind_protect ((Lisp_Object (*) P_ ((Lisp_Object))) init_sys_modes,
9617 Qnil);
9618 stuff_buffered_input (stuffstring);
9619 if (cannot_suspend)
9620 sys_subshell ();
9621 else
9622 sys_suspend ();
9623 unbind_to (count, Qnil);
9624
9625 /* Check if terminal/window size has changed.
9626 Note that this is not useful when we are running directly
9627 with a window system; but suspend should be disabled in that case. */
9628 get_frame_size (&width, &height);
9629 if (width != old_width || height != old_height)
9630 change_frame_size (SELECTED_FRAME (), height, width, 0, 0, 0);
9631
9632 /* Run suspend-resume-hook. */
9633 if (!NILP (Vrun_hooks))
9634 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
9635
9636 UNGCPRO;
9637 return Qnil;
9638 }
9639
9640 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
9641 Then in any case stuff anything Emacs has read ahead and not used. */
9642
9643 void
9644 stuff_buffered_input (stuffstring)
9645 Lisp_Object stuffstring;
9646 {
9647 /* stuff_char works only in BSD, versions 4.2 and up. */
9648 #ifdef BSD_SYSTEM
9649 #ifndef BSD4_1
9650 register unsigned char *p;
9651
9652 if (STRINGP (stuffstring))
9653 {
9654 register int count;
9655
9656 p = XSTRING (stuffstring)->data;
9657 count = STRING_BYTES (XSTRING (stuffstring));
9658 while (count-- > 0)
9659 stuff_char (*p++);
9660 stuff_char ('\n');
9661 }
9662
9663 /* Anything we have read ahead, put back for the shell to read. */
9664 /* ?? What should this do when we have multiple keyboards??
9665 Should we ignore anything that was typed in at the "wrong" kboard? */
9666 for (; kbd_fetch_ptr != kbd_store_ptr; kbd_fetch_ptr++)
9667 {
9668 int idx;
9669
9670 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
9671 kbd_fetch_ptr = kbd_buffer;
9672 if (kbd_fetch_ptr->kind == ascii_keystroke)
9673 stuff_char (kbd_fetch_ptr->code);
9674
9675 kbd_fetch_ptr->kind = no_event;
9676 idx = 2 * (kbd_fetch_ptr - kbd_buffer);
9677 ASET (kbd_buffer_gcpro, idx, Qnil);
9678 ASET (kbd_buffer_gcpro, idx + 1, Qnil);
9679 }
9680
9681 input_pending = 0;
9682 #endif
9683 #endif /* BSD_SYSTEM and not BSD4_1 */
9684 }
9685 \f
9686 void
9687 set_waiting_for_input (time_to_clear)
9688 EMACS_TIME *time_to_clear;
9689 {
9690 input_available_clear_time = time_to_clear;
9691
9692 /* Tell interrupt_signal to throw back to read_char, */
9693 waiting_for_input = 1;
9694
9695 /* If interrupt_signal was called before and buffered a C-g,
9696 make it run again now, to avoid timing error. */
9697 if (!NILP (Vquit_flag))
9698 quit_throw_to_read_char ();
9699 }
9700
9701 void
9702 clear_waiting_for_input ()
9703 {
9704 /* Tell interrupt_signal not to throw back to read_char, */
9705 waiting_for_input = 0;
9706 input_available_clear_time = 0;
9707 }
9708
9709 /* This routine is called at interrupt level in response to C-G.
9710
9711 If interrupt_input, this is the handler for SIGINT. Otherwise, it
9712 is called from kbd_buffer_store_event, in handling SIGIO or
9713 SIGTINT.
9714
9715 If `waiting_for_input' is non zero, then unless `echoing' is
9716 nonzero, immediately throw back to read_char.
9717
9718 Otherwise it sets the Lisp variable quit-flag not-nil. This causes
9719 eval to throw, when it gets a chance. If quit-flag is already
9720 non-nil, it stops the job right away. */
9721
9722 SIGTYPE
9723 interrupt_signal (signalnum) /* If we don't have an argument, */
9724 int signalnum; /* some compilers complain in signal calls. */
9725 {
9726 char c;
9727 /* Must preserve main program's value of errno. */
9728 int old_errno = errno;
9729 struct frame *sf = SELECTED_FRAME ();
9730
9731 #if defined (USG) && !defined (POSIX_SIGNALS)
9732 if (!read_socket_hook && NILP (Vwindow_system))
9733 {
9734 /* USG systems forget handlers when they are used;
9735 must reestablish each time */
9736 signal (SIGINT, interrupt_signal);
9737 signal (SIGQUIT, interrupt_signal);
9738 }
9739 #endif /* USG */
9740
9741 cancel_echoing ();
9742
9743 if (!NILP (Vquit_flag)
9744 && (FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf)))
9745 {
9746 /* If SIGINT isn't blocked, don't let us be interrupted by
9747 another SIGINT, it might be harmful due to non-reentrancy
9748 in I/O functions. */
9749 sigblock (sigmask (SIGINT));
9750
9751 fflush (stdout);
9752 reset_sys_modes ();
9753
9754 #ifdef SIGTSTP /* Support possible in later USG versions */
9755 /*
9756 * On systems which can suspend the current process and return to the original
9757 * shell, this command causes the user to end up back at the shell.
9758 * The "Auto-save" and "Abort" questions are not asked until
9759 * the user elects to return to emacs, at which point he can save the current
9760 * job and either dump core or continue.
9761 */
9762 sys_suspend ();
9763 #else
9764 #ifdef VMS
9765 if (sys_suspend () == -1)
9766 {
9767 printf ("Not running as a subprocess;\n");
9768 printf ("you can continue or abort.\n");
9769 }
9770 #else /* not VMS */
9771 /* Perhaps should really fork an inferior shell?
9772 But that would not provide any way to get back
9773 to the original shell, ever. */
9774 printf ("No support for stopping a process on this operating system;\n");
9775 printf ("you can continue or abort.\n");
9776 #endif /* not VMS */
9777 #endif /* not SIGTSTP */
9778 #ifdef MSDOS
9779 /* We must remain inside the screen area when the internal terminal
9780 is used. Note that [Enter] is not echoed by dos. */
9781 cursor_to (0, 0);
9782 #endif
9783 /* It doesn't work to autosave while GC is in progress;
9784 the code used for auto-saving doesn't cope with the mark bit. */
9785 if (!gc_in_progress)
9786 {
9787 printf ("Auto-save? (y or n) ");
9788 fflush (stdout);
9789 if (((c = getchar ()) & ~040) == 'Y')
9790 {
9791 Fdo_auto_save (Qt, Qnil);
9792 #ifdef MSDOS
9793 printf ("\r\nAuto-save done");
9794 #else /* not MSDOS */
9795 printf ("Auto-save done\n");
9796 #endif /* not MSDOS */
9797 }
9798 while (c != '\n') c = getchar ();
9799 }
9800 else
9801 {
9802 /* During GC, it must be safe to reenable quitting again. */
9803 Vinhibit_quit = Qnil;
9804 #ifdef MSDOS
9805 printf ("\r\n");
9806 #endif /* not MSDOS */
9807 printf ("Garbage collection in progress; cannot auto-save now\r\n");
9808 printf ("but will instead do a real quit after garbage collection ends\r\n");
9809 fflush (stdout);
9810 }
9811
9812 #ifdef MSDOS
9813 printf ("\r\nAbort? (y or n) ");
9814 #else /* not MSDOS */
9815 #ifdef VMS
9816 printf ("Abort (and enter debugger)? (y or n) ");
9817 #else /* not VMS */
9818 printf ("Abort (and dump core)? (y or n) ");
9819 #endif /* not VMS */
9820 #endif /* not MSDOS */
9821 fflush (stdout);
9822 if (((c = getchar ()) & ~040) == 'Y')
9823 abort ();
9824 while (c != '\n') c = getchar ();
9825 #ifdef MSDOS
9826 printf ("\r\nContinuing...\r\n");
9827 #else /* not MSDOS */
9828 printf ("Continuing...\n");
9829 #endif /* not MSDOS */
9830 fflush (stdout);
9831 init_sys_modes ();
9832 sigfree ();
9833 }
9834 else
9835 {
9836 /* If executing a function that wants to be interrupted out of
9837 and the user has not deferred quitting by binding `inhibit-quit'
9838 then quit right away. */
9839 if (immediate_quit && NILP (Vinhibit_quit))
9840 {
9841 struct gl_state_s saved;
9842 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9843
9844 immediate_quit = 0;
9845 sigfree ();
9846 saved = gl_state;
9847 GCPRO4 (saved.object, saved.global_code,
9848 saved.current_syntax_table, saved.old_prop);
9849 Fsignal (Qquit, Qnil);
9850 gl_state = saved;
9851 UNGCPRO;
9852 }
9853 else
9854 /* Else request quit when it's safe */
9855 Vquit_flag = Qt;
9856 }
9857
9858 if (waiting_for_input && !echoing)
9859 quit_throw_to_read_char ();
9860
9861 errno = old_errno;
9862 }
9863
9864 /* Handle a C-g by making read_char return C-g. */
9865
9866 void
9867 quit_throw_to_read_char ()
9868 {
9869 sigfree ();
9870 /* Prevent another signal from doing this before we finish. */
9871 clear_waiting_for_input ();
9872 input_pending = 0;
9873
9874 Vunread_command_events = Qnil;
9875 unread_command_char = -1;
9876
9877 #if 0 /* Currently, sit_for is called from read_char without turning
9878 off polling. And that can call set_waiting_for_input.
9879 It seems to be harmless. */
9880 #ifdef POLL_FOR_INPUT
9881 /* May be > 1 if in recursive minibuffer. */
9882 if (poll_suppress_count == 0)
9883 abort ();
9884 #endif
9885 #endif
9886 if (FRAMEP (internal_last_event_frame)
9887 && !EQ (internal_last_event_frame, selected_frame))
9888 do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
9889 0, 0);
9890
9891 _longjmp (getcjmp, 1);
9892 }
9893 \f
9894 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
9895 "Set mode of reading keyboard input.\n\
9896 First arg INTERRUPT non-nil means use input interrupts;\n\
9897 nil means use CBREAK mode.\n\
9898 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
9899 (no effect except in CBREAK mode).\n\
9900 Third arg META t means accept 8-bit input (for a Meta key).\n\
9901 META nil means ignore the top bit, on the assumption it is parity.\n\
9902 Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
9903 Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\
9904 See also `current-input-mode'.")
9905 (interrupt, flow, meta, quit)
9906 Lisp_Object interrupt, flow, meta, quit;
9907 {
9908 if (!NILP (quit)
9909 && (!INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400))
9910 error ("set-input-mode: QUIT must be an ASCII character");
9911
9912 #ifdef POLL_FOR_INPUT
9913 stop_polling ();
9914 #endif
9915
9916 #ifndef DOS_NT
9917 /* this causes startup screen to be restored and messes with the mouse */
9918 reset_sys_modes ();
9919 #endif
9920
9921 #ifdef SIGIO
9922 /* Note SIGIO has been undef'd if FIONREAD is missing. */
9923 if (read_socket_hook)
9924 {
9925 /* When using X, don't give the user a real choice,
9926 because we haven't implemented the mechanisms to support it. */
9927 #ifdef NO_SOCK_SIGIO
9928 interrupt_input = 0;
9929 #else /* not NO_SOCK_SIGIO */
9930 interrupt_input = 1;
9931 #endif /* NO_SOCK_SIGIO */
9932 }
9933 else
9934 interrupt_input = !NILP (interrupt);
9935 #else /* not SIGIO */
9936 interrupt_input = 0;
9937 #endif /* not SIGIO */
9938
9939 /* Our VMS input only works by interrupts, as of now. */
9940 #ifdef VMS
9941 interrupt_input = 1;
9942 #endif
9943
9944 flow_control = !NILP (flow);
9945 if (NILP (meta))
9946 meta_key = 0;
9947 else if (EQ (meta, Qt))
9948 meta_key = 1;
9949 else
9950 meta_key = 2;
9951 if (!NILP (quit))
9952 /* Don't let this value be out of range. */
9953 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
9954
9955 #ifndef DOS_NT
9956 init_sys_modes ();
9957 #endif
9958
9959 #ifdef POLL_FOR_INPUT
9960 poll_suppress_count = 1;
9961 start_polling ();
9962 #endif
9963 return Qnil;
9964 }
9965
9966 DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
9967 "Return information about the way Emacs currently reads keyboard input.\n\
9968 The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
9969 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
9970 nil, Emacs is using CBREAK mode.\n\
9971 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
9972 terminal; this does not apply if Emacs uses interrupt-driven input.\n\
9973 META is t if accepting 8-bit input with 8th bit as Meta flag.\n\
9974 META nil means ignoring the top bit, on the assumption it is parity.\n\
9975 META is neither t nor nil if accepting 8-bit input and using\n\
9976 all 8 bits as the character code.\n\
9977 QUIT is the character Emacs currently uses to quit.\n\
9978 The elements of this list correspond to the arguments of\n\
9979 `set-input-mode'.")
9980 ()
9981 {
9982 Lisp_Object val[4];
9983
9984 val[0] = interrupt_input ? Qt : Qnil;
9985 val[1] = flow_control ? Qt : Qnil;
9986 val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
9987 XSETFASTINT (val[3], quit_char);
9988
9989 return Flist (sizeof (val) / sizeof (val[0]), val);
9990 }
9991
9992 \f
9993 /*
9994 * Set up a new kboard object with reasonable initial values.
9995 */
9996 void
9997 init_kboard (kb)
9998 KBOARD *kb;
9999 {
10000 kb->Voverriding_terminal_local_map = Qnil;
10001 kb->Vlast_command = Qnil;
10002 kb->Vreal_last_command = Qnil;
10003 kb->Vprefix_arg = Qnil;
10004 kb->Vlast_prefix_arg = Qnil;
10005 kb->kbd_queue = Qnil;
10006 kb->kbd_queue_has_data = 0;
10007 kb->immediate_echo = 0;
10008 kb->echoptr = kb->echobuf;
10009 kb->echo_after_prompt = -1;
10010 kb->kbd_macro_buffer = 0;
10011 kb->kbd_macro_bufsize = 0;
10012 kb->defining_kbd_macro = Qnil;
10013 kb->Vlast_kbd_macro = Qnil;
10014 kb->reference_count = 0;
10015 kb->Vsystem_key_alist = Qnil;
10016 kb->system_key_syms = Qnil;
10017 kb->Vdefault_minibuffer_frame = Qnil;
10018 }
10019
10020 /*
10021 * Destroy the contents of a kboard object, but not the object itself.
10022 * We use this just before deleting it, or if we're going to initialize
10023 * it a second time.
10024 */
10025 static void
10026 wipe_kboard (kb)
10027 KBOARD *kb;
10028 {
10029 if (kb->kbd_macro_buffer)
10030 xfree (kb->kbd_macro_buffer);
10031 }
10032
10033 #ifdef MULTI_KBOARD
10034
10035 /* Free KB and memory referenced from it. */
10036
10037 void
10038 delete_kboard (kb)
10039 KBOARD *kb;
10040 {
10041 KBOARD **kbp;
10042
10043 for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
10044 if (*kbp == NULL)
10045 abort ();
10046 *kbp = kb->next_kboard;
10047
10048 /* Prevent a dangling reference to KB. */
10049 if (kb == current_kboard
10050 && FRAMEP (selected_frame)
10051 && FRAME_LIVE_P (XFRAME (selected_frame)))
10052 {
10053 current_kboard = XFRAME (selected_frame)->kboard;
10054 if (current_kboard == kb)
10055 abort ();
10056 }
10057
10058 wipe_kboard (kb);
10059 xfree (kb);
10060 }
10061
10062 #endif /* MULTI_KBOARD */
10063
10064 void
10065 init_keyboard ()
10066 {
10067 /* This is correct before outermost invocation of the editor loop */
10068 command_loop_level = -1;
10069 immediate_quit = 0;
10070 quit_char = Ctl ('g');
10071 Vunread_command_events = Qnil;
10072 unread_command_char = -1;
10073 EMACS_SET_SECS_USECS (timer_idleness_start_time, -1, -1);
10074 total_keys = 0;
10075 recent_keys_index = 0;
10076 kbd_fetch_ptr = kbd_buffer;
10077 kbd_store_ptr = kbd_buffer;
10078 kbd_buffer_gcpro = Fmake_vector (make_number (2 * KBD_BUFFER_SIZE), Qnil);
10079 #ifdef HAVE_MOUSE
10080 do_mouse_tracking = Qnil;
10081 #endif
10082 input_pending = 0;
10083
10084 /* This means that command_loop_1 won't try to select anything the first
10085 time through. */
10086 internal_last_event_frame = Qnil;
10087 Vlast_event_frame = internal_last_event_frame;
10088
10089 #ifdef MULTI_KBOARD
10090 current_kboard = initial_kboard;
10091 #endif
10092 wipe_kboard (current_kboard);
10093 init_kboard (current_kboard);
10094
10095 if (!noninteractive && !read_socket_hook && NILP (Vwindow_system))
10096 {
10097 signal (SIGINT, interrupt_signal);
10098 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
10099 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
10100 SIGQUIT and we can't tell which one it will give us. */
10101 signal (SIGQUIT, interrupt_signal);
10102 #endif /* HAVE_TERMIO */
10103 }
10104 /* Note SIGIO has been undef'd if FIONREAD is missing. */
10105 #ifdef SIGIO
10106 if (!noninteractive)
10107 signal (SIGIO, input_available_signal);
10108 #endif /* SIGIO */
10109
10110 /* Use interrupt input by default, if it works and noninterrupt input
10111 has deficiencies. */
10112
10113 #ifdef INTERRUPT_INPUT
10114 interrupt_input = 1;
10115 #else
10116 interrupt_input = 0;
10117 #endif
10118
10119 /* Our VMS input only works by interrupts, as of now. */
10120 #ifdef VMS
10121 interrupt_input = 1;
10122 #endif
10123
10124 sigfree ();
10125 dribble = 0;
10126
10127 if (keyboard_init_hook)
10128 (*keyboard_init_hook) ();
10129
10130 #ifdef POLL_FOR_INPUT
10131 poll_suppress_count = 1;
10132 start_polling ();
10133 #endif
10134 }
10135
10136 /* This type's only use is in syms_of_keyboard, to initialize the
10137 event header symbols and put properties on them. */
10138 struct event_head {
10139 Lisp_Object *var;
10140 char *name;
10141 Lisp_Object *kind;
10142 };
10143
10144 struct event_head head_table[] = {
10145 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
10146 &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement,
10147 &Qswitch_frame, "switch-frame", &Qswitch_frame,
10148 &Qdelete_frame, "delete-frame", &Qdelete_frame,
10149 &Qiconify_frame, "iconify-frame", &Qiconify_frame,
10150 &Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible,
10151 };
10152
10153 void
10154 syms_of_keyboard ()
10155 {
10156 Vlispy_mouse_stem = build_string ("mouse");
10157 staticpro (&Vlispy_mouse_stem);
10158
10159 /* Tool-bars. */
10160 QCimage = intern (":image");
10161 staticpro (&QCimage);
10162
10163 staticpro (&Qhelp_echo);
10164 Qhelp_echo = intern ("help-echo");
10165
10166 staticpro (&item_properties);
10167 item_properties = Qnil;
10168
10169 staticpro (&tool_bar_item_properties);
10170 tool_bar_item_properties = Qnil;
10171 staticpro (&tool_bar_items_vector);
10172 tool_bar_items_vector = Qnil;
10173
10174 staticpro (&real_this_command);
10175 real_this_command = Qnil;
10176
10177 Qtimer_event_handler = intern ("timer-event-handler");
10178 staticpro (&Qtimer_event_handler);
10179
10180 Qdisabled_command_hook = intern ("disabled-command-hook");
10181 staticpro (&Qdisabled_command_hook);
10182
10183 Qself_insert_command = intern ("self-insert-command");
10184 staticpro (&Qself_insert_command);
10185
10186 Qforward_char = intern ("forward-char");
10187 staticpro (&Qforward_char);
10188
10189 Qbackward_char = intern ("backward-char");
10190 staticpro (&Qbackward_char);
10191
10192 Qdisabled = intern ("disabled");
10193 staticpro (&Qdisabled);
10194
10195 Qundefined = intern ("undefined");
10196 staticpro (&Qundefined);
10197
10198 Qpre_command_hook = intern ("pre-command-hook");
10199 staticpro (&Qpre_command_hook);
10200
10201 Qpost_command_hook = intern ("post-command-hook");
10202 staticpro (&Qpost_command_hook);
10203
10204 Qpost_command_idle_hook = intern ("post-command-idle-hook");
10205 staticpro (&Qpost_command_idle_hook);
10206
10207 Qdeferred_action_function = intern ("deferred-action-function");
10208 staticpro (&Qdeferred_action_function);
10209
10210 Qcommand_hook_internal = intern ("command-hook-internal");
10211 staticpro (&Qcommand_hook_internal);
10212
10213 Qfunction_key = intern ("function-key");
10214 staticpro (&Qfunction_key);
10215 Qmouse_click = intern ("mouse-click");
10216 staticpro (&Qmouse_click);
10217 #ifdef WINDOWSNT
10218 Qmouse_wheel = intern ("mouse-wheel");
10219 staticpro (&Qmouse_wheel);
10220 Qlanguage_change = intern ("language-change");
10221 staticpro (&Qlanguage_change);
10222 #endif
10223 Qdrag_n_drop = intern ("drag-n-drop");
10224 staticpro (&Qdrag_n_drop);
10225
10226 Qusr1_signal = intern ("usr1-signal");
10227 staticpro (&Qusr1_signal);
10228 Qusr2_signal = intern ("usr2-signal");
10229 staticpro (&Qusr2_signal);
10230
10231 Qmenu_enable = intern ("menu-enable");
10232 staticpro (&Qmenu_enable);
10233 Qmenu_alias = intern ("menu-alias");
10234 staticpro (&Qmenu_alias);
10235 QCenable = intern (":enable");
10236 staticpro (&QCenable);
10237 QCvisible = intern (":visible");
10238 staticpro (&QCvisible);
10239 QChelp = intern (":help");
10240 staticpro (&QChelp);
10241 QCfilter = intern (":filter");
10242 staticpro (&QCfilter);
10243 QCbutton = intern (":button");
10244 staticpro (&QCbutton);
10245 QCkeys = intern (":keys");
10246 staticpro (&QCkeys);
10247 QCkey_sequence = intern (":key-sequence");
10248 staticpro (&QCkey_sequence);
10249 QCtoggle = intern (":toggle");
10250 staticpro (&QCtoggle);
10251 QCradio = intern (":radio");
10252 staticpro (&QCradio);
10253
10254 Qmode_line = intern ("mode-line");
10255 staticpro (&Qmode_line);
10256 Qvertical_line = intern ("vertical-line");
10257 staticpro (&Qvertical_line);
10258 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
10259 staticpro (&Qvertical_scroll_bar);
10260 Qmenu_bar = intern ("menu-bar");
10261 staticpro (&Qmenu_bar);
10262
10263 Qabove_handle = intern ("above-handle");
10264 staticpro (&Qabove_handle);
10265 Qhandle = intern ("handle");
10266 staticpro (&Qhandle);
10267 Qbelow_handle = intern ("below-handle");
10268 staticpro (&Qbelow_handle);
10269 Qup = intern ("up");
10270 staticpro (&Qup);
10271 Qdown = intern ("down");
10272 staticpro (&Qdown);
10273 Qtop = intern ("top");
10274 staticpro (&Qtop);
10275 Qbottom = intern ("bottom");
10276 staticpro (&Qbottom);
10277 Qend_scroll = intern ("end-scroll");
10278 staticpro (&Qend_scroll);
10279 Qratio = intern ("ratio");
10280 staticpro (&Qratio);
10281
10282 Qevent_kind = intern ("event-kind");
10283 staticpro (&Qevent_kind);
10284 Qevent_symbol_elements = intern ("event-symbol-elements");
10285 staticpro (&Qevent_symbol_elements);
10286 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
10287 staticpro (&Qevent_symbol_element_mask);
10288 Qmodifier_cache = intern ("modifier-cache");
10289 staticpro (&Qmodifier_cache);
10290
10291 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
10292 staticpro (&Qrecompute_lucid_menubar);
10293 Qactivate_menubar_hook = intern ("activate-menubar-hook");
10294 staticpro (&Qactivate_menubar_hook);
10295
10296 Qpolling_period = intern ("polling-period");
10297 staticpro (&Qpolling_period);
10298
10299 Qinput_method_function = intern ("input-method-function");
10300 staticpro (&Qinput_method_function);
10301
10302 Qinput_method_exit_on_first_char = intern ("input-method-exit-on-first-char");
10303 staticpro (&Qinput_method_exit_on_first_char);
10304 Qinput_method_use_echo_area = intern ("input-method-use-echo-area");
10305 staticpro (&Qinput_method_use_echo_area);
10306
10307 Fset (Qinput_method_exit_on_first_char, Qnil);
10308 Fset (Qinput_method_use_echo_area, Qnil);
10309
10310 last_point_position_buffer = Qnil;
10311
10312 {
10313 struct event_head *p;
10314
10315 for (p = head_table;
10316 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
10317 p++)
10318 {
10319 *p->var = intern (p->name);
10320 staticpro (p->var);
10321 Fput (*p->var, Qevent_kind, *p->kind);
10322 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
10323 }
10324 }
10325
10326 button_down_location = Fmake_vector (make_number (1), Qnil);
10327 staticpro (&button_down_location);
10328 mouse_syms = Fmake_vector (make_number (1), Qnil);
10329 staticpro (&mouse_syms);
10330
10331 {
10332 int i;
10333 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
10334
10335 modifier_symbols = Fmake_vector (make_number (len), Qnil);
10336 for (i = 0; i < len; i++)
10337 if (modifier_names[i])
10338 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
10339 staticpro (&modifier_symbols);
10340 }
10341
10342 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
10343 staticpro (&recent_keys);
10344
10345 this_command_keys = Fmake_vector (make_number (40), Qnil);
10346 staticpro (&this_command_keys);
10347
10348 raw_keybuf = Fmake_vector (make_number (30), Qnil);
10349 staticpro (&raw_keybuf);
10350
10351 Qextended_command_history = intern ("extended-command-history");
10352 Fset (Qextended_command_history, Qnil);
10353 staticpro (&Qextended_command_history);
10354
10355 kbd_buffer_gcpro = Fmake_vector (make_number (2 * KBD_BUFFER_SIZE), Qnil);
10356 staticpro (&kbd_buffer_gcpro);
10357
10358 accent_key_syms = Qnil;
10359 staticpro (&accent_key_syms);
10360
10361 func_key_syms = Qnil;
10362 staticpro (&func_key_syms);
10363
10364 #ifdef WINDOWSNT
10365 mouse_wheel_syms = Qnil;
10366 staticpro (&mouse_wheel_syms);
10367
10368 drag_n_drop_syms = Qnil;
10369 staticpro (&drag_n_drop_syms);
10370 #endif
10371
10372 unread_switch_frame = Qnil;
10373 staticpro (&unread_switch_frame);
10374
10375 internal_last_event_frame = Qnil;
10376 staticpro (&internal_last_event_frame);
10377
10378 read_key_sequence_cmd = Qnil;
10379 staticpro (&read_key_sequence_cmd);
10380
10381 menu_bar_one_keymap_changed_items = Qnil;
10382 staticpro (&menu_bar_one_keymap_changed_items);
10383
10384 defsubr (&Sevent_convert_list);
10385 defsubr (&Sread_key_sequence);
10386 defsubr (&Sread_key_sequence_vector);
10387 defsubr (&Srecursive_edit);
10388 #ifdef HAVE_MOUSE
10389 defsubr (&Strack_mouse);
10390 #endif
10391 defsubr (&Sinput_pending_p);
10392 defsubr (&Scommand_execute);
10393 defsubr (&Srecent_keys);
10394 defsubr (&Sthis_command_keys);
10395 defsubr (&Sthis_command_keys_vector);
10396 defsubr (&Sthis_single_command_keys);
10397 defsubr (&Sthis_single_command_raw_keys);
10398 defsubr (&Sreset_this_command_lengths);
10399 defsubr (&Sclear_this_command_keys);
10400 defsubr (&Ssuspend_emacs);
10401 defsubr (&Sabort_recursive_edit);
10402 defsubr (&Sexit_recursive_edit);
10403 defsubr (&Srecursion_depth);
10404 defsubr (&Stop_level);
10405 defsubr (&Sdiscard_input);
10406 defsubr (&Sopen_dribble_file);
10407 defsubr (&Sset_input_mode);
10408 defsubr (&Scurrent_input_mode);
10409 defsubr (&Sexecute_extended_command);
10410
10411 DEFVAR_LISP ("last-command-char", &last_command_char,
10412 "Last input event that was part of a command.");
10413
10414 DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char,
10415 "Last input event that was part of a command.");
10416
10417 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
10418 "Last input event in a command, except for mouse menu events.\n\
10419 Mouse menus give back keys that don't look like mouse events;\n\
10420 this variable holds the actual mouse event that led to the menu,\n\
10421 so that you can determine whether the command was run by mouse or not.");
10422
10423 DEFVAR_LISP ("last-input-char", &last_input_char,
10424 "Last input event.");
10425
10426 DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char,
10427 "Last input event.");
10428
10429 DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
10430 "List of events to be read as the command input.\n\
10431 These events are processed first, before actual keyboard input.");
10432 Vunread_command_events = Qnil;
10433
10434 DEFVAR_INT ("unread-command-char", &unread_command_char,
10435 "If not -1, an object to be read as next command input event.");
10436
10437 DEFVAR_LISP ("unread-post-input-method-events", &Vunread_post_input_method_events,
10438 "List of events to be processed as input by input methods.\n\
10439 These events are processed after `unread-command-events', but\n\
10440 before actual keyboard input.");
10441 Vunread_post_input_method_events = Qnil;
10442
10443 DEFVAR_LISP ("unread-input-method-events", &Vunread_input_method_events,
10444 "List of events to be processed as input by input methods.\n\
10445 These events are processed after `unread-command-events', but\n\
10446 before actual keyboard input.");
10447 Vunread_input_method_events = Qnil;
10448
10449 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
10450 "Meta-prefix character code.\n\
10451 Meta-foo as command input turns into this character followed by foo.");
10452 XSETINT (meta_prefix_char, 033);
10453
10454 DEFVAR_KBOARD ("last-command", Vlast_command,
10455 "The last command executed.\n\
10456 Normally a symbol with a function definition, but can be whatever was found\n\
10457 in the keymap, or whatever the variable `this-command' was set to by that\n\
10458 command.\n\
10459 \n\
10460 The value `mode-exit' is special; it means that the previous command\n\
10461 read an event that told it to exit, and it did so and unread that event.\n\
10462 In other words, the present command is the event that made the previous\n\
10463 command exit.\n\
10464 \n\
10465 The value `kill-region' is special; it means that the previous command\n\
10466 was a kill command.");
10467
10468 DEFVAR_KBOARD ("real-last-command", Vreal_last_command,
10469 "Same as `last-command', but never altered by Lisp code.");
10470
10471 DEFVAR_LISP ("this-command", &Vthis_command,
10472 "The command now being executed.\n\
10473 The command can set this variable; whatever is put here\n\
10474 will be in `last-command' during the following command.");
10475 Vthis_command = Qnil;
10476
10477 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
10478 "*Number of input events between auto-saves.\n\
10479 Zero means disable autosaving due to number of characters typed.");
10480 auto_save_interval = 300;
10481
10482 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
10483 "*Number of seconds idle time before auto-save.\n\
10484 Zero or nil means disable auto-saving due to idleness.\n\
10485 After auto-saving due to this many seconds of idle time,\n\
10486 Emacs also does a garbage collection if that seems to be warranted.");
10487 XSETFASTINT (Vauto_save_timeout, 30);
10488
10489 DEFVAR_LISP ("echo-keystrokes", &Vecho_keystrokes,
10490 "*Nonzero means echo unfinished commands after this many seconds of pause.\n\
10491 The value may be integer or floating point.");
10492 Vecho_keystrokes = make_number (1);
10493
10494 DEFVAR_INT ("polling-period", &polling_period,
10495 "*Interval between polling for input during Lisp execution.\n\
10496 The reason for polling is to make C-g work to stop a running program.\n\
10497 Polling is needed only when using X windows and SIGIO does not work.\n\
10498 Polling is automatically disabled in all other cases.");
10499 polling_period = 2;
10500
10501 DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
10502 "*Maximum time between mouse clicks to make a double-click.\n\
10503 Measured in milliseconds. nil means disable double-click recognition;\n\
10504 t means double-clicks have no time limit and are detected\n\
10505 by position only.");
10506 Vdouble_click_time = make_number (500);
10507
10508 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus,
10509 "*Non-nil means inhibit local map menu bar menus.");
10510 inhibit_local_menu_bar_menus = 0;
10511
10512 DEFVAR_INT ("num-input-keys", &num_input_keys,
10513 "Number of complete key sequences read as input so far.\n\
10514 This includes key sequences read from keyboard macros.\n\
10515 The number is effectively the number of interactive command invocations.");
10516 num_input_keys = 0;
10517
10518 DEFVAR_INT ("num-nonmacro-input-events", &num_nonmacro_input_events,
10519 "Number of input events read from the keyboard so far.\n\
10520 This does not include events generated by keyboard macros.");
10521 num_nonmacro_input_events = 0;
10522
10523 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
10524 "The frame in which the most recently read event occurred.\n\
10525 If the last event came from a keyboard macro, this is set to `macro'.");
10526 Vlast_event_frame = Qnil;
10527
10528 /* This variable is set up in sysdep.c. */
10529 DEFVAR_LISP ("tty-erase-char", &Vtty_erase_char,
10530 "The ERASE character as set by the user with stty.");
10531
10532 DEFVAR_LISP ("help-char", &Vhelp_char,
10533 "Character to recognize as meaning Help.\n\
10534 When it is read, do `(eval help-form)', and display result if it's a string.\n\
10535 If the value of `help-form' is nil, this char can be read normally.");
10536 XSETINT (Vhelp_char, Ctl ('H'));
10537
10538 DEFVAR_LISP ("help-event-list", &Vhelp_event_list,
10539 "List of input events to recognize as meaning Help.\n\
10540 These work just like the value of `help-char' (see that).");
10541 Vhelp_event_list = Qnil;
10542
10543 DEFVAR_LISP ("help-form", &Vhelp_form,
10544 "Form to execute when character `help-char' is read.\n\
10545 If the form returns a string, that string is displayed.\n\
10546 If `help-form' is nil, the help char is not recognized.");
10547 Vhelp_form = Qnil;
10548
10549 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
10550 "Command to run when `help-char' character follows a prefix key.\n\
10551 This command is used only when there is no actual binding\n\
10552 for that character after that prefix key.");
10553 Vprefix_help_command = Qnil;
10554
10555 DEFVAR_LISP ("top-level", &Vtop_level,
10556 "Form to evaluate when Emacs starts up.\n\
10557 Useful to set before you dump a modified Emacs.");
10558 Vtop_level = Qnil;
10559
10560 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
10561 "Translate table for keyboard input, or nil.\n\
10562 Each character is looked up in this string and the contents used instead.\n\
10563 The value may be a string, a vector, or a char-table.\n\
10564 If it is a string or vector of length N,\n\
10565 character codes N and up are untranslated.\n\
10566 In a vector or a char-table, an element which is nil means \"no translation\".");
10567 Vkeyboard_translate_table = Qnil;
10568
10569 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
10570 "Non-nil means to always spawn a subshell instead of suspending.\n\
10571 \(Even if the operating system has support for stopping a process.\)");
10572 cannot_suspend = 0;
10573
10574 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
10575 "Non-nil means prompt with menus when appropriate.\n\
10576 This is done when reading from a keymap that has a prompt string,\n\
10577 for elements that have prompt strings.\n\
10578 The menu is displayed on the screen\n\
10579 if X menus were enabled at configuration\n\
10580 time and the previous event was a mouse click prefix key.\n\
10581 Otherwise, menu prompting uses the echo area.");
10582 menu_prompting = 1;
10583
10584 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
10585 "Character to see next line of menu prompt.\n\
10586 Type this character while in a menu prompt to rotate around the lines of it.");
10587 XSETINT (menu_prompt_more_char, ' ');
10588
10589 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
10590 "A mask of additional modifier keys to use with every keyboard character.\n\
10591 Emacs applies the modifiers of the character stored here to each keyboard\n\
10592 character it reads. For example, after evaluating the expression\n\
10593 (setq extra-keyboard-modifiers ?\\C-x)\n\
10594 all input characters will have the control modifier applied to them.\n\
10595 \n\
10596 Note that the character ?\\C-@, equivalent to the integer zero, does\n\
10597 not count as a control character; rather, it counts as a character\n\
10598 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\
10599 cancels any modification.");
10600 extra_keyboard_modifiers = 0;
10601
10602 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
10603 "If an editing command sets this to t, deactivate the mark afterward.\n\
10604 The command loop sets this to nil before each command,\n\
10605 and tests the value when the command returns.\n\
10606 Buffer modification stores t in this variable.");
10607 Vdeactivate_mark = Qnil;
10608
10609 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
10610 "Temporary storage of pre-command-hook or post-command-hook.");
10611 Vcommand_hook_internal = Qnil;
10612
10613 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
10614 "Normal hook run before each command is executed.\n\
10615 If an unhandled error happens in running this hook,\n\
10616 the hook value is set to nil, since otherwise the error\n\
10617 might happen repeatedly and make Emacs nonfunctional.");
10618 Vpre_command_hook = Qnil;
10619
10620 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
10621 "Normal hook run after each command is executed.\n\
10622 If an unhandled error happens in running this hook,\n\
10623 the hook value is set to nil, since otherwise the error\n\
10624 might happen repeatedly and make Emacs nonfunctional.");
10625 Vpost_command_hook = Qnil;
10626
10627 DEFVAR_LISP ("post-command-idle-hook", &Vpost_command_idle_hook,
10628 "Normal hook run after each command is executed, if idle.\n\
10629 Errors running the hook are caught and ignored.\n\
10630 This feature is obsolete; use idle timers instead. See `etc/NEWS'.");
10631 Vpost_command_idle_hook = Qnil;
10632
10633 DEFVAR_INT ("post-command-idle-delay", &post_command_idle_delay,
10634 "Delay time before running `post-command-idle-hook'.\n\
10635 This is measured in microseconds.");
10636 post_command_idle_delay = 100000;
10637
10638 #if 0
10639 DEFVAR_LISP ("echo-area-clear-hook", ...,
10640 "Normal hook run when clearing the echo area.");
10641 #endif
10642 Qecho_area_clear_hook = intern ("echo-area-clear-hook");
10643 XSYMBOL (Qecho_area_clear_hook)->value = Qnil;
10644
10645 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
10646 "t means menu bar, specified Lucid style, needs to be recomputed.");
10647 Vlucid_menu_bar_dirty_flag = Qnil;
10648
10649 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
10650 "List of menu bar items to move to the end of the menu bar.\n\
10651 The elements of the list are event types that may have menu bar bindings.");
10652 Vmenu_bar_final_items = Qnil;
10653
10654 DEFVAR_KBOARD ("overriding-terminal-local-map",
10655 Voverriding_terminal_local_map,
10656 "Per-terminal keymap that overrides all other local keymaps.\n\
10657 If this variable is non-nil, it is used as a keymap instead of the\n\
10658 buffer's local map, and the minor mode keymaps and text property keymaps.\n\
10659 This variable is intended to let commands such as `universal-argument'\n\
10660 set up a different keymap for reading the next command.");
10661
10662 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
10663 "Keymap that overrides all other local keymaps.\n\
10664 If this variable is non-nil, it is used as a keymap instead of the\n\
10665 buffer's local map, and the minor mode keymaps and text property keymaps.");
10666 Voverriding_local_map = Qnil;
10667
10668 DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag,
10669 "Non-nil means `overriding-local-map' applies to the menu bar.\n\
10670 Otherwise, the menu bar continues to reflect the buffer's local map\n\
10671 and the minor mode maps regardless of `overriding-local-map'.");
10672 Voverriding_local_map_menu_flag = Qnil;
10673
10674 DEFVAR_LISP ("special-event-map", &Vspecial_event_map,
10675 "Keymap defining bindings for special events to execute at low level.");
10676 Vspecial_event_map = Fcons (intern ("keymap"), Qnil);
10677
10678 DEFVAR_LISP ("track-mouse", &do_mouse_tracking,
10679 "*Non-nil means generate motion events for mouse motion.");
10680
10681 DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist,
10682 "Alist of system-specific X windows key symbols.\n\
10683 Each element should have the form (N . SYMBOL) where N is the\n\
10684 numeric keysym code (sans the \"system-specific\" bit 1<<28)\n\
10685 and SYMBOL is its name.");
10686
10687 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
10688 "List of deferred actions to be performed at a later time.\n\
10689 The precise format isn't relevant here; we just check whether it is nil.");
10690 Vdeferred_action_list = Qnil;
10691
10692 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
10693 "Function to call to handle deferred actions, after each command.\n\
10694 This function is called with no arguments after each command\n\
10695 whenever `deferred-action-list' is non-nil.");
10696 Vdeferred_action_function = Qnil;
10697
10698 DEFVAR_LISP ("suggest-key-bindings", &Vsuggest_key_bindings,
10699 "*Non-nil means show the equivalent key-binding when M-x command has one.\n\
10700 The value can be a length of time to show the message for.\n\
10701 If the value is non-nil and not a number, we wait 2 seconds.");
10702 Vsuggest_key_bindings = Qt;
10703
10704 DEFVAR_LISP ("timer-list", &Vtimer_list,
10705 "List of active absolute time timers in order of increasing time");
10706 Vtimer_list = Qnil;
10707
10708 DEFVAR_LISP ("timer-idle-list", &Vtimer_idle_list,
10709 "List of active idle-time timers in order of increasing time");
10710 Vtimer_idle_list = Qnil;
10711
10712 DEFVAR_LISP ("input-method-function", &Vinput_method_function,
10713 "If non-nil, the function that implements the current input method.\n\
10714 It's called with one argument, a printing character that was just read.\n\
10715 \(That means a character with code 040...0176.)\n\
10716 Typically this function uses `read-event' to read additional events.\n\
10717 When it does so, it should first bind `input-method-function' to nil\n\
10718 so it will not be called recursively.\n\
10719 \n\
10720 The function should return a list of zero or more events\n\
10721 to be used as input. If it wants to put back some events\n\
10722 to be reconsidered, separately, by the input method,\n\
10723 it can add them to the beginning of `unread-command-events'.\n\
10724 \n\
10725 The input method function can find in `input-method-previous-method'\n\
10726 the previous echo area message.\n\
10727 \n\
10728 The input method function should refer to the variables\n\
10729 `input-method-use-echo-area' and `input-method-exit-on-first-char'\n\
10730 for guidance on what to do.");
10731 Vinput_method_function = Qnil;
10732
10733 DEFVAR_LISP ("input-method-previous-message",
10734 &Vinput_method_previous_message,
10735 "When `input-method-function' is called, hold the previous echo area message.\n\
10736 This variable exists because `read-event' clears the echo area\n\
10737 before running the input method. It is nil if there was no message.");
10738 Vinput_method_previous_message = Qnil;
10739
10740 DEFVAR_LISP ("show-help-function", &Vshow_help_function,
10741 "If non-nil, the function that implements the display of help.\n\
10742 It's called with one argument, the help string to display.");
10743 Vshow_help_function = Qnil;
10744
10745 DEFVAR_LISP ("disable-point-adjustment", &Vdisable_point_adjustment,
10746 "If non-nil, suppress point adjustment after executing a command.\n\
10747 \n\
10748 After a command is executed, if point is moved into a region that has\n\
10749 special properties (e.g. composition, display), we adjust point to\n\
10750 the boundary of the region. But, several special commands sets this\n\
10751 variable to non-nil, then we suppress the point adjustment.\n\
10752 \n\
10753 This variable is set to nil before reading a command, and is checked\n\
10754 just after executing the command");
10755 Vdisable_point_adjustment = Qnil;
10756
10757 DEFVAR_LISP ("global-disable-point-adjustment",
10758 &Vglobal_disable_point_adjustment,
10759 "*If non-nil, always suppress point adjustment.\n\
10760 \n\
10761 The default value is nil, in which case, point adjustment are\n\
10762 suppressed only after special commands that set\n\
10763 `disable-point-adjustment' (which see) to non-nil.");
10764 Vglobal_disable_point_adjustment = Qnil;
10765
10766 DEFVAR_BOOL ("update-menu-bindings", &update_menu_bindings,
10767 "Non-nil means updating menu bindings is allowed.\n\
10768 A value of nil means menu bindings should not be updated.\n\
10769 Used during Emacs' startup.");
10770 update_menu_bindings = 1;
10771
10772 DEFVAR_LISP ("minibuffer-message-timeout", &Vminibuffer_message_timeout,
10773 "*How long to display an echo-area message when the minibuffer is active.\n\
10774 If the value is not a number, such messages don't time out.");
10775 Vminibuffer_message_timeout = make_number (2);
10776 }
10777
10778 void
10779 keys_of_keyboard ()
10780 {
10781 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
10782 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
10783 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
10784 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
10785 initial_define_key (meta_map, 'x', "execute-extended-command");
10786
10787 initial_define_lispy_key (Vspecial_event_map, "delete-frame",
10788 "handle-delete-frame");
10789 initial_define_lispy_key (Vspecial_event_map, "iconify-frame",
10790 "ignore-event");
10791 initial_define_lispy_key (Vspecial_event_map, "make-frame-visible",
10792 "ignore-event");
10793 }