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