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