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