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