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