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