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