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