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