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