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