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