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