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