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