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