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