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