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