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