]> code.delx.au - gnu-emacs/blob - src/keyboard.c
(cmd_error, command_loop_1, Fcommand_execute)
[gnu-emacs] / src / keyboard.c
1 /* Keyboard and mouse input; editor command loop.
2 Copyright (C) 1985,86,87,88,89,93,94,95 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /* Allow config.h to undefine symbols found here. */
21 #include <signal.h>
22
23 #include <config.h>
24 #include <stdio.h>
25 #include "termchar.h"
26 #include "termopts.h"
27 #include "lisp.h"
28 #include "termhooks.h"
29 #include "macros.h"
30 #include "frame.h"
31 #include "window.h"
32 #include "commands.h"
33 #include "buffer.h"
34 #include "disptab.h"
35 #include "dispextern.h"
36 #include "keyboard.h"
37 #include "intervals.h"
38 #include "blockinput.h"
39 #include <setjmp.h>
40 #include <errno.h>
41
42 #ifdef MSDOS
43 #include "msdos.h"
44 #include <time.h>
45 #else /* not MSDOS */
46 #ifndef VMS
47 #include <sys/ioctl.h>
48 #endif
49 #endif /* not MSDOS */
50
51 #include "syssignal.h"
52 #include "systty.h"
53
54 /* This is to get the definitions of the XK_ symbols. */
55 #ifdef HAVE_X_WINDOWS
56 #include "xterm.h"
57 #endif
58
59 /* Include systime.h after xterm.h to avoid double inclusion of time.h. */
60 #include "systime.h"
61
62 extern int errno;
63
64 /* Variables for blockinput.h: */
65
66 /* Non-zero if interrupt input is blocked right now. */
67 int interrupt_input_blocked;
68
69 /* Nonzero means an input interrupt has arrived
70 during the current critical section. */
71 int interrupt_input_pending;
72
73
74 /* File descriptor to use for input. */
75 extern int input_fd;
76
77 #ifdef HAVE_X_WINDOWS
78 /* Make all keyboard buffers much bigger when using X windows. */
79 #define KBD_BUFFER_SIZE 4096
80 #else /* No X-windows, character input */
81 #define KBD_BUFFER_SIZE 256
82 #endif /* No X-windows */
83
84 /* Following definition copied from eval.c */
85
86 struct backtrace
87 {
88 struct backtrace *next;
89 Lisp_Object *function;
90 Lisp_Object *args; /* Points to vector of args. */
91 int nargs; /* length of vector. If nargs is UNEVALLED,
92 args points to slot holding list of
93 unevalled args */
94 char evalargs;
95 };
96
97 #ifdef MULTI_KBOARD
98 KBOARD *initial_kboard;
99 KBOARD *current_kboard;
100 KBOARD *all_kboards;
101 int single_kboard;
102 #else
103 KBOARD the_only_kboard;
104 #endif
105
106 /* Non-nil disable property on a command means
107 do not execute it; call disabled-command-hook's value instead. */
108 Lisp_Object Qdisabled, Qdisabled_command_hook;
109
110 #define NUM_RECENT_KEYS (100)
111 int recent_keys_index; /* Index for storing next element into recent_keys */
112 int total_keys; /* Total number of elements stored into recent_keys */
113 Lisp_Object recent_keys; /* A vector, holding the last 100 keystrokes */
114
115 /* Vector holding the key sequence that invoked the current command.
116 It is reused for each command, and it may be longer than the current
117 sequence; this_command_key_count indicates how many elements
118 actually mean something.
119 It's easier to staticpro a single Lisp_Object than an array. */
120 Lisp_Object this_command_keys;
121 int this_command_key_count;
122
123 extern int minbuf_level;
124
125 extern struct backtrace *backtrace_list;
126
127 /* Nonzero means do menu prompting. */
128 static int menu_prompting;
129
130 /* Character to see next line of menu prompt. */
131 static Lisp_Object menu_prompt_more_char;
132
133 /* For longjmp to where kbd input is being done. */
134 static jmp_buf getcjmp;
135
136 /* True while doing kbd input. */
137 int waiting_for_input;
138
139 /* True while displaying for echoing. Delays C-g throwing. */
140 static int echoing;
141
142 /* Nonzero means disregard local maps for the menu bar. */
143 static int inhibit_local_menu_bar_menus;
144
145 /* Nonzero means C-g should cause immediate error-signal. */
146 int immediate_quit;
147
148 /* Character to recognize as the help char. */
149 Lisp_Object Vhelp_char;
150
151 /* Form to execute when help char is typed. */
152 Lisp_Object Vhelp_form;
153
154 /* Command to run when the help character follows a prefix key. */
155 Lisp_Object Vprefix_help_command;
156
157 /* List of items that should move to the end of the menu bar. */
158 Lisp_Object Vmenu_bar_final_items;
159
160 /* Character that causes a quit. Normally C-g.
161
162 If we are running on an ordinary terminal, this must be an ordinary
163 ASCII char, since we want to make it our interrupt character.
164
165 If we are not running on an ordinary terminal, it still needs to be
166 an ordinary ASCII char. This character needs to be recognized in
167 the input interrupt handler. At this point, the keystroke is
168 represented as a struct input_event, while the desired quit
169 character is specified as a lispy event. The mapping from struct
170 input_events to lispy events cannot run in an interrupt handler,
171 and the reverse mapping is difficult for anything but ASCII
172 keystrokes.
173
174 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
175 ASCII character. */
176 int quit_char;
177
178 extern Lisp_Object current_global_map;
179 extern int minibuf_level;
180
181 /* If non-nil, this is a map that overrides all other local maps. */
182 Lisp_Object Voverriding_local_map;
183
184 /* If non-nil, Voverriding_local_map applies to the menu bar. */
185 Lisp_Object Voverriding_local_map_menu_flag;
186
187 /* Current depth in recursive edits. */
188 int command_loop_level;
189
190 /* Total number of times command_loop has read a key sequence. */
191 int num_input_keys;
192
193 /* Last input character read as a command. */
194 Lisp_Object last_command_char;
195
196 /* Last input character read as a command, not counting menus
197 reached by the mouse. */
198 Lisp_Object last_nonmenu_event;
199
200 /* Last input character read for any purpose. */
201 Lisp_Object last_input_char;
202
203 /* If not Qnil, a list of objects to be read as subsequent command input. */
204 Lisp_Object Vunread_command_events;
205
206 /* If not -1, an event to be read as subsequent command input. */
207 int unread_command_char;
208
209 /* If not Qnil, this is a switch-frame event which we decided to put
210 off until the end of a key sequence. This should be read as the
211 next command input, after any unread_command_events.
212
213 read_key_sequence uses this to delay switch-frame events until the
214 end of the key sequence; Fread_char uses it to put off switch-frame
215 events until a non-ASCII event is acceptable as input. */
216 Lisp_Object unread_switch_frame;
217
218 /* A mask of extra modifier bits to put into every keyboard char. */
219 int extra_keyboard_modifiers;
220
221 /* Char to use as prefix when a meta character is typed in.
222 This is bound on entry to minibuffer in case ESC is changed there. */
223
224 Lisp_Object meta_prefix_char;
225
226 /* Last size recorded for a current buffer which is not a minibuffer. */
227 static int last_non_minibuf_size;
228
229 /* Number of idle seconds before an auto-save and garbage collection. */
230 static Lisp_Object Vauto_save_timeout;
231
232 /* Total number of times read_char has returned. */
233 int num_input_chars;
234
235 /* Total number of times read_char has returned, outside of macros. */
236 int num_nonmacro_input_chars;
237
238 /* Auto-save automatically when this many characters have been typed
239 since the last time. */
240
241 static int auto_save_interval;
242
243 /* Value of num_nonmacro_input_chars as of last auto save. */
244
245 int last_auto_save;
246
247 /* Last command executed by the editor command loop, not counting
248 commands that set the prefix argument. */
249
250 Lisp_Object last_command;
251
252 /* The command being executed by the command loop.
253 Commands may set this, and the value set will be copied into last_command
254 instead of the actual command. */
255 Lisp_Object this_command;
256
257 /* The value of point when the last command was executed. */
258 int last_point_position;
259
260 /* The buffer that was current when the last command was started. */
261 Lisp_Object last_point_position_buffer;
262
263 #ifdef MULTI_FRAME
264 /* The frame in which the last input event occurred, or Qmacro if the
265 last event came from a macro. We use this to determine when to
266 generate switch-frame events. This may be cleared by functions
267 like Fselect_frame, to make sure that a switch-frame event is
268 generated by the next character. */
269 Lisp_Object internal_last_event_frame;
270 #endif
271
272 /* A user-visible version of the above, intended to allow users to
273 figure out where the last event came from, if the event doesn't
274 carry that information itself (i.e. if it was a character). */
275 Lisp_Object Vlast_event_frame;
276
277 /* The timestamp of the last input event we received from the X server.
278 X Windows wants this for selection ownership. */
279 unsigned long last_event_timestamp;
280
281 Lisp_Object Qself_insert_command;
282 Lisp_Object Qforward_char;
283 Lisp_Object Qbackward_char;
284 Lisp_Object Qundefined;
285
286 /* read_key_sequence stores here the command definition of the
287 key sequence that it reads. */
288 Lisp_Object read_key_sequence_cmd;
289
290 /* Form to evaluate (if non-nil) when Emacs is started. */
291 Lisp_Object Vtop_level;
292
293 /* User-supplied string to translate input characters through. */
294 Lisp_Object Vkeyboard_translate_table;
295
296 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
297 extern Lisp_Object Vfunction_key_map;
298
299 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
300 Lisp_Object Vkey_translation_map;
301
302 /* Non-nil means deactivate the mark at end of this command. */
303 Lisp_Object Vdeactivate_mark;
304
305 /* Menu bar specified in Lucid Emacs fashion. */
306
307 Lisp_Object Vlucid_menu_bar_dirty_flag;
308 Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
309
310 /* Hooks to run before and after each command. */
311 Lisp_Object Qpre_command_hook, Qpost_command_hook;
312 Lisp_Object Vpre_command_hook, Vpost_command_hook;
313 Lisp_Object Qcommand_hook_internal, Vcommand_hook_internal;
314
315 /* List of deferred actions to be performed at a later time.
316 The precise format isn't relevant here; we just check whether it is nil. */
317 Lisp_Object Vdeferred_action_list;
318
319 /* Function to call to handle deferred actions, when there are any. */
320 Lisp_Object Vdeferred_action_function;
321 Lisp_Object Qdeferred_action_function;
322
323 /* File in which we write all commands we read. */
324 FILE *dribble;
325
326 /* Nonzero if input is available. */
327 int input_pending;
328
329 /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
330 keep 0200 bit in input chars. 0 to ignore the 0200 bit. */
331
332 int meta_key;
333
334 extern char *pending_malloc_warning;
335
336 /* Circular buffer for pre-read keyboard input. */
337 static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
338
339 /* Vector to GCPRO the frames and windows mentioned in kbd_buffer.
340
341 The interrupt-level event handlers will never enqueue an event on a
342 frame which is not in Vframe_list, and once an event is dequeued,
343 internal_last_event_frame or the event itself points to the frame.
344 So that's all fine.
345
346 But while the event is sitting in the queue, it's completely
347 unprotected. Suppose the user types one command which will run for
348 a while and then delete a frame, and then types another event at
349 the frame that will be deleted, before the command gets around to
350 it. Suppose there are no references to this frame elsewhere in
351 Emacs, and a GC occurs before the second event is dequeued. Now we
352 have an event referring to a freed frame, which will crash Emacs
353 when it is dequeued.
354
355 Similar things happen when an event on a scroll bar is enqueued; the
356 window may be deleted while the event is in the queue.
357
358 So, we use this vector to protect the frame_or_window field in the
359 event queue. That way, they'll be dequeued as dead frames or
360 windows, but still valid lisp objects.
361
362 If kbd_buffer[i].kind != no_event, then
363 (XVECTOR (kbd_buffer_frame_or_window)->contents[i]
364 == kbd_buffer[i].frame_or_window. */
365 static Lisp_Object kbd_buffer_frame_or_window;
366
367 /* Pointer to next available character in kbd_buffer.
368 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
369 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
370 next available char is in kbd_buffer[0]. */
371 static struct input_event *kbd_fetch_ptr;
372
373 /* Pointer to next place to store character in kbd_buffer. This
374 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
375 character should go in kbd_buffer[0]. */
376 static volatile struct input_event *kbd_store_ptr;
377
378 /* The above pair of variables forms a "queue empty" flag. When we
379 enqueue a non-hook event, we increment kbd_store_ptr. When we
380 dequeue a non-hook event, we increment kbd_fetch_ptr. We say that
381 there is input available iff the two pointers are not equal.
382
383 Why not just have a flag set and cleared by the enqueuing and
384 dequeuing functions? Such a flag could be screwed up by interrupts
385 at inopportune times. */
386
387 /* If this flag is a frame, we check mouse_moved to see when the
388 mouse moves, and motion events will appear in the input stream.
389 Otherwise, mouse motion is ignored. */
390 static Lisp_Object do_mouse_tracking;
391
392 #ifdef HAVE_MOUSE
393 /* The window system handling code should set this if the mouse has
394 moved since the last call to the mouse_position_hook. Calling that
395 hook should clear this. Code assumes that if this is set, it can
396 call mouse_position_hook to get the promised position, so don't set
397 it unless you're prepared to substantiate the claim! */
398 int mouse_moved;
399 #endif /* HAVE_MOUSE */
400
401 /* Symbols to head events. */
402 Lisp_Object Qmouse_movement;
403 Lisp_Object Qscroll_bar_movement;
404 Lisp_Object Qswitch_frame;
405 Lisp_Object Qdelete_frame;
406 Lisp_Object Qiconify_frame;
407 Lisp_Object Qmake_frame_visible;
408
409 /* Symbols to denote kinds of events. */
410 Lisp_Object Qfunction_key;
411 Lisp_Object Qmouse_click;
412 /* Lisp_Object Qmouse_movement; - also an event header */
413
414 /* Properties of event headers. */
415 Lisp_Object Qevent_kind;
416 Lisp_Object Qevent_symbol_elements;
417
418 Lisp_Object Qmenu_enable;
419
420 /* An event header symbol HEAD may have a property named
421 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
422 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
423 mask of modifiers applied to it. If present, this is used to help
424 speed up parse_modifiers. */
425 Lisp_Object Qevent_symbol_element_mask;
426
427 /* An unmodified event header BASE may have a property named
428 Qmodifier_cache, which is an alist mapping modifier masks onto
429 modified versions of BASE. If present, this helps speed up
430 apply_modifiers. */
431 Lisp_Object Qmodifier_cache;
432
433 /* Symbols to use for parts of windows. */
434 Lisp_Object Qmode_line;
435 Lisp_Object Qvertical_line;
436 Lisp_Object Qvertical_scroll_bar;
437 Lisp_Object Qmenu_bar;
438
439 extern Lisp_Object Qmenu_enable;
440
441 Lisp_Object recursive_edit_unwind (), command_loop ();
442 Lisp_Object Fthis_command_keys ();
443 Lisp_Object Qextended_command_history;
444
445 Lisp_Object Qpolling_period;
446
447 extern Lisp_Object Vprint_level, Vprint_length;
448
449 /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
450 happens. */
451 EMACS_TIME *input_available_clear_time;
452
453 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
454 Default is 1 if INTERRUPT_INPUT is defined. */
455 int interrupt_input;
456
457 /* Nonzero while interrupts are temporarily deferred during redisplay. */
458 int interrupts_deferred;
459
460 /* nonzero means use ^S/^Q for flow control. */
461 int flow_control;
462
463 /* Allow m- file to inhibit use of FIONREAD. */
464 #ifdef BROKEN_FIONREAD
465 #undef FIONREAD
466 #endif
467
468 /* We are unable to use interrupts if FIONREAD is not available,
469 so flush SIGIO so we won't try. */
470 #ifndef FIONREAD
471 #ifdef SIGIO
472 #undef SIGIO
473 #endif
474 #endif
475
476 /* If we support X Windows, turn on the code to poll periodically
477 to detect C-g. It isn't actually used when doing interrupt input. */
478 #ifdef HAVE_X_WINDOWS
479 #define POLL_FOR_INPUT
480 #endif
481 \f
482 /* Global variable declarations. */
483
484 /* Function for init_keyboard to call with no args (if nonzero). */
485 void (*keyboard_init_hook) ();
486
487 static int read_avail_input ();
488 static void get_input_pending ();
489 static int readable_events ();
490 static Lisp_Object read_char_x_menu_prompt ();
491 static Lisp_Object read_char_minibuf_menu_prompt ();
492 static Lisp_Object make_lispy_event ();
493 #ifdef HAVE_MOUSE
494 static Lisp_Object make_lispy_movement ();
495 #endif
496 static Lisp_Object modify_event_symbol ();
497 static Lisp_Object make_lispy_switch_frame ();
498 static int parse_solitary_modifier ();
499
500 /* > 0 if we are to echo keystrokes. */
501 static int echo_keystrokes;
502
503 /* Nonzero means don't try to suspend even if the operating system seems
504 to support it. */
505 static int cannot_suspend;
506
507 #define min(a,b) ((a)<(b)?(a):(b))
508 #define max(a,b) ((a)>(b)?(a):(b))
509
510 /* Install the string STR as the beginning of the string of echoing,
511 so that it serves as a prompt for the next character.
512 Also start echoing. */
513
514 echo_prompt (str)
515 char *str;
516 {
517 int len = strlen (str);
518
519 if (len > ECHOBUFSIZE - 4)
520 len = ECHOBUFSIZE - 4;
521 bcopy (str, current_kboard->echobuf, len);
522 current_kboard->echoptr = current_kboard->echobuf + len;
523 *current_kboard->echoptr = '\0';
524
525 current_kboard->echo_after_prompt = len;
526
527 echo ();
528 }
529
530 /* Add C to the echo string, if echoing is going on.
531 C can be a character, which is printed prettily ("M-C-x" and all that
532 jazz), or a symbol, whose name is printed. */
533
534 echo_char (c)
535 Lisp_Object c;
536 {
537 extern char *push_key_description ();
538
539 if (current_kboard->immediate_echo)
540 {
541 char *ptr = current_kboard->echoptr;
542
543 if (ptr != current_kboard->echobuf)
544 *ptr++ = ' ';
545
546 /* If someone has passed us a composite event, use its head symbol. */
547 c = EVENT_HEAD (c);
548
549 if (INTEGERP (c))
550 {
551 if (ptr - current_kboard->echobuf > ECHOBUFSIZE - 6)
552 return;
553
554 ptr = push_key_description (XINT (c), ptr);
555 }
556 else if (SYMBOLP (c))
557 {
558 struct Lisp_String *name = XSYMBOL (c)->name;
559 if ((ptr - current_kboard->echobuf) + name->size + 4 > ECHOBUFSIZE)
560 return;
561 bcopy (name->data, ptr, name->size);
562 ptr += name->size;
563 }
564
565 if (current_kboard->echoptr == current_kboard->echobuf
566 && EQ (c, Vhelp_char))
567 {
568 strcpy (ptr, " (Type ? for further options)");
569 ptr += strlen (ptr);
570 }
571
572 *ptr = 0;
573 current_kboard->echoptr = ptr;
574
575 echo ();
576 }
577 }
578
579 /* Temporarily add a dash to the end of the echo string if it's not
580 empty, so that it serves as a mini-prompt for the very next character. */
581
582 echo_dash ()
583 {
584 if (!current_kboard->immediate_echo
585 && current_kboard->echoptr == current_kboard->echobuf)
586 return;
587 /* Do nothing if we just printed a prompt. */
588 if (current_kboard->echo_after_prompt
589 == current_kboard->echoptr - current_kboard->echobuf)
590 return;
591 /* Do nothing if not echoing at all. */
592 if (current_kboard->echoptr == 0)
593 return;
594
595 /* Put a dash at the end of the buffer temporarily,
596 but make it go away when the next character is added. */
597 current_kboard->echoptr[0] = '-';
598 current_kboard->echoptr[1] = 0;
599
600 echo ();
601 }
602
603 /* Display the current echo string, and begin echoing if not already
604 doing so. */
605
606 echo ()
607 {
608 if (!current_kboard->immediate_echo)
609 {
610 int i;
611 current_kboard->immediate_echo = 1;
612
613 for (i = 0; i < this_command_key_count; i++)
614 {
615 Lisp_Object c;
616 c = XVECTOR (this_command_keys)->contents[i];
617 if (! (EVENT_HAS_PARAMETERS (c)
618 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
619 echo_char (c);
620 }
621 echo_dash ();
622 }
623
624 echoing = 1;
625 message1_nolog (current_kboard->echobuf);
626 echoing = 0;
627
628 if (waiting_for_input && !NILP (Vquit_flag))
629 quit_throw_to_read_char ();
630 }
631
632 /* Turn off echoing, for the start of a new command. */
633
634 cancel_echoing ()
635 {
636 current_kboard->immediate_echo = 0;
637 current_kboard->echoptr = current_kboard->echobuf;
638 current_kboard->echo_after_prompt = -1;
639 }
640
641 /* Return the length of the current echo string. */
642
643 static int
644 echo_length ()
645 {
646 return current_kboard->echoptr - current_kboard->echobuf;
647 }
648
649 /* Truncate the current echo message to its first LEN chars.
650 This and echo_char get used by read_key_sequence when the user
651 switches frames while entering a key sequence. */
652
653 static void
654 echo_truncate (len)
655 int len;
656 {
657 current_kboard->echobuf[len] = '\0';
658 current_kboard->echoptr = current_kboard->echobuf + len;
659 truncate_echo_area (len);
660 }
661
662 \f
663 /* Functions for manipulating this_command_keys. */
664 static void
665 add_command_key (key)
666 Lisp_Object key;
667 {
668 int size = XVECTOR (this_command_keys)->size;
669
670 if (this_command_key_count >= size)
671 {
672 Lisp_Object new_keys;
673
674 new_keys = Fmake_vector (make_number (size * 2), Qnil);
675 bcopy (XVECTOR (this_command_keys)->contents,
676 XVECTOR (new_keys)->contents,
677 size * sizeof (Lisp_Object));
678
679 this_command_keys = new_keys;
680 }
681
682 XVECTOR (this_command_keys)->contents[this_command_key_count++] = key;
683 }
684 \f
685 Lisp_Object
686 recursive_edit_1 ()
687 {
688 int count = specpdl_ptr - specpdl;
689 Lisp_Object val;
690
691 if (command_loop_level > 0)
692 {
693 specbind (Qstandard_output, Qt);
694 specbind (Qstandard_input, Qt);
695 }
696
697 val = command_loop ();
698 if (EQ (val, Qt))
699 Fsignal (Qquit, Qnil);
700
701 return unbind_to (count, Qnil);
702 }
703
704 /* When an auto-save happens, record the "time", and don't do again soon. */
705
706 record_auto_save ()
707 {
708 last_auto_save = num_nonmacro_input_chars;
709 }
710
711 /* Make an auto save happen as soon as possible at command level. */
712
713 force_auto_save_soon ()
714 {
715 last_auto_save = - auto_save_interval - 1;
716
717 record_asynch_buffer_change ();
718 }
719 \f
720 DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
721 "Invoke the editor command loop recursively.\n\
722 To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
723 that tells this function to return.\n\
724 Alternately, `(throw 'exit t)' makes this function signal an error.\n\
725 This function is called by the editor initialization to begin editing.")
726 ()
727 {
728 int count = specpdl_ptr - specpdl;
729 Lisp_Object val;
730
731 command_loop_level++;
732 update_mode_lines = 1;
733
734 record_unwind_protect (recursive_edit_unwind,
735 (command_loop_level
736 && current_buffer != XBUFFER (XWINDOW (selected_window)->buffer))
737 ? Fcurrent_buffer ()
738 : Qnil);
739 recursive_edit_1 ();
740 return unbind_to (count, Qnil);
741 }
742
743 Lisp_Object
744 recursive_edit_unwind (buffer)
745 Lisp_Object buffer;
746 {
747 if (!NILP (buffer))
748 Fset_buffer (buffer);
749
750 command_loop_level--;
751 update_mode_lines = 1;
752 return Qnil;
753 }
754 \f
755 static void
756 any_kboard_state ()
757 {
758 #ifdef MULTI_KBOARD
759 #if 0 /* Theory: if there's anything in Vunread_command_events,
760 it will right away be read by read_key_sequence,
761 and then if we do switch KBOARDS, it will go into the side
762 queue then. So we don't need to do anything special here -- rms. */
763 if (CONSP (Vunread_command_events))
764 {
765 current_kboard->kbd_queue
766 = nconc2 (Vunread_command_events, current_kboard->kbd_queue);
767 current_kboard->kbd_queue_has_data = 1;
768 }
769 Vunread_command_events = Qnil;
770 #endif
771 single_kboard = 0;
772 #endif
773 }
774
775 /* Switch to the single-kboard state, making current_kboard
776 the only KBOARD from which further input is accepted. */
777
778 void
779 single_kboard_state ()
780 {
781 #ifdef MULTI_KBOARD
782 single_kboard = 1;
783 #endif
784 }
785
786 /* Maintain a stack of kboards, so other parts of Emacs
787 can switch temporarily to the kboard of a given frame
788 and then revert to the previous status. */
789
790 struct kboard_stack
791 {
792 KBOARD *kboard;
793 struct kboard_stack *next;
794 };
795
796 static struct kboard_stack *kboard_stack;
797
798 void
799 push_frame_kboard (f)
800 FRAME_PTR f;
801 {
802 #ifdef MULTI_KBOARD
803 struct kboard_stack *p
804 = (struct kboard_stack *) xmalloc (sizeof (struct kboard_stack));
805
806 p->next = kboard_stack;
807 p->kboard = current_kboard;
808 kboard_stack = p;
809
810 current_kboard = FRAME_KBOARD (f);
811 #endif
812 }
813
814 void
815 pop_frame_kboard ()
816 {
817 #ifdef MULTI_KBOARD
818 struct kboard_stack *p = kboard_stack;
819 current_kboard = p->kboard;
820 kboard_stack = p->next;
821 xfree (p);
822 #endif
823 }
824 \f
825 /* Handle errors that are not handled at inner levels
826 by printing an error message and returning to the editor command loop. */
827
828 Lisp_Object
829 cmd_error (data)
830 Lisp_Object data;
831 {
832 Lisp_Object old_level, old_length;
833
834 Vstandard_output = Qt;
835 Vstandard_input = Qt;
836 Vexecuting_macro = Qnil;
837 current_kboard->Vprefix_arg = Qnil;
838 cancel_echoing ();
839
840 /* Avoid unquittable loop if data contains a circular list. */
841 old_level = Vprint_level;
842 old_length = Vprint_length;
843 XSETFASTINT(Vprint_level, 10);
844 XSETFASTINT(Vprint_length, 10);
845 cmd_error_internal (data, NULL);
846 Vprint_level = old_level;
847 Vprint_length = old_length;
848
849 Vquit_flag = Qnil;
850
851 Vinhibit_quit = Qnil;
852 #ifdef MULTI_KBOARD
853 any_kboard_state ();
854 #endif
855
856 return make_number (0);
857 }
858
859 cmd_error_internal (data, context)
860 Lisp_Object data;
861 char *context;
862 {
863 Lisp_Object errmsg, tail, errname, file_error;
864 Lisp_Object stream;
865 struct gcpro gcpro1;
866 int i;
867
868 Vquit_flag = Qnil;
869 Vinhibit_quit = Qt;
870 echo_area_glyphs = 0;
871
872 /* If the window system or terminal frame hasn't been initialized
873 yet, or we're not interactive, it's best to dump this message out
874 to stderr and exit. */
875 if (! FRAME_MESSAGE_BUF (selected_frame)
876 || noninteractive)
877 stream = Qexternal_debugging_output;
878 else
879 {
880 Fdiscard_input ();
881 bitch_at_user ();
882 stream = Qt;
883 }
884
885 if (context != 0)
886 write_string_1 (context, -1, stream);
887
888 errname = Fcar (data);
889
890 if (EQ (errname, Qerror))
891 {
892 data = Fcdr (data);
893 if (!CONSP (data)) data = Qnil;
894 errmsg = Fcar (data);
895 file_error = Qnil;
896 }
897 else
898 {
899 errmsg = Fget (errname, Qerror_message);
900 file_error = Fmemq (Qfile_error,
901 Fget (errname, Qerror_conditions));
902 }
903
904 /* Print an error message including the data items.
905 This is done by printing it into a scratch buffer
906 and then making a copy of the text in the buffer. */
907
908 if (!CONSP (data)) data = Qnil;
909 tail = Fcdr (data);
910 GCPRO1 (tail);
911
912 /* For file-error, make error message by concatenating
913 all the data items. They are all strings. */
914 if (!NILP (file_error) && !NILP (tail))
915 errmsg = XCONS (tail)->car, tail = XCONS (tail)->cdr;
916
917 if (STRINGP (errmsg))
918 Fprinc (errmsg, stream);
919 else
920 write_string_1 ("peculiar error", -1, stream);
921
922 for (i = 0; CONSP (tail); tail = Fcdr (tail), i++)
923 {
924 write_string_1 (i ? ", " : ": ", 2, stream);
925 if (!NILP (file_error))
926 Fprinc (Fcar (tail), stream);
927 else
928 Fprin1 (Fcar (tail), stream);
929 }
930 UNGCPRO;
931
932 /* If the window system or terminal frame hasn't been initialized
933 yet, or we're in -batch mode, this error should cause Emacs to exit. */
934 if (! FRAME_MESSAGE_BUF (selected_frame)
935 || noninteractive)
936 {
937 Fterpri (stream);
938 Fkill_emacs (make_number (-1));
939 }
940 }
941 \f
942 Lisp_Object command_loop_1 ();
943 Lisp_Object command_loop_2 ();
944 Lisp_Object top_level_1 ();
945
946 /* Entry to editor-command-loop.
947 This level has the catches for exiting/returning to editor command loop.
948 It returns nil to exit recursive edit, t to abort it. */
949
950 Lisp_Object
951 command_loop ()
952 {
953 if (command_loop_level > 0 || minibuf_level > 0)
954 {
955 return internal_catch (Qexit, command_loop_2, Qnil);
956 }
957 else
958 while (1)
959 {
960 internal_catch (Qtop_level, top_level_1, Qnil);
961 internal_catch (Qtop_level, command_loop_2, Qnil);
962
963 /* End of file in -batch run causes exit here. */
964 if (noninteractive)
965 Fkill_emacs (Qt);
966 }
967 }
968
969 /* Here we catch errors in execution of commands within the
970 editing loop, and reenter the editing loop.
971 When there is an error, cmd_error runs and returns a non-nil
972 value to us. A value of nil means that cmd_loop_1 itself
973 returned due to end of file (or end of kbd macro). */
974
975 Lisp_Object
976 command_loop_2 ()
977 {
978 register Lisp_Object val;
979
980 do
981 val = internal_condition_case (command_loop_1, Qerror, cmd_error);
982 while (!NILP (val));
983
984 return Qnil;
985 }
986
987 Lisp_Object
988 top_level_2 ()
989 {
990 return Feval (Vtop_level);
991 }
992
993 Lisp_Object
994 top_level_1 ()
995 {
996 /* On entry to the outer level, run the startup file */
997 if (!NILP (Vtop_level))
998 internal_condition_case (top_level_2, Qerror, cmd_error);
999 else if (!NILP (Vpurify_flag))
1000 message ("Bare impure Emacs (standard Lisp code not loaded)");
1001 else
1002 message ("Bare Emacs (standard Lisp code not loaded)");
1003 return Qnil;
1004 }
1005
1006 DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
1007 "Exit all recursive editing levels.")
1008 ()
1009 {
1010 Fthrow (Qtop_level, Qnil);
1011 }
1012
1013 DEFUN ("exit-recursive-edit", Fexit_recursive_edit, Sexit_recursive_edit, 0, 0, "",
1014 "Exit from the innermost recursive edit or minibuffer.")
1015 ()
1016 {
1017 if (command_loop_level > 0 || minibuf_level > 0)
1018 Fthrow (Qexit, Qnil);
1019
1020 error ("No recursive edit is in progress");
1021 }
1022
1023 DEFUN ("abort-recursive-edit", Fabort_recursive_edit, Sabort_recursive_edit, 0, 0, "",
1024 "Abort the command that requested this recursive edit or minibuffer input.")
1025 ()
1026 {
1027 if (command_loop_level > 0 || minibuf_level > 0)
1028 Fthrow (Qexit, Qt);
1029
1030 error ("No recursive edit is in progress");
1031 }
1032 \f
1033 /* This is the actual command reading loop,
1034 sans error-handling encapsulation. */
1035
1036 Lisp_Object Fcommand_execute ();
1037 static int read_key_sequence ();
1038 static void safe_run_hooks ();
1039
1040 Lisp_Object
1041 command_loop_1 ()
1042 {
1043 Lisp_Object cmd, tem;
1044 int lose;
1045 int nonundocount;
1046 Lisp_Object keybuf[30];
1047 int i;
1048 int no_redisplay;
1049 int no_direct;
1050 int prev_modiff;
1051 struct buffer *prev_buffer;
1052 #ifdef MULTI_KBOARD
1053 int was_locked = single_kboard;
1054 #endif
1055
1056 Vdeactivate_mark = Qnil;
1057 waiting_for_input = 0;
1058 cancel_echoing ();
1059
1060 nonundocount = 0;
1061 no_redisplay = 0;
1062 this_command_key_count = 0;
1063
1064 /* Make sure this hook runs after commands that get errors and
1065 throw to top level. */
1066 /* Note that the value cell will never directly contain nil
1067 if the symbol is a local variable. */
1068 if (!NILP (XSYMBOL (Qpost_command_hook)->value) && !NILP (Vrun_hooks))
1069 safe_run_hooks (Qpost_command_hook);
1070
1071 if (!NILP (Vdeferred_action_list))
1072 call0 (Vdeferred_action_function);
1073
1074 /* Do this after running Vpost_command_hook, for consistency. */
1075 last_command = this_command;
1076
1077 while (1)
1078 {
1079 /* Make sure the current window's buffer is selected. */
1080 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
1081 set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
1082
1083 /* Display any malloc warning that just came out. Use while because
1084 displaying one warning can cause another. */
1085
1086 while (pending_malloc_warning)
1087 display_malloc_warning ();
1088
1089 no_direct = 0;
1090
1091 Vdeactivate_mark = Qnil;
1092
1093 /* If minibuffer on and echo area in use,
1094 wait 2 sec and redraw minibuffer. */
1095
1096 if (minibuf_level && echo_area_glyphs)
1097 {
1098 /* Bind inhibit-quit to t so that C-g gets read in
1099 rather than quitting back to the minibuffer. */
1100 int count = specpdl_ptr - specpdl;
1101 specbind (Qinhibit_quit, Qt);
1102 Fsit_for (make_number (2), Qnil, Qnil);
1103 unbind_to (count, Qnil);
1104
1105 echo_area_glyphs = 0;
1106 no_direct = 1;
1107 if (!NILP (Vquit_flag))
1108 {
1109 Vquit_flag = Qnil;
1110 Vunread_command_events = Fcons (make_number (quit_char), Qnil);
1111 }
1112 }
1113
1114 #ifdef C_ALLOCA
1115 alloca (0); /* Cause a garbage collection now */
1116 /* Since we can free the most stuff here. */
1117 #endif /* C_ALLOCA */
1118
1119 #if 0
1120 #ifdef MULTI_FRAME
1121 /* Select the frame that the last event came from. Usually,
1122 switch-frame events will take care of this, but if some lisp
1123 code swallows a switch-frame event, we'll fix things up here.
1124 Is this a good idea? */
1125 if (FRAMEP (internal_last_event_frame)
1126 && XFRAME (internal_last_event_frame) != selected_frame)
1127 Fselect_frame (internal_last_event_frame, Qnil);
1128 #endif
1129 #endif
1130 /* If it has changed current-menubar from previous value,
1131 really recompute the menubar from the value. */
1132 if (! NILP (Vlucid_menu_bar_dirty_flag)
1133 && !NILP (Ffboundp (Qrecompute_lucid_menubar)))
1134 call0 (Qrecompute_lucid_menubar);
1135
1136 /* Read next key sequence; i gets its length. */
1137 i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
1138 Qnil, 0, 1);
1139
1140 ++num_input_keys;
1141
1142 /* Now we have read a key sequence of length I,
1143 or else I is 0 and we found end of file. */
1144
1145 if (i == 0) /* End of file -- happens only in */
1146 return Qnil; /* a kbd macro, at the end. */
1147 /* -1 means read_key_sequence got a menu that was rejected.
1148 Just loop around and read another command. */
1149 if (i == -1)
1150 {
1151 cancel_echoing ();
1152 this_command_key_count = 0;
1153 goto finalize;
1154 }
1155
1156 last_command_char = keybuf[i - 1];
1157
1158 /* If the previous command tried to force a specific window-start,
1159 forget about that, in case this command moves point far away
1160 from that position. But also throw away beg_unchanged and
1161 end_unchanged information in that case, so that redisplay will
1162 update the whole window properly. */
1163 if (!NILP (XWINDOW (selected_window)->force_start))
1164 {
1165 XWINDOW (selected_window)->force_start = Qnil;
1166 beg_unchanged = end_unchanged = 0;
1167 }
1168
1169 cmd = read_key_sequence_cmd;
1170 if (!NILP (Vexecuting_macro))
1171 {
1172 if (!NILP (Vquit_flag))
1173 {
1174 Vexecuting_macro = Qt;
1175 QUIT; /* Make some noise. */
1176 /* Will return since macro now empty. */
1177 }
1178 }
1179
1180 /* Do redisplay processing after this command except in special
1181 cases identified below that set no_redisplay to 1.
1182 (actually, there's currently no way to prevent the redisplay,
1183 and no_redisplay is ignored.
1184 Perhaps someday we will really implement it.) */
1185 no_redisplay = 0;
1186
1187 prev_buffer = current_buffer;
1188 prev_modiff = MODIFF;
1189 last_point_position = PT;
1190 XSETBUFFER (last_point_position_buffer, prev_buffer);
1191
1192 /* Execute the command. */
1193
1194 this_command = cmd;
1195 /* Note that the value cell will never directly contain nil
1196 if the symbol is a local variable. */
1197 if (!NILP (XSYMBOL (Qpre_command_hook)->value) && !NILP (Vrun_hooks))
1198 safe_run_hooks (Qpre_command_hook);
1199
1200 if (NILP (this_command))
1201 {
1202 /* nil means key is undefined. */
1203 bitch_at_user ();
1204 current_kboard->defining_kbd_macro = Qnil;
1205 update_mode_lines = 1;
1206 current_kboard->Vprefix_arg = Qnil;
1207 }
1208 else
1209 {
1210 if (NILP (current_kboard->Vprefix_arg) && ! no_direct)
1211 {
1212 /* Recognize some common commands in common situations and
1213 do them directly. */
1214 if (EQ (this_command, Qforward_char) && PT < ZV)
1215 {
1216 struct Lisp_Vector *dp
1217 = window_display_table (XWINDOW (selected_window));
1218 lose = FETCH_CHAR (PT);
1219 SET_PT (PT + 1);
1220 if ((dp
1221 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1222 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1223 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1224 && (lose >= 0x20 && lose < 0x7f)))
1225 : (lose >= 0x20 && lose < 0x7f))
1226 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1227 >= MODIFF)
1228 && (XFASTINT (XWINDOW (selected_window)->last_point)
1229 == PT - 1)
1230 && !windows_or_buffers_changed
1231 && EQ (current_buffer->selective_display, Qnil)
1232 && !detect_input_pending ()
1233 && NILP (Vexecuting_macro))
1234 no_redisplay = direct_output_forward_char (1);
1235 goto directly_done;
1236 }
1237 else if (EQ (this_command, Qbackward_char) && PT > BEGV)
1238 {
1239 struct Lisp_Vector *dp
1240 = window_display_table (XWINDOW (selected_window));
1241 SET_PT (PT - 1);
1242 lose = FETCH_CHAR (PT);
1243 if ((dp
1244 ? (VECTORP (DISP_CHAR_VECTOR (dp, lose))
1245 ? XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1
1246 : (NILP (DISP_CHAR_VECTOR (dp, lose))
1247 && (lose >= 0x20 && lose < 0x7f)))
1248 : (lose >= 0x20 && lose < 0x7f))
1249 && (XFASTINT (XWINDOW (selected_window)->last_modified)
1250 >= MODIFF)
1251 && (XFASTINT (XWINDOW (selected_window)->last_point)
1252 == PT + 1)
1253 && !windows_or_buffers_changed
1254 && EQ (current_buffer->selective_display, Qnil)
1255 && !detect_input_pending ()
1256 && NILP (Vexecuting_macro))
1257 no_redisplay = direct_output_forward_char (-1);
1258 goto directly_done;
1259 }
1260 else if (EQ (this_command, Qself_insert_command)
1261 /* Try this optimization only on ascii keystrokes. */
1262 && INTEGERP (last_command_char))
1263 {
1264 unsigned char c = XINT (last_command_char);
1265 int value;
1266
1267 if (NILP (Vexecuting_macro)
1268 && !EQ (minibuf_window, selected_window))
1269 {
1270 if (!nonundocount || nonundocount >= 20)
1271 {
1272 Fundo_boundary ();
1273 nonundocount = 0;
1274 }
1275 nonundocount++;
1276 }
1277 lose = ((XFASTINT (XWINDOW (selected_window)->last_modified)
1278 < MODIFF)
1279 || (XFASTINT (XWINDOW (selected_window)->last_point)
1280 != PT)
1281 || MODIFF <= SAVE_MODIFF
1282 || windows_or_buffers_changed
1283 || !EQ (current_buffer->selective_display, Qnil)
1284 || detect_input_pending ()
1285 || !NILP (Vexecuting_macro));
1286 value = internal_self_insert (c, 0);
1287 if (value)
1288 lose = 1;
1289 if (value == 2)
1290 nonundocount = 0;
1291
1292 if (!lose
1293 && (PT == ZV || FETCH_CHAR (PT) == '\n'))
1294 {
1295 struct Lisp_Vector *dp
1296 = window_display_table (XWINDOW (selected_window));
1297 int lose = c;
1298
1299 if (dp)
1300 {
1301 Lisp_Object obj;
1302
1303 obj = DISP_CHAR_VECTOR (dp, lose);
1304 if (NILP (obj))
1305 {
1306 /* Do it only for char codes
1307 that by default display as themselves. */
1308 if (lose >= 0x20 && lose <= 0x7e)
1309 no_redisplay = direct_output_for_insert (lose);
1310 }
1311 else if (VECTORP (obj)
1312 && XVECTOR (obj)->size == 1
1313 && (obj = XVECTOR (obj)->contents[0],
1314 INTEGERP (obj))
1315 /* Insist face not specified in glyph. */
1316 && (XINT (obj) & ((-1) << 8)) == 0)
1317 no_redisplay
1318 = direct_output_for_insert (XINT (obj));
1319 }
1320 else
1321 {
1322 if (lose >= 0x20 && lose <= 0x7e)
1323 no_redisplay = direct_output_for_insert (lose);
1324 }
1325 }
1326 goto directly_done;
1327 }
1328 }
1329
1330 /* Here for a command that isn't executed directly */
1331
1332 nonundocount = 0;
1333 if (NILP (current_kboard->Vprefix_arg))
1334 Fundo_boundary ();
1335 Fcommand_execute (this_command, Qnil);
1336
1337 }
1338 directly_done: ;
1339
1340 /* Note that the value cell will never directly contain nil
1341 if the symbol is a local variable. */
1342 if (!NILP (XSYMBOL (Qpost_command_hook)->value) && !NILP (Vrun_hooks))
1343 safe_run_hooks (Qpost_command_hook);
1344
1345 if (!NILP (Vdeferred_action_list))
1346 safe_run_hooks (Qdeferred_action_function);
1347
1348 /* If there is a prefix argument,
1349 1) We don't want last_command to be ``universal-argument''
1350 (that would be dumb), so don't set last_command,
1351 2) we want to leave echoing on so that the prefix will be
1352 echoed as part of this key sequence, so don't call
1353 cancel_echoing, and
1354 3) we want to leave this_command_key_count non-zero, so that
1355 read_char will realize that it is re-reading a character, and
1356 not echo it a second time. */
1357 if (NILP (current_kboard->Vprefix_arg))
1358 {
1359 last_command = this_command;
1360 cancel_echoing ();
1361 this_command_key_count = 0;
1362 }
1363
1364 if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
1365 {
1366 if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
1367 {
1368 current_buffer->mark_active = Qnil;
1369 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
1370 }
1371 else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
1372 call1 (Vrun_hooks, intern ("activate-mark-hook"));
1373 }
1374
1375 finalize:
1376 /* Install chars successfully executed in kbd macro. */
1377
1378 if (!NILP (current_kboard->defining_kbd_macro)
1379 && NILP (current_kboard->Vprefix_arg))
1380 finalize_kbd_macro_chars ();
1381
1382 #ifdef MULTI_KBOARD
1383 if (!was_locked)
1384 any_kboard_state ();
1385 #endif
1386 }
1387 }
1388
1389 /* Subroutine for safe_run_hooks: run the hook HOOK. */
1390
1391 static Lisp_Object
1392 safe_run_hooks_1 (hook)
1393 Lisp_Object hook;
1394 {
1395 return call1 (Vrun_hooks, Vinhibit_quit);
1396 }
1397
1398 /* Subroutine for safe_run_hooks: handle an error by clearing out the hook. */
1399
1400 static Lisp_Object
1401 safe_run_hooks_error (data)
1402 Lisp_Object data;
1403 {
1404 Fset (Vinhibit_quit, Qnil);
1405 }
1406
1407 /* If we get an error while running the hook, cause the hook variable
1408 to be nil. Also inhibit quits, so that C-g won't cause the hook
1409 to mysteriously evaporate. */
1410
1411 static void
1412 safe_run_hooks (hook)
1413 Lisp_Object hook;
1414 {
1415 Lisp_Object value;
1416 int count = specpdl_ptr - specpdl;
1417 specbind (Qinhibit_quit, hook);
1418
1419 internal_condition_case (safe_run_hooks_1, Qerror, safe_run_hooks_error);
1420
1421 unbind_to (count, Qnil);
1422 }
1423 \f
1424 /* Number of seconds between polling for input. */
1425 int polling_period;
1426
1427 /* Nonzero means polling for input is temporarily suppressed. */
1428 int poll_suppress_count;
1429
1430 /* Nonzero if polling_for_input is actually being used. */
1431 int polling_for_input;
1432
1433 #ifdef POLL_FOR_INPUT
1434
1435 /* Handle an alarm once each second and read pending input
1436 so as to handle a C-g if it comces in. */
1437
1438 SIGTYPE
1439 input_poll_signal (signalnum) /* If we don't have an argument, */
1440 int signalnum; /* some compilers complain in signal calls. */
1441 {
1442 if (interrupt_input_blocked == 0
1443 && !waiting_for_input)
1444 read_avail_input (0);
1445 signal (SIGALRM, input_poll_signal);
1446 alarm (polling_period);
1447 }
1448
1449 #endif
1450
1451 /* Begin signals to poll for input, if they are appropriate.
1452 This function is called unconditionally from various places. */
1453
1454 start_polling ()
1455 {
1456 #ifdef POLL_FOR_INPUT
1457 if (read_socket_hook && !interrupt_input)
1458 {
1459 poll_suppress_count--;
1460 if (poll_suppress_count == 0)
1461 {
1462 signal (SIGALRM, input_poll_signal);
1463 polling_for_input = 1;
1464 alarm (polling_period);
1465 }
1466 }
1467 #endif
1468 }
1469
1470 /* Nonzero if we are using polling to handle input asynchronously. */
1471
1472 int
1473 input_polling_used ()
1474 {
1475 #ifdef POLL_FOR_INPUT
1476 return read_socket_hook && !interrupt_input;
1477 #else
1478 return 0;
1479 #endif
1480 }
1481
1482 /* Turn off polling. */
1483
1484 stop_polling ()
1485 {
1486 #ifdef POLL_FOR_INPUT
1487 if (read_socket_hook && !interrupt_input)
1488 {
1489 if (poll_suppress_count == 0)
1490 {
1491 polling_for_input = 0;
1492 alarm (0);
1493 }
1494 poll_suppress_count++;
1495 }
1496 #endif
1497 }
1498
1499 /* Set the value of poll_suppress_count to COUNT
1500 and start or stop polling accordingly. */
1501
1502 void
1503 set_poll_suppress_count (count)
1504 int count;
1505 {
1506 #ifdef POLL_FOR_INPUT
1507 if (count == 0 && poll_suppress_count != 0)
1508 {
1509 poll_suppress_count = 1;
1510 start_polling ();
1511 }
1512 else if (count != 0 && poll_suppress_count == 0)
1513 {
1514 stop_polling ();
1515 }
1516 poll_suppress_count = count;
1517 #endif
1518 }
1519
1520 /* Bind polling_period to a value at least N.
1521 But don't decrease it. */
1522
1523 bind_polling_period (n)
1524 int n;
1525 {
1526 #ifdef POLL_FOR_INPUT
1527 int new = polling_period;
1528
1529 if (n > new)
1530 new = n;
1531
1532 stop_polling ();
1533 specbind (Qpolling_period, make_number (new));
1534 /* Start a new alarm with the new period. */
1535 start_polling ();
1536 #endif
1537 }
1538 \f
1539 /* Apply the control modifier to CHARACTER. */
1540
1541 int
1542 make_ctrl_char (c)
1543 int c;
1544 {
1545 /* Save the upper bits here. */
1546 int upper = c & ~0177;
1547
1548 c &= 0177;
1549
1550 /* Everything in the columns containing the upper-case letters
1551 denotes a control character. */
1552 if (c >= 0100 && c < 0140)
1553 {
1554 int oc = c;
1555 c &= ~0140;
1556 /* Set the shift modifier for a control char
1557 made from a shifted letter. But only for letters! */
1558 if (oc >= 'A' && oc <= 'Z')
1559 c |= shift_modifier;
1560 }
1561
1562 /* The lower-case letters denote control characters too. */
1563 else if (c >= 'a' && c <= 'z')
1564 c &= ~0140;
1565
1566 /* Include the bits for control and shift
1567 only if the basic ASCII code can't indicate them. */
1568 else if (c >= ' ')
1569 c |= ctrl_modifier;
1570
1571 /* Replace the high bits. */
1572 c |= (upper & ~ctrl_modifier);
1573
1574 return c;
1575 }
1576
1577
1578 \f
1579 /* Input of single characters from keyboard */
1580
1581 Lisp_Object print_help ();
1582 static Lisp_Object kbd_buffer_get_event ();
1583 static void record_char ();
1584
1585 #ifdef MULTI_KBOARD
1586 static jmp_buf wrong_kboard_jmpbuf;
1587 #endif
1588
1589 /* read a character from the keyboard; call the redisplay if needed */
1590 /* commandflag 0 means do not do auto-saving, but do do redisplay.
1591 -1 means do not do redisplay, but do do autosaving.
1592 1 means do both. */
1593
1594 /* The arguments MAPS and NMAPS are for menu prompting.
1595 MAPS is an array of keymaps; NMAPS is the length of MAPS.
1596
1597 PREV_EVENT is the previous input event, or nil if we are reading
1598 the first event of a key sequence.
1599
1600 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
1601 if we used a mouse menu to read the input, or zero otherwise. If
1602 USED_MOUSE_MENU is null, we don't dereference it.
1603
1604 Value is t if we showed a menu and the user rejected it. */
1605
1606 Lisp_Object
1607 read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
1608 int commandflag;
1609 int nmaps;
1610 Lisp_Object *maps;
1611 Lisp_Object prev_event;
1612 int *used_mouse_menu;
1613 {
1614 register Lisp_Object c;
1615 int count;
1616 jmp_buf local_getcjmp;
1617 jmp_buf save_jump;
1618 int key_already_recorded = 0;
1619 Lisp_Object also_record;
1620 also_record = Qnil;
1621
1622 if (CONSP (Vunread_command_events))
1623 {
1624 c = XCONS (Vunread_command_events)->car;
1625 Vunread_command_events = XCONS (Vunread_command_events)->cdr;
1626
1627 if (this_command_key_count == 0)
1628 goto reread_first;
1629 else
1630 goto reread;
1631 }
1632
1633 if (unread_command_char != -1)
1634 {
1635 XSETINT (c, unread_command_char);
1636 unread_command_char = -1;
1637
1638 if (this_command_key_count == 0)
1639 goto reread_first;
1640 else
1641 goto reread;
1642 }
1643
1644 if (!NILP (Vexecuting_macro))
1645 {
1646 #ifdef MULTI_FRAME
1647 /* We set this to Qmacro; since that's not a frame, nobody will
1648 try to switch frames on us, and the selected window will
1649 remain unchanged.
1650
1651 Since this event came from a macro, it would be misleading to
1652 leave internal_last_event_frame set to wherever the last
1653 real event came from. Normally, a switch-frame event selects
1654 internal_last_event_frame after each command is read, but
1655 events read from a macro should never cause a new frame to be
1656 selected. */
1657 Vlast_event_frame = internal_last_event_frame = Qmacro;
1658 #endif
1659
1660 /* Exit the macro if we are at the end.
1661 Also, some things replace the macro with t
1662 to force an early exit. */
1663 if (EQ (Vexecuting_macro, Qt)
1664 || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
1665 {
1666 XSETINT (c, -1);
1667 return c;
1668 }
1669
1670 c = Faref (Vexecuting_macro, make_number (executing_macro_index));
1671 if (STRINGP (Vexecuting_macro)
1672 && (XINT (c) & 0x80))
1673 XSETFASTINT (c, CHAR_META | (XINT (c) & ~0x80));
1674
1675 executing_macro_index++;
1676
1677 goto from_macro;
1678 }
1679
1680 if (!NILP (unread_switch_frame))
1681 {
1682 c = unread_switch_frame;
1683 unread_switch_frame = Qnil;
1684
1685 /* This event should make it into this_command_keys, and get echoed
1686 again, so we go to reread_first, rather than reread. */
1687 goto reread_first;
1688 }
1689
1690 if (commandflag >= 0 && !input_pending && !detect_input_pending ())
1691 redisplay ();
1692
1693 /* Message turns off echoing unless more keystrokes turn it on again. */
1694 if (echo_area_glyphs && *echo_area_glyphs
1695 && echo_area_glyphs != current_kboard->echobuf)
1696 cancel_echoing ();
1697 else
1698 /* If already echoing, continue. */
1699 echo_dash ();
1700
1701 /* Try reading a character via menu prompting in the minibuf.
1702 Try this before the sit-for, because the sit-for
1703 would do the wrong thing if we are supposed to do
1704 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
1705 after a mouse event so don't try a minibuf menu. */
1706 c = Qnil;
1707 if (nmaps > 0 && INTERACTIVE
1708 && !NILP (prev_event) && ! EVENT_HAS_PARAMETERS (prev_event)
1709 /* Don't bring up a menu if we already have another event. */
1710 && NILP (Vunread_command_events)
1711 && unread_command_char < 0
1712 && !detect_input_pending ())
1713 {
1714 c = read_char_minibuf_menu_prompt (commandflag, nmaps, maps);
1715 if (! NILP (c))
1716 {
1717 key_already_recorded = 1;
1718 goto non_reread_1;
1719 }
1720 }
1721
1722 /* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
1723 We will do that below, temporarily for short sections of code,
1724 when appropriate. local_getcjmp must be in effect
1725 around any call to sit_for or kbd_buffer_get_event;
1726 it *must not* be in effect when we call redisplay. */
1727
1728 if (_setjmp (local_getcjmp))
1729 {
1730 XSETINT (c, quit_char);
1731 #ifdef MULTI_FRAME
1732 XSETFRAME (internal_last_event_frame, selected_frame);
1733 Vlast_event_frame = internal_last_event_frame;
1734 #endif
1735 /* If we report the quit char as an event,
1736 don't do so more than once. */
1737 if (!NILP (Vinhibit_quit))
1738 Vquit_flag = Qnil;
1739
1740 #ifdef MULTI_KBOARD
1741 {
1742 KBOARD *kb = FRAME_KBOARD (selected_frame);
1743 if (kb != current_kboard)
1744 {
1745 Lisp_Object *tailp = &kb->kbd_queue;
1746 /* We shouldn't get here if we were in single-kboard mode! */
1747 if (single_kboard)
1748 abort ();
1749 while (CONSP (*tailp))
1750 tailp = &XCONS (*tailp)->cdr;
1751 if (!NILP (*tailp))
1752 abort ();
1753 *tailp = Fcons (c, Qnil);
1754 kb->kbd_queue_has_data = 1;
1755 current_kboard = kb;
1756 longjmp (wrong_kboard_jmpbuf, 1);
1757 }
1758 }
1759 #endif
1760 goto non_reread;
1761 }
1762
1763 /* If in middle of key sequence and minibuffer not active,
1764 start echoing if enough time elapses. */
1765
1766 if (minibuf_level == 0 && !current_kboard->immediate_echo
1767 && this_command_key_count > 0
1768 && ! noninteractive
1769 && echo_keystrokes > 0
1770 && (echo_area_glyphs == 0 || *echo_area_glyphs == 0))
1771 {
1772 Lisp_Object tem0;
1773
1774 /* After a mouse event, start echoing right away.
1775 This is because we are probably about to display a menu,
1776 and we don't want to delay before doing so. */
1777 if (EVENT_HAS_PARAMETERS (prev_event))
1778 echo ();
1779 else
1780 {
1781 save_getcjmp (save_jump);
1782 restore_getcjmp (local_getcjmp);
1783 tem0 = sit_for (echo_keystrokes, 0, 1, 1);
1784 restore_getcjmp (save_jump);
1785 if (EQ (tem0, Qt))
1786 echo ();
1787 }
1788 }
1789
1790 /* Maybe auto save due to number of keystrokes. */
1791
1792 if (commandflag != 0
1793 && auto_save_interval > 0
1794 && num_nonmacro_input_chars - last_auto_save > max (auto_save_interval, 20)
1795 && !detect_input_pending ())
1796 {
1797 Fdo_auto_save (Qnil, Qnil);
1798 /* Hooks can actually change some buffers in auto save. */
1799 redisplay ();
1800 }
1801
1802 /* Try reading using an X menu.
1803 This is never confused with reading using the minibuf
1804 because the recursive call of read_char in read_char_minibuf_menu_prompt
1805 does not pass on any keymaps. */
1806
1807 if (nmaps > 0 && INTERACTIVE
1808 && !NILP (prev_event)
1809 && EVENT_HAS_PARAMETERS (prev_event)
1810 && !EQ (XCONS (prev_event)->car, Qmenu_bar)
1811 /* Don't bring up a menu if we already have another event. */
1812 && NILP (Vunread_command_events)
1813 && unread_command_char < 0)
1814 c = read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu);
1815
1816 /* Maybe autosave and/or garbage collect due to idleness. */
1817
1818 if (INTERACTIVE && NILP (c))
1819 {
1820 int delay_level, buffer_size;
1821
1822 /* Slow down auto saves logarithmically in size of current buffer,
1823 and garbage collect while we're at it. */
1824 if (! MINI_WINDOW_P (XWINDOW (selected_window)))
1825 last_non_minibuf_size = Z - BEG;
1826 buffer_size = (last_non_minibuf_size >> 8) + 1;
1827 delay_level = 0;
1828 while (buffer_size > 64)
1829 delay_level++, buffer_size -= buffer_size >> 2;
1830 if (delay_level < 4) delay_level = 4;
1831 /* delay_level is 4 for files under around 50k, 7 at 100k,
1832 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
1833
1834 /* Auto save if enough time goes by without input. */
1835 if (commandflag != 0
1836 && num_nonmacro_input_chars > last_auto_save
1837 && INTEGERP (Vauto_save_timeout)
1838 && XINT (Vauto_save_timeout) > 0)
1839 {
1840 Lisp_Object tem0;
1841 int delay = delay_level * XFASTINT (Vauto_save_timeout) / 4;
1842
1843 save_getcjmp (save_jump);
1844 restore_getcjmp (local_getcjmp);
1845 tem0 = sit_for (delay, 0, 1, 1);
1846 restore_getcjmp (save_jump);
1847
1848 if (EQ (tem0, Qt))
1849 {
1850 Fdo_auto_save (Qnil, Qnil);
1851
1852 /* If we have auto-saved and there is still no input
1853 available, garbage collect if there has been enough
1854 consing going on to make it worthwhile. */
1855 if (!detect_input_pending ()
1856 && consing_since_gc > gc_cons_threshold / 2)
1857 Fgarbage_collect ();
1858
1859 redisplay ();
1860 }
1861 }
1862 }
1863
1864 /* Read something from current KBOARD's side queue, if possible. */
1865
1866 if (NILP (c))
1867 {
1868 if (current_kboard->kbd_queue_has_data)
1869 {
1870 if (!CONSP (current_kboard->kbd_queue))
1871 abort ();
1872 c = XCONS (current_kboard->kbd_queue)->car;
1873 current_kboard->kbd_queue
1874 = XCONS (current_kboard->kbd_queue)->cdr;
1875 if (NILP (current_kboard->kbd_queue))
1876 current_kboard->kbd_queue_has_data = 0;
1877 input_pending = readable_events ();
1878 #ifdef MULTI_FRAME
1879 if (EVENT_HAS_PARAMETERS (c)
1880 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qswitch_frame))
1881 internal_last_event_frame = XCONS (XCONS (c)->cdr)->car;
1882 Vlast_event_frame = internal_last_event_frame;
1883 #endif
1884 }
1885 }
1886
1887 #ifdef MULTI_KBOARD
1888 /* If current_kboard's side queue is empty check the other kboards.
1889 If one of them has data that we have not yet seen here,
1890 switch to it and process the data waiting for it.
1891
1892 Note: if the events queued up for another kboard
1893 have already been seen here, and therefore are not a complete command,
1894 the kbd_queue_has_data field is 0, so we skip that kboard here.
1895 That's to avoid an infinite loop switching between kboards here. */
1896 if (NILP (c) && !single_kboard)
1897 {
1898 KBOARD *kb;
1899 for (kb = all_kboards; kb; kb = kb->next_kboard)
1900 if (kb->kbd_queue_has_data)
1901 {
1902 current_kboard = kb;
1903 longjmp (wrong_kboard_jmpbuf, 1);
1904 }
1905 }
1906 #endif
1907
1908 wrong_kboard:
1909
1910 stop_polling ();
1911
1912 /* Finally, we read from the main queue,
1913 and if that gives us something we can't use yet, we put it on the
1914 appropriate side queue and try again. */
1915
1916 if (NILP (c))
1917 {
1918 KBOARD *kb;
1919
1920 /* Actually read a character, waiting if necessary. */
1921 save_getcjmp (save_jump);
1922 restore_getcjmp (local_getcjmp);
1923 c = kbd_buffer_get_event (&kb, used_mouse_menu);
1924 restore_getcjmp (save_jump);
1925
1926 #ifdef MULTI_KBOARD
1927 if (! NILP (c) && (kb != current_kboard))
1928 {
1929 Lisp_Object *tailp = &kb->kbd_queue;
1930 while (CONSP (*tailp))
1931 tailp = &XCONS (*tailp)->cdr;
1932 if (!NILP (*tailp))
1933 abort ();
1934 *tailp = Fcons (c, Qnil);
1935 kb->kbd_queue_has_data = 1;
1936 c = Qnil;
1937 if (single_kboard)
1938 goto wrong_kboard;
1939 current_kboard = kb;
1940 longjmp (wrong_kboard_jmpbuf, 1);
1941 }
1942 #endif
1943 }
1944
1945 /* Terminate Emacs in batch mode if at eof. */
1946 if (noninteractive && INTEGERP (c) && XINT (c) < 0)
1947 Fkill_emacs (make_number (1));
1948
1949 if (INTEGERP (c))
1950 {
1951 /* Add in any extra modifiers, where appropriate. */
1952 if ((extra_keyboard_modifiers & CHAR_CTL)
1953 || ((extra_keyboard_modifiers & 0177) < ' '
1954 && (extra_keyboard_modifiers & 0177) != 0))
1955 XSETINT (c, make_ctrl_char (XINT (c)));
1956
1957 /* Transfer any other modifier bits directly from
1958 extra_keyboard_modifiers to c. Ignore the actual character code
1959 in the low 16 bits of extra_keyboard_modifiers. */
1960 XSETINT (c, XINT (c) | (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL));
1961 }
1962
1963 non_reread:
1964
1965 start_polling ();
1966
1967 if (NILP (c))
1968 {
1969 if (commandflag >= 0
1970 && !input_pending && !detect_input_pending ())
1971 redisplay ();
1972
1973 goto wrong_kboard;
1974 }
1975
1976 non_reread_1:
1977
1978 /* Buffer switch events are only for internal wakeups
1979 so don't show them to the user. */
1980 if (BUFFERP (c))
1981 return c;
1982
1983 if (key_already_recorded)
1984 return c;
1985
1986 /* Wipe the echo area. */
1987 echo_area_glyphs = 0;
1988
1989 /* Handle things that only apply to characters. */
1990 if (INTEGERP (c))
1991 {
1992 /* If kbd_buffer_get_event gave us an EOF, return that. */
1993 if (XINT (c) == -1)
1994 return c;
1995
1996 if (STRINGP (Vkeyboard_translate_table)
1997 && XSTRING (Vkeyboard_translate_table)->size > XFASTINT (c))
1998 XSETINT (c, XSTRING (Vkeyboard_translate_table)->data[XFASTINT (c)]);
1999 }
2000
2001 /* If this event is a mouse click in the menu bar,
2002 return just menu-bar for now. Modify the mouse click event
2003 so we won't do this twice, then queue it up. */
2004 if (EVENT_HAS_PARAMETERS (c)
2005 && CONSP (XCONS (c)->cdr)
2006 && CONSP (EVENT_START (c))
2007 && CONSP (XCONS (EVENT_START (c))->cdr))
2008 {
2009 Lisp_Object posn;
2010
2011 posn = POSN_BUFFER_POSN (EVENT_START (c));
2012 /* Handle menu-bar events:
2013 insert the dummy prefix event `menu-bar'. */
2014 if (EQ (posn, Qmenu_bar))
2015 {
2016 /* Change menu-bar to (menu-bar) as the event "position". */
2017 POSN_BUFFER_POSN (EVENT_START (c)) = Fcons (posn, Qnil);
2018
2019 also_record = c;
2020 Vunread_command_events = Fcons (c, Vunread_command_events);
2021 c = posn;
2022 }
2023 }
2024
2025 record_char (c);
2026 if (! NILP (also_record))
2027 record_char (also_record);
2028
2029 from_macro:
2030 reread_first:
2031
2032 /* Don't echo mouse motion events. */
2033 if (echo_keystrokes
2034 && ! (EVENT_HAS_PARAMETERS (c)
2035 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
2036 {
2037 echo_char (c);
2038 if (! NILP (also_record))
2039 echo_char (also_record);
2040 }
2041
2042 /* Record this character as part of the current key. */
2043 add_command_key (c);
2044 if (! NILP (also_record))
2045 add_command_key (also_record);
2046
2047 /* Re-reading in the middle of a command */
2048 reread:
2049 last_input_char = c;
2050 num_input_chars++;
2051
2052 /* Process the help character specially if enabled */
2053 if (EQ (c, Vhelp_char) && !NILP (Vhelp_form))
2054 {
2055 Lisp_Object tem0;
2056 count = specpdl_ptr - specpdl;
2057
2058 record_unwind_protect (Fset_window_configuration,
2059 Fcurrent_window_configuration (Qnil));
2060
2061 tem0 = Feval (Vhelp_form);
2062 if (STRINGP (tem0))
2063 internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
2064
2065 cancel_echoing ();
2066 do
2067 c = read_char (0, 0, 0, Qnil, 0);
2068 while (BUFFERP (c));
2069 /* Remove the help from the frame */
2070 unbind_to (count, Qnil);
2071
2072 redisplay ();
2073 if (EQ (c, make_number (040)))
2074 {
2075 cancel_echoing ();
2076 do
2077 c = read_char (0, 0, 0, Qnil, 0);
2078 while (BUFFERP (c));
2079 }
2080 }
2081
2082 return c;
2083 }
2084
2085 /* Record the input event C in various ways. */
2086
2087 static void
2088 record_char (c)
2089 Lisp_Object c;
2090 {
2091 total_keys++;
2092 XVECTOR (recent_keys)->contents[recent_keys_index] = c;
2093 if (++recent_keys_index >= NUM_RECENT_KEYS)
2094 recent_keys_index = 0;
2095
2096 /* Write c to the dribble file. If c is a lispy event, write
2097 the event's symbol to the dribble file, in <brackets>. Bleaugh.
2098 If you, dear reader, have a better idea, you've got the source. :-) */
2099 if (dribble)
2100 {
2101 if (INTEGERP (c))
2102 {
2103 if (XUINT (c) < 0x100)
2104 putc (XINT (c), dribble);
2105 else
2106 fprintf (dribble, " 0x%x", (int) XUINT (c));
2107 }
2108 else
2109 {
2110 Lisp_Object dribblee;
2111
2112 /* If it's a structured event, take the event header. */
2113 dribblee = EVENT_HEAD (c);
2114
2115 if (SYMBOLP (dribblee))
2116 {
2117 putc ('<', dribble);
2118 fwrite (XSYMBOL (dribblee)->name->data, sizeof (char),
2119 XSYMBOL (dribblee)->name->size,
2120 dribble);
2121 putc ('>', dribble);
2122 }
2123 }
2124
2125 fflush (dribble);
2126 }
2127
2128 store_kbd_macro_char (c);
2129
2130 num_nonmacro_input_chars++;
2131 }
2132
2133 Lisp_Object
2134 print_help (object)
2135 Lisp_Object object;
2136 {
2137 struct buffer *old = current_buffer;
2138 Fprinc (object, Qnil);
2139 set_buffer_internal (XBUFFER (Vstandard_output));
2140 call0 (intern ("help-mode"));
2141 set_buffer_internal (old);
2142 return Qnil;
2143 }
2144
2145 /* Copy out or in the info on where C-g should throw to.
2146 This is used when running Lisp code from within get_char,
2147 in case get_char is called recursively.
2148 See read_process_output. */
2149
2150 save_getcjmp (temp)
2151 jmp_buf temp;
2152 {
2153 bcopy (getcjmp, temp, sizeof getcjmp);
2154 }
2155
2156 restore_getcjmp (temp)
2157 jmp_buf temp;
2158 {
2159 bcopy (temp, getcjmp, sizeof getcjmp);
2160 }
2161 \f
2162 #ifdef HAVE_MOUSE
2163
2164 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
2165 of this function. */
2166
2167 static Lisp_Object
2168 tracking_off (old_value)
2169 Lisp_Object old_value;
2170 {
2171 do_mouse_tracking = old_value;
2172 if (NILP (old_value))
2173 {
2174 /* Redisplay may have been preempted because there was input
2175 available, and it assumes it will be called again after the
2176 input has been processed. If the only input available was
2177 the sort that we have just disabled, then we need to call
2178 redisplay. */
2179 if (!readable_events ())
2180 {
2181 redisplay_preserve_echo_area ();
2182 get_input_pending (&input_pending);
2183 }
2184 }
2185 }
2186
2187 DEFUN ("track-mouse", Ftrack_mouse, Strack_mouse, 0, UNEVALLED, 0,
2188 "Evaluate BODY with mouse movement events enabled.\n\
2189 Within a `track-mouse' form, mouse motion generates input events that\n\
2190 you can read with `read-event'.\n\
2191 Normally, mouse motion is ignored.")
2192 (args)
2193 Lisp_Object args;
2194 {
2195 int count = specpdl_ptr - specpdl;
2196 Lisp_Object val;
2197
2198 record_unwind_protect (tracking_off, do_mouse_tracking);
2199
2200 XSETFRAME (do_mouse_tracking, selected_frame);
2201
2202 val = Fprogn (args);
2203 return unbind_to (count, val);
2204 }
2205
2206 #endif /* HAVE_MOUSE */
2207 \f
2208 /* Low level keyboard/mouse input.
2209 kbd_buffer_store_event places events in kbd_buffer, and
2210 kbd_buffer_get_event retrieves them.
2211 mouse_moved indicates when the mouse has moved again, and
2212 *mouse_position_hook provides the mouse position. */
2213
2214 /* Return true iff there are any events in the queue that read-char
2215 would return. If this returns false, a read-char would block. */
2216 static int
2217 readable_events ()
2218 {
2219 if (kbd_fetch_ptr != kbd_store_ptr)
2220 return 1;
2221 #ifdef HAVE_MOUSE
2222 if (FRAMEP (do_mouse_tracking) && mouse_moved)
2223 return 1;
2224 #endif
2225 if (single_kboard)
2226 {
2227 if (current_kboard->kbd_queue_has_data)
2228 return 1;
2229 }
2230 else
2231 {
2232 KBOARD *kb;
2233 for (kb = all_kboards; kb; kb = kb->next_kboard)
2234 if (kb->kbd_queue_has_data)
2235 return 1;
2236 }
2237 return 0;
2238 }
2239
2240 /* Set this for debugging, to have a way to get out */
2241 int stop_character;
2242
2243 #ifdef MULTI_KBOARD
2244 static KBOARD *
2245 event_to_kboard (event)
2246 struct input_event *event;
2247 {
2248 Lisp_Object frame;
2249 frame = event->frame_or_window;
2250 if (CONSP (frame))
2251 frame = XCONS (frame)->car;
2252 else if (WINDOWP (frame))
2253 frame = WINDOW_FRAME (XWINDOW (frame));
2254
2255 /* There are still some events that don't set this field.
2256 For now, just ignore the problem.
2257 Also ignore dead frames here. */
2258 if (!FRAMEP (frame) || !FRAME_LIVE_P (XFRAME (frame)))
2259 return 0;
2260 else
2261 return FRAME_KBOARD (XFRAME (frame));
2262 }
2263 #endif
2264
2265 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
2266
2267 void
2268 kbd_buffer_store_event (event)
2269 register struct input_event *event;
2270 {
2271 if (event->kind == no_event)
2272 abort ();
2273
2274 if (event->kind == ascii_keystroke)
2275 {
2276 register int c = event->code & 0377;
2277
2278 if (event->modifiers & ctrl_modifier)
2279 c = make_ctrl_char (c);
2280
2281 c |= (event->modifiers
2282 & (meta_modifier | alt_modifier
2283 | hyper_modifier | super_modifier));
2284
2285 if (c == quit_char)
2286 {
2287 extern SIGTYPE interrupt_signal ();
2288 #ifdef MULTI_KBOARD
2289 KBOARD *kb;
2290 struct input_event *sp;
2291
2292 if (single_kboard
2293 && (kb = FRAME_KBOARD (XFRAME (event->frame_or_window)),
2294 kb != current_kboard))
2295 {
2296 kb->kbd_queue
2297 = Fcons (make_lispy_switch_frame (event->frame_or_window),
2298 Fcons (make_number (c), Qnil));
2299 kb->kbd_queue_has_data = 1;
2300 for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
2301 {
2302 if (sp == kbd_buffer + KBD_BUFFER_SIZE)
2303 sp = kbd_buffer;
2304
2305 if (event_to_kboard (sp) == kb)
2306 {
2307 sp->kind = no_event;
2308 sp->frame_or_window = Qnil;
2309 }
2310 }
2311 return;
2312 }
2313 #endif
2314
2315 #ifdef MULTI_FRAME
2316 /* If this results in a quit_char being returned to Emacs as
2317 input, set Vlast_event_frame properly. If this doesn't
2318 get returned to Emacs as an event, the next event read
2319 will set Vlast_event_frame again, so this is safe to do. */
2320 {
2321 Lisp_Object focus;
2322
2323 focus = FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
2324 if (NILP (focus))
2325 focus = event->frame_or_window;
2326 internal_last_event_frame = focus;
2327 Vlast_event_frame = focus;
2328 }
2329 #endif
2330
2331 last_event_timestamp = event->timestamp;
2332 interrupt_signal ();
2333 return;
2334 }
2335
2336 if (c && c == stop_character)
2337 {
2338 sys_suspend ();
2339 return;
2340 }
2341 }
2342
2343 if (kbd_store_ptr - kbd_buffer == KBD_BUFFER_SIZE)
2344 kbd_store_ptr = kbd_buffer;
2345
2346 /* Don't let the very last slot in the buffer become full,
2347 since that would make the two pointers equal,
2348 and that is indistinguishable from an empty buffer.
2349 Discard the event if it would fill the last slot. */
2350 if (kbd_fetch_ptr - 1 != kbd_store_ptr)
2351 {
2352 volatile struct input_event *sp = kbd_store_ptr;
2353 sp->kind = event->kind;
2354 if (event->kind == selection_request_event)
2355 {
2356 /* We must not use the ordinary copying code for this case,
2357 since `part' is an enum and copying it might not copy enough
2358 in this case. */
2359 bcopy (event, (char *) sp, sizeof (*event));
2360 }
2361 else
2362 {
2363 sp->code = event->code;
2364 sp->part = event->part;
2365 sp->frame_or_window = event->frame_or_window;
2366 sp->modifiers = event->modifiers;
2367 sp->x = event->x;
2368 sp->y = event->y;
2369 sp->timestamp = event->timestamp;
2370 }
2371 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_store_ptr
2372 - kbd_buffer]
2373 = event->frame_or_window);
2374
2375 kbd_store_ptr++;
2376 }
2377 }
2378 \f
2379 /* Read one event from the event buffer, waiting if necessary.
2380 The value is a Lisp object representing the event.
2381 The value is nil for an event that should be ignored,
2382 or that was handled here.
2383 We always read and discard one event. */
2384
2385 static Lisp_Object
2386 kbd_buffer_get_event (kbp, used_mouse_menu)
2387 KBOARD **kbp;
2388 int *used_mouse_menu;
2389 {
2390 register int c;
2391 Lisp_Object obj;
2392
2393 if (noninteractive)
2394 {
2395 c = getchar ();
2396 XSETINT (obj, c);
2397 *kbp = current_kboard;
2398 return obj;
2399 }
2400
2401 /* Wait until there is input available. */
2402 for (;;)
2403 {
2404 if (kbd_fetch_ptr != kbd_store_ptr)
2405 break;
2406 #ifdef HAVE_MOUSE
2407 if (FRAMEP (do_mouse_tracking) && mouse_moved)
2408 break;
2409 #endif
2410
2411 /* If the quit flag is set, then read_char will return
2412 quit_char, so that counts as "available input." */
2413 if (!NILP (Vquit_flag))
2414 quit_throw_to_read_char ();
2415
2416 /* One way or another, wait until input is available; then, if
2417 interrupt handlers have not read it, read it now. */
2418
2419 #ifdef OLDVMS
2420 wait_for_kbd_input ();
2421 #else
2422 /* Note SIGIO has been undef'd if FIONREAD is missing. */
2423 #ifdef SIGIO
2424 gobble_input (0);
2425 #endif /* SIGIO */
2426 if (kbd_fetch_ptr != kbd_store_ptr)
2427 break;
2428 #ifdef HAVE_MOUSE
2429 if (FRAMEP (do_mouse_tracking) && mouse_moved)
2430 break;
2431 #endif
2432 {
2433 Lisp_Object minus_one;
2434
2435 XSETINT (minus_one, -1);
2436 wait_reading_process_input (0, 0, minus_one, 1);
2437
2438 if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
2439 /* Pass 1 for EXPECT since we just waited to have input. */
2440 read_avail_input (1);
2441 }
2442 #endif /* not VMS */
2443 }
2444
2445 /* At this point, we know that there is a readable event available
2446 somewhere. If the event queue is empty, then there must be a
2447 mouse movement enabled and available. */
2448 if (kbd_fetch_ptr != kbd_store_ptr)
2449 {
2450 struct input_event *event;
2451
2452 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
2453 ? kbd_fetch_ptr
2454 : kbd_buffer);
2455
2456 last_event_timestamp = event->timestamp;
2457
2458 #ifdef MULTI_KBOARD
2459 *kbp = event_to_kboard (event);
2460 if (*kbp == 0)
2461 *kbp = current_kboard; /* Better than returning null ptr? */
2462 #else
2463 *kbp = &the_only_kboard;
2464 #endif
2465
2466 obj = Qnil;
2467
2468 /* These two kinds of events get special handling
2469 and don't actually appear to the command loop.
2470 We return nil for them. */
2471 if (event->kind == selection_request_event)
2472 {
2473 #ifdef HAVE_X11
2474 struct input_event copy;
2475
2476 /* Remove it from the buffer before processing it,
2477 since otherwise swallow_events will see it
2478 and process it again. */
2479 copy = *event;
2480 kbd_fetch_ptr = event + 1;
2481 x_handle_selection_request (&copy);
2482 #else
2483 /* We're getting selection request events, but we don't have
2484 a window system. */
2485 abort ();
2486 #endif
2487 }
2488
2489 else if (event->kind == selection_clear_event)
2490 {
2491 #ifdef HAVE_X11
2492 x_handle_selection_clear (event);
2493 kbd_fetch_ptr = event + 1;
2494 #else
2495 /* We're getting selection request events, but we don't have
2496 a window system. */
2497 abort ();
2498 #endif
2499 }
2500 #ifdef HAVE_X11
2501 else if (event->kind == delete_window_event)
2502 {
2503 /* Make an event (delete-frame (FRAME)). */
2504 obj = Fcons (event->frame_or_window, Qnil);
2505 obj = Fcons (Qdelete_frame, Fcons (obj, Qnil));
2506 kbd_fetch_ptr = event + 1;
2507 }
2508 else if (event->kind == iconify_event)
2509 {
2510 /* Make an event (iconify-frame (FRAME)). */
2511 obj = Fcons (event->frame_or_window, Qnil);
2512 obj = Fcons (Qiconify_frame, Fcons (obj, Qnil));
2513 kbd_fetch_ptr = event + 1;
2514 }
2515 else if (event->kind == deiconify_event)
2516 {
2517 /* Make an event (make-frame-visible (FRAME)). */
2518 obj = Fcons (event->frame_or_window, Qnil);
2519 obj = Fcons (Qmake_frame_visible, Fcons (obj, Qnil));
2520 kbd_fetch_ptr = event + 1;
2521 }
2522 #endif
2523 else if (event->kind == buffer_switch_event)
2524 {
2525 /* The value doesn't matter here; only the type is tested. */
2526 XSETBUFFER (obj, current_buffer);
2527 kbd_fetch_ptr = event + 1;
2528 }
2529 /* Just discard these, by returning nil.
2530 With MULTI_KBOARD, these events are used as placeholders
2531 when we need to randomly delete events from the queue.
2532 (They shouldn't otherwise be found in the buffer,
2533 but on some machines it appears they do show up
2534 even without MULTI_KBOARD.) */
2535 else if (event->kind == no_event)
2536 kbd_fetch_ptr = event + 1;
2537
2538 /* If this event is on a different frame, return a switch-frame this
2539 time, and leave the event in the queue for next time. */
2540 else
2541 {
2542 #ifdef MULTI_FRAME
2543 Lisp_Object frame;
2544 Lisp_Object focus;
2545
2546 frame = event->frame_or_window;
2547 if (CONSP (frame))
2548 frame = XCONS (frame)->car;
2549 else if (WINDOWP (frame))
2550 frame = WINDOW_FRAME (XWINDOW (frame));
2551
2552 focus = FRAME_FOCUS_FRAME (XFRAME (frame));
2553 if (! NILP (focus))
2554 frame = focus;
2555
2556 if (! EQ (frame, internal_last_event_frame)
2557 && XFRAME (frame) != selected_frame)
2558 obj = make_lispy_switch_frame (frame);
2559 internal_last_event_frame = frame;
2560 #endif /* MULTI_FRAME */
2561
2562 /* If we didn't decide to make a switch-frame event, go ahead
2563 and build a real event from the queue entry. */
2564
2565 if (NILP (obj))
2566 {
2567 obj = make_lispy_event (event);
2568 #ifdef USE_X_TOOLKIT
2569 /* If this was a menu selection, then set the flag to inhibit
2570 writing to last_nonmenu_event. Don't do this if the event
2571 we're returning is (menu-bar), though; that indicates the
2572 beginning of the menu sequence, and we might as well leave
2573 that as the `event with parameters' for this selection. */
2574 if (event->kind == menu_bar_event
2575 && !(CONSP (obj) && EQ (XCONS (obj)->car, Qmenu_bar))
2576 && used_mouse_menu)
2577 *used_mouse_menu = 1;
2578 #endif
2579
2580 /* Wipe out this event, to catch bugs. */
2581 event->kind = no_event;
2582 XVECTOR (kbd_buffer_frame_or_window)->contents[event - kbd_buffer] = Qnil;
2583
2584 kbd_fetch_ptr = event + 1;
2585 }
2586 }
2587 }
2588 #ifdef HAVE_MOUSE
2589 /* Try generating a mouse motion event. */
2590 else if (FRAMEP (do_mouse_tracking) && mouse_moved)
2591 {
2592 FRAME_PTR f = XFRAME (do_mouse_tracking);
2593 Lisp_Object bar_window;
2594 enum scroll_bar_part part;
2595 Lisp_Object x, y;
2596 unsigned long time;
2597
2598 *kbp = current_kboard;
2599 /* Note that this uses F to determine which display to look at.
2600 If there is no valid info, it does not store anything
2601 so x remains nil. */
2602 x = Qnil;
2603 (*mouse_position_hook) (&f, 0, &bar_window, &part, &x, &y, &time);
2604
2605 obj = Qnil;
2606
2607 #ifdef MULTI_FRAME
2608 /* Decide if we should generate a switch-frame event. Don't
2609 generate switch-frame events for motion outside of all Emacs
2610 frames. */
2611 if (!NILP (x) && f)
2612 {
2613 Lisp_Object frame;
2614
2615 frame = FRAME_FOCUS_FRAME (f);
2616 if (NILP (frame))
2617 XSETFRAME (frame, f);
2618
2619 if (! EQ (frame, internal_last_event_frame)
2620 && XFRAME (frame) != selected_frame)
2621 obj = make_lispy_switch_frame (frame);
2622 internal_last_event_frame = frame;
2623 }
2624 #endif
2625
2626 /* If we didn't decide to make a switch-frame event, go ahead and
2627 return a mouse-motion event. */
2628 if (!NILP (x) && NILP (obj))
2629 obj = make_lispy_movement (f, bar_window, part, x, y, time);
2630 }
2631 #endif /* HAVE_MOUSE */
2632 else
2633 /* We were promised by the above while loop that there was
2634 something for us to read! */
2635 abort ();
2636
2637 input_pending = readable_events ();
2638
2639 #ifdef MULTI_FRAME
2640 Vlast_event_frame = internal_last_event_frame;
2641 #endif
2642
2643 return (obj);
2644 }
2645 \f
2646 /* Process any events that are not user-visible,
2647 then return, without reading any user-visible events. */
2648
2649 void
2650 swallow_events ()
2651 {
2652 while (kbd_fetch_ptr != kbd_store_ptr)
2653 {
2654 struct input_event *event;
2655
2656 event = ((kbd_fetch_ptr < kbd_buffer + KBD_BUFFER_SIZE)
2657 ? kbd_fetch_ptr
2658 : kbd_buffer);
2659
2660 last_event_timestamp = event->timestamp;
2661
2662 /* These two kinds of events get special handling
2663 and don't actually appear to the command loop. */
2664 if (event->kind == selection_request_event)
2665 {
2666 #ifdef HAVE_X11
2667 struct input_event copy;
2668 copy = *event;
2669 kbd_fetch_ptr = event + 1;
2670 x_handle_selection_request (&copy);
2671 #else
2672 /* We're getting selection request events, but we don't have
2673 a window system. */
2674 abort ();
2675 #endif
2676 }
2677
2678 else if (event->kind == selection_clear_event)
2679 {
2680 #ifdef HAVE_X11
2681 x_handle_selection_clear (event);
2682 kbd_fetch_ptr = event + 1;
2683 #else
2684 /* We're getting selection request events, but we don't have
2685 a window system. */
2686 abort ();
2687 #endif
2688 }
2689 else
2690 break;
2691 }
2692
2693 get_input_pending (&input_pending);
2694 }
2695 \f
2696 /* Caches for modify_event_symbol. */
2697 static Lisp_Object accent_key_syms;
2698 static Lisp_Object system_key_syms;
2699 static Lisp_Object func_key_syms;
2700 static Lisp_Object mouse_syms;
2701
2702 /* This is a list of keysym codes for special "accent" characters.
2703 It parallels lispy_accent_keys. */
2704
2705 static int lispy_accent_codes[] =
2706 {
2707 #ifdef XK_dead_circumflex
2708 XK_dead_circumflex,
2709 #else
2710 0,
2711 #endif
2712 #ifdef XK_dead_grave
2713 XK_dead_grave,
2714 #else
2715 0,
2716 #endif
2717 #ifdef XK_dead_tilde
2718 XK_dead_tilde,
2719 #else
2720 0,
2721 #endif
2722 #ifdef XK_dead_diaeresis
2723 XK_dead_diaeresis,
2724 #else
2725 0,
2726 #endif
2727 #ifdef XK_dead_macron
2728 XK_dead_macron,
2729 #else
2730 0,
2731 #endif
2732 #ifdef XK_dead_degree
2733 XK_dead_degree,
2734 #else
2735 0,
2736 #endif
2737 #ifdef XK_dead_acute
2738 XK_dead_acute,
2739 #else
2740 0,
2741 #endif
2742 #ifdef XK_dead_cedilla
2743 XK_dead_cedilla,
2744 #else
2745 0,
2746 #endif
2747 #ifdef XK_dead_breve
2748 XK_dead_breve,
2749 #else
2750 0,
2751 #endif
2752 #ifdef XK_dead_ogonek
2753 XK_dead_ogonek,
2754 #else
2755 0,
2756 #endif
2757 #ifdef XK_dead_caron
2758 XK_dead_caron,
2759 #else
2760 0,
2761 #endif
2762 #ifdef XK_dead_doubleacute
2763 XK_dead_doubleacute,
2764 #else
2765 0,
2766 #endif
2767 #ifdef XK_dead_abovedot
2768 XK_dead_abovedot,
2769 #else
2770 0,
2771 #endif
2772 };
2773
2774 /* This is a list of Lisp names for special "accent" characters.
2775 It parallels lispy_accent_codes. */
2776
2777 static char *lispy_accent_keys[] =
2778 {
2779 "dead-circumflex",
2780 "dead-grave",
2781 "dead-tilde",
2782 "dead-diaeresis",
2783 "dead-macron",
2784 "dead-degree",
2785 "dead-acute",
2786 "dead-cedilla",
2787 "dead-breve",
2788 "dead-ogonek",
2789 "dead-caron",
2790 "dead-doubleacute",
2791 "dead-abovedot",
2792 };
2793
2794 /* You'll notice that this table is arranged to be conveniently
2795 indexed by X Windows keysym values. */
2796 static char *lispy_function_keys[] =
2797 {
2798 /* X Keysym value */
2799
2800 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00 */
2801 "backspace",
2802 "tab",
2803 "linefeed",
2804 "clear",
2805 0,
2806 "return",
2807 0, 0,
2808 0, 0, 0, /* 0xff10 */
2809 "pause",
2810 0, 0, 0, 0, 0, 0, 0,
2811 "escape",
2812 0, 0, 0, 0,
2813 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff20...2f */
2814 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff30...3f */
2815 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
2816
2817 "home", /* 0xff50 */ /* IsCursorKey */
2818 "left",
2819 "up",
2820 "right",
2821 "down",
2822 "prior",
2823 "next",
2824 "end",
2825 "begin",
2826 0, /* 0xff59 */
2827 0, 0, 0, 0, 0, 0,
2828 "select", /* 0xff60 */ /* IsMiscFunctionKey */
2829 "print",
2830 "execute",
2831 "insert",
2832 0, /* 0xff64 */
2833 "undo",
2834 "redo",
2835 "menu",
2836 "find",
2837 "cancel",
2838 "help",
2839 "break", /* 0xff6b */
2840
2841 0, 0, 0, 0, 0, 0, 0, 0, "backtab", 0,
2842 0, /* 0xff76 */
2843 0, 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff7f */
2844 "kp-space", /* 0xff80 */ /* IsKeypadKey */
2845 0, 0, 0, 0, 0, 0, 0, 0,
2846 "kp-tab", /* 0xff89 */
2847 0, 0, 0,
2848 "kp-enter", /* 0xff8d */
2849 0, 0, 0,
2850 "kp-f1", /* 0xff91 */
2851 "kp-f2",
2852 "kp-f3",
2853 "kp-f4",
2854 "kp-home", /* 0xff95 */
2855 "kp-left",
2856 "kp-up",
2857 "kp-right",
2858 "kp-down",
2859 "kp-prior", /* kp-page-up */
2860 "kp-next", /* kp-page-down */
2861 "kp-end",
2862 "kp-begin",
2863 "kp-insert",
2864 "kp-delete",
2865 0, /* 0xffa0 */
2866 0, 0, 0, 0, 0, 0, 0, 0, 0,
2867 "kp-multiply", /* 0xffaa */
2868 "kp-add",
2869 "kp-separator",
2870 "kp-subtract",
2871 "kp-decimal",
2872 "kp-divide", /* 0xffaf */
2873 "kp-0", /* 0xffb0 */
2874 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
2875 0, /* 0xffba */
2876 0, 0,
2877 "kp-equal", /* 0xffbd */
2878 "f1", /* 0xffbe */ /* IsFunctionKey */
2879 "f2",
2880 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
2881 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
2882 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
2883 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
2884 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
2885 0, 0, 0, 0, 0, 0, 0, 0,
2886 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
2887 0, 0, 0, 0, 0, 0, 0, "delete"
2888 };
2889
2890 static char *lispy_mouse_names[] =
2891 {
2892 "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
2893 };
2894
2895 /* Scroll bar parts. */
2896 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
2897
2898 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
2899 Lisp_Object *scroll_bar_parts[] = {
2900 &Qabove_handle, &Qhandle, &Qbelow_handle
2901 };
2902
2903
2904 /* A vector, indexed by button number, giving the down-going location
2905 of currently depressed buttons, both scroll bar and non-scroll bar.
2906
2907 The elements have the form
2908 (BUTTON-NUMBER MODIFIER-MASK . REST)
2909 where REST is the cdr of a position as it would be reported in the event.
2910
2911 The make_lispy_event function stores positions here to tell the
2912 difference between click and drag events, and to store the starting
2913 location to be included in drag events. */
2914
2915 static Lisp_Object button_down_location;
2916
2917 /* Information about the most recent up-going button event: Which
2918 button, what location, and what time. */
2919
2920 static int last_mouse_button;
2921 static int last_mouse_x;
2922 static int last_mouse_y;
2923 static unsigned long button_down_time;
2924
2925 /* The maximum time between clicks to make a double-click,
2926 or Qnil to disable double-click detection,
2927 or Qt for no time limit. */
2928 Lisp_Object Vdouble_click_time;
2929
2930 /* The number of clicks in this multiple-click. */
2931
2932 int double_click_count;
2933
2934 /* Given a struct input_event, build the lisp event which represents
2935 it. If EVENT is 0, build a mouse movement event from the mouse
2936 movement buffer, which should have a movement event in it.
2937
2938 Note that events must be passed to this function in the order they
2939 are received; this function stores the location of button presses
2940 in order to build drag events when the button is released. */
2941
2942 static Lisp_Object
2943 make_lispy_event (event)
2944 struct input_event *event;
2945 {
2946 int i;
2947
2948 switch (SWITCH_ENUM_CAST (event->kind))
2949 {
2950 /* A simple keystroke. */
2951 case ascii_keystroke:
2952 {
2953 Lisp_Object lispy_c;
2954 int c = event->code & 0377;
2955 /* Turn ASCII characters into control characters
2956 when proper. */
2957 if (event->modifiers & ctrl_modifier)
2958 c = make_ctrl_char (c);
2959
2960 /* Add in the other modifier bits. We took care of ctrl_modifier
2961 just above, and the shift key was taken care of by the X code,
2962 and applied to control characters by make_ctrl_char. */
2963 c |= (event->modifiers
2964 & (meta_modifier | alt_modifier
2965 | hyper_modifier | super_modifier));
2966 button_down_time = 0;
2967 XSETFASTINT (lispy_c, c);
2968 return lispy_c;
2969 }
2970
2971 /* A function key. The symbol may need to have modifier prefixes
2972 tacked onto it. */
2973 case non_ascii_keystroke:
2974 button_down_time = 0;
2975
2976 for (i = 0; i < sizeof (lispy_accent_codes) / sizeof (int); i++)
2977 if (event->code == lispy_accent_codes[i])
2978 return modify_event_symbol (i,
2979 event->modifiers,
2980 Qfunction_key, Qnil,
2981 lispy_accent_keys, &accent_key_syms,
2982 (sizeof (lispy_accent_keys)
2983 / sizeof (lispy_accent_keys[0])));
2984
2985 /* Handle system-specific keysyms. */
2986 if (event->code & (1 << 28))
2987 {
2988 /* We need to use an alist rather than a vector as the cache
2989 since we can't make a vector long enuf. */
2990 if (NILP (system_key_syms))
2991 system_key_syms = Fcons (Qnil, Qnil);
2992 return modify_event_symbol (event->code & 0xffffff,
2993 event->modifiers,
2994 Qfunction_key,
2995 current_kboard->Vsystem_key_alist,
2996 0, &system_key_syms, 0xffffff);
2997 }
2998
2999 return modify_event_symbol (event->code - 0xff00,
3000 event->modifiers,
3001 Qfunction_key, Qnil,
3002 lispy_function_keys, &func_key_syms,
3003 (sizeof (lispy_function_keys)
3004 / sizeof (lispy_function_keys[0])));
3005 break;
3006
3007 #ifdef HAVE_MOUSE
3008 /* A mouse click. Figure out where it is, decide whether it's
3009 a press, click or drag, and build the appropriate structure. */
3010 case mouse_click:
3011 case scroll_bar_click:
3012 {
3013 int button = event->code;
3014 int is_double;
3015 Lisp_Object position;
3016 Lisp_Object *start_pos_ptr;
3017 Lisp_Object start_pos;
3018
3019 if (button < 0 || button >= NUM_MOUSE_BUTTONS)
3020 abort ();
3021
3022 /* Build the position as appropriate for this mouse click. */
3023 if (event->kind == mouse_click)
3024 {
3025 int part;
3026 FRAME_PTR f = XFRAME (event->frame_or_window);
3027 Lisp_Object window;
3028 Lisp_Object posn;
3029 int row, column;
3030
3031 /* Ignore mouse events that were made on frame that
3032 have been deleted. */
3033 if (! FRAME_LIVE_P (f))
3034 return Qnil;
3035
3036 pixel_to_glyph_coords (f, XINT (event->x), XINT (event->y),
3037 &column, &row, NULL, 1);
3038
3039 #ifndef USE_X_TOOLKIT
3040 /* In the non-toolkit version, clicks on the menu bar
3041 are ordinary button events in the event buffer.
3042 Distinguish them, and invoke the menu.
3043
3044 (In the toolkit version, the toolkit handles the menu bar
3045 and Emacs doesn't know about it until after the user
3046 makes a selection.) */
3047 if (row >= 0 && row < FRAME_MENU_BAR_LINES (f))
3048 {
3049 Lisp_Object items, item;
3050 int hpos;
3051 int i;
3052
3053 /* Activate the menu bar on the down event. If the
3054 up event comes in before the menu code can deal with it,
3055 just ignore it. */
3056 if (! (event->modifiers & down_modifier))
3057 return Qnil;
3058
3059 item = Qnil;
3060 items = FRAME_MENU_BAR_ITEMS (f);
3061 for (i = 0; i < XVECTOR (items)->size; i += 3)
3062 {
3063 Lisp_Object pos, string;
3064 string = XVECTOR (items)->contents[i + 1];
3065 pos = XVECTOR (items)->contents[i + 2];
3066 if (NILP (string))
3067 break;
3068 if (column >= XINT (pos)
3069 && column < XINT (pos) + XSTRING (string)->size)
3070 {
3071 item = XVECTOR (items)->contents[i];
3072 break;
3073 }
3074 }
3075
3076 position
3077 = Fcons (event->frame_or_window,
3078 Fcons (Qmenu_bar,
3079 Fcons (Fcons (event->x, event->y),
3080 Fcons (make_number (event->timestamp),
3081 Qnil))));
3082
3083 return Fcons (item, Fcons (position, Qnil));
3084 }
3085 #endif /* not USE_X_TOOLKIT */
3086
3087 window = window_from_coordinates (f, column, row, &part);
3088
3089 if (!WINDOWP (window))
3090 {
3091 window = event->frame_or_window;
3092 posn = Qnil;
3093 }
3094 else
3095 {
3096 int pixcolumn, pixrow;
3097 column -= XINT (XWINDOW (window)->left);
3098 row -= XINT (XWINDOW (window)->top);
3099 glyph_to_pixel_coords (f, column, row, &pixcolumn, &pixrow);
3100 XSETINT (event->x, pixcolumn);
3101 XSETINT (event->y, pixrow);
3102
3103 if (part == 1)
3104 posn = Qmode_line;
3105 else if (part == 2)
3106 posn = Qvertical_line;
3107 else
3108 XSETINT (posn,
3109 buffer_posn_from_coords (XWINDOW (window),
3110 column, row));
3111 }
3112
3113 position
3114 = Fcons (window,
3115 Fcons (posn,
3116 Fcons (Fcons (event->x, event->y),
3117 Fcons (make_number (event->timestamp),
3118 Qnil))));
3119 }
3120 else
3121 {
3122 Lisp_Object window;
3123 Lisp_Object portion_whole;
3124 Lisp_Object part;
3125
3126 window = event->frame_or_window;
3127 portion_whole = Fcons (event->x, event->y);
3128 part = *scroll_bar_parts[(int) event->part];
3129
3130 position =
3131 Fcons (window,
3132 Fcons (Qvertical_scroll_bar,
3133 Fcons (portion_whole,
3134 Fcons (make_number (event->timestamp),
3135 Fcons (part, Qnil)))));
3136 }
3137
3138 start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
3139
3140 start_pos = *start_pos_ptr;
3141 *start_pos_ptr = Qnil;
3142
3143 is_double = (button == last_mouse_button
3144 && XINT (event->x) == last_mouse_x
3145 && XINT (event->y) == last_mouse_y
3146 && button_down_time != 0
3147 && (EQ (Vdouble_click_time, Qt)
3148 || (INTEGERP (Vdouble_click_time)
3149 && ((int)(event->timestamp - button_down_time)
3150 < XINT (Vdouble_click_time)))));
3151 last_mouse_button = button;
3152 last_mouse_x = XINT (event->x);
3153 last_mouse_y = XINT (event->y);
3154
3155 /* If this is a button press, squirrel away the location, so
3156 we can decide later whether it was a click or a drag. */
3157 if (event->modifiers & down_modifier)
3158 {
3159 if (is_double)
3160 {
3161 double_click_count++;
3162 event->modifiers |= ((double_click_count > 2)
3163 ? triple_modifier
3164 : double_modifier);
3165 }
3166 else
3167 double_click_count = 1;
3168 button_down_time = event->timestamp;
3169 *start_pos_ptr = Fcopy_alist (position);
3170 }
3171
3172 /* Now we're releasing a button - check the co-ordinates to
3173 see if this was a click or a drag. */
3174 else if (event->modifiers & up_modifier)
3175 {
3176 /* If we did not see a down before this up,
3177 ignore the up. Probably this happened because
3178 the down event chose a menu item.
3179 It would be an annoyance to treat the release
3180 of the button that chose the menu item
3181 as a separate event. */
3182
3183 if (!CONSP (start_pos))
3184 return Qnil;
3185
3186 event->modifiers &= ~up_modifier;
3187 #if 0 /* Formerly we treated an up with no down as a click event. */
3188 if (!CONSP (start_pos))
3189 event->modifiers |= click_modifier;
3190 else
3191 #endif
3192 {
3193 /* The third element of every position should be the (x,y)
3194 pair. */
3195 Lisp_Object down;
3196
3197 down = Fnth (make_number (2), start_pos);
3198 if (EQ (event->x, XCONS (down)->car)
3199 && EQ (event->y, XCONS (down)->cdr))
3200 {
3201 event->modifiers |= click_modifier;
3202 }
3203 else
3204 {
3205 button_down_time = 0;
3206 event->modifiers |= drag_modifier;
3207 }
3208 /* Don't check is_double; treat this as multiple
3209 if the down-event was multiple. */
3210 if (double_click_count > 1)
3211 event->modifiers |= ((double_click_count > 2)
3212 ? triple_modifier
3213 : double_modifier);
3214 }
3215 }
3216 else
3217 /* Every mouse event should either have the down_modifier or
3218 the up_modifier set. */
3219 abort ();
3220
3221 {
3222 /* Get the symbol we should use for the mouse click. */
3223 Lisp_Object head;
3224
3225 head = modify_event_symbol (button,
3226 event->modifiers,
3227 Qmouse_click, Qnil,
3228 lispy_mouse_names, &mouse_syms,
3229 (sizeof (lispy_mouse_names)
3230 / sizeof (lispy_mouse_names[0])));
3231 if (event->modifiers & drag_modifier)
3232 return Fcons (head,
3233 Fcons (start_pos,
3234 Fcons (position,
3235 Qnil)));
3236 else if (event->modifiers & (double_modifier | triple_modifier))
3237 return Fcons (head,
3238 Fcons (position,
3239 Fcons (make_number (double_click_count),
3240 Qnil)));
3241 else
3242 return Fcons (head,
3243 Fcons (position,
3244 Qnil));
3245 }
3246 }
3247 #endif /* HAVE_MOUSE */
3248
3249 #ifdef USE_X_TOOLKIT
3250 case menu_bar_event:
3251 /* The event value is in the cdr of the frame_or_window slot. */
3252 if (!CONSP (event->frame_or_window))
3253 abort ();
3254 return XCONS (event->frame_or_window)->cdr;
3255 #endif
3256
3257 /* The 'kind' field of the event is something we don't recognize. */
3258 default:
3259 abort ();
3260 }
3261 }
3262
3263 #ifdef HAVE_MOUSE
3264
3265 static Lisp_Object
3266 make_lispy_movement (frame, bar_window, part, x, y, time)
3267 FRAME_PTR frame;
3268 Lisp_Object bar_window;
3269 enum scroll_bar_part part;
3270 Lisp_Object x, y;
3271 unsigned long time;
3272 {
3273 #ifdef MULTI_FRAME
3274 /* Is it a scroll bar movement? */
3275 if (frame && ! NILP (bar_window))
3276 {
3277 Lisp_Object part_sym;
3278
3279 part_sym = *scroll_bar_parts[(int) part];
3280 return Fcons (Qscroll_bar_movement,
3281 (Fcons (Fcons (bar_window,
3282 Fcons (Qvertical_scroll_bar,
3283 Fcons (Fcons (x, y),
3284 Fcons (make_number (time),
3285 Fcons (part_sym,
3286 Qnil))))),
3287 Qnil)));
3288 }
3289
3290 /* Or is it an ordinary mouse movement? */
3291 else
3292 #endif /* MULTI_FRAME */
3293 {
3294 int area;
3295 Lisp_Object window;
3296 Lisp_Object posn;
3297 int column, row;
3298
3299 #ifdef MULTI_FRAME
3300 if (frame)
3301 #else
3302 if (1)
3303 #endif
3304 {
3305 /* It's in a frame; which window on that frame? */
3306 pixel_to_glyph_coords (frame, XINT (x), XINT (y), &column, &row,
3307 NULL, 1);
3308 window = window_from_coordinates (frame, column, row, &area);
3309 }
3310 else
3311 window = Qnil;
3312
3313 if (WINDOWP (window))
3314 {
3315 int pixcolumn, pixrow;
3316 column -= XINT (XWINDOW (window)->left);
3317 row -= XINT (XWINDOW (window)->top);
3318 glyph_to_pixel_coords (frame, column, row, &pixcolumn, &pixrow);
3319 XSETINT (x, pixcolumn);
3320 XSETINT (y, pixrow);
3321
3322 if (area == 1)
3323 posn = Qmode_line;
3324 else if (area == 2)
3325 posn = Qvertical_line;
3326 else
3327 XSETINT (posn,
3328 buffer_posn_from_coords (XWINDOW (window), column, row));
3329 }
3330 #ifdef MULTI_FRAME
3331 else if (frame != 0)
3332 {
3333 XSETFRAME (window, frame);
3334 posn = Qnil;
3335 }
3336 #endif
3337 else
3338 {
3339 window = Qnil;
3340 posn = Qnil;
3341 XSETFASTINT (x, 0);
3342 XSETFASTINT (y, 0);
3343 }
3344
3345 return Fcons (Qmouse_movement,
3346 Fcons (Fcons (window,
3347 Fcons (posn,
3348 Fcons (Fcons (x, y),
3349 Fcons (make_number (time),
3350 Qnil)))),
3351 Qnil));
3352 }
3353 }
3354
3355 #endif /* HAVE_MOUSE */
3356
3357 /* Construct a switch frame event. */
3358 static Lisp_Object
3359 make_lispy_switch_frame (frame)
3360 Lisp_Object frame;
3361 {
3362 return Fcons (Qswitch_frame, Fcons (frame, Qnil));
3363 }
3364 \f
3365 /* Manipulating modifiers. */
3366
3367 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
3368
3369 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
3370 SYMBOL's name of the end of the modifiers; the string from this
3371 position is the unmodified symbol name.
3372
3373 This doesn't use any caches. */
3374
3375 static int
3376 parse_modifiers_uncached (symbol, modifier_end)
3377 Lisp_Object symbol;
3378 int *modifier_end;
3379 {
3380 struct Lisp_String *name;
3381 int i;
3382 int modifiers;
3383
3384 CHECK_SYMBOL (symbol, 1);
3385
3386 modifiers = 0;
3387 name = XSYMBOL (symbol)->name;
3388
3389 for (i = 0; i+2 <= name->size; )
3390 {
3391 int this_mod_end = 0;
3392 int this_mod = 0;
3393
3394 /* See if the name continues with a modifier word.
3395 Check that the word appears, but don't check what follows it.
3396 Set this_mod and this_mod_end to record what we find. */
3397
3398 switch (name->data[i])
3399 {
3400 #define SINGLE_LETTER_MOD(BIT) \
3401 (this_mod_end = i + 1, this_mod = BIT)
3402
3403 case 'A':
3404 SINGLE_LETTER_MOD (alt_modifier);
3405 break;
3406
3407 case 'C':
3408 SINGLE_LETTER_MOD (ctrl_modifier);
3409 break;
3410
3411 case 'H':
3412 SINGLE_LETTER_MOD (hyper_modifier);
3413 break;
3414
3415 case 'M':
3416 SINGLE_LETTER_MOD (meta_modifier);
3417 break;
3418
3419 case 'S':
3420 SINGLE_LETTER_MOD (shift_modifier);
3421 break;
3422
3423 case 's':
3424 SINGLE_LETTER_MOD (super_modifier);
3425 break;
3426
3427 #undef SINGLE_LETTER_MOD
3428 }
3429
3430 /* If we found no modifier, stop looking for them. */
3431 if (this_mod_end == 0)
3432 break;
3433
3434 /* Check there is a dash after the modifier, so that it
3435 really is a modifier. */
3436 if (this_mod_end >= name->size || name->data[this_mod_end] != '-')
3437 break;
3438
3439 /* This modifier is real; look for another. */
3440 modifiers |= this_mod;
3441 i = this_mod_end + 1;
3442 }
3443
3444 /* Should we include the `click' modifier? */
3445 if (! (modifiers & (down_modifier | drag_modifier
3446 | double_modifier | triple_modifier))
3447 && i + 7 == name->size
3448 && strncmp (name->data + i, "mouse-", 6) == 0
3449 && ('0' <= name->data[i + 6] && name->data[i + 6] <= '9'))
3450 modifiers |= click_modifier;
3451
3452 if (modifier_end)
3453 *modifier_end = i;
3454
3455 return modifiers;
3456 }
3457
3458 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
3459 prepended to the string BASE[0..BASE_LEN-1].
3460 This doesn't use any caches. */
3461 static Lisp_Object
3462 apply_modifiers_uncached (modifiers, base, base_len)
3463 int modifiers;
3464 char *base;
3465 int base_len;
3466 {
3467 /* Since BASE could contain nulls, we can't use intern here; we have
3468 to use Fintern, which expects a genuine Lisp_String, and keeps a
3469 reference to it. */
3470 char *new_mods =
3471 (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
3472 int mod_len;
3473
3474 {
3475 char *p = new_mods;
3476
3477 /* Only the event queue may use the `up' modifier; it should always
3478 be turned into a click or drag event before presented to lisp code. */
3479 if (modifiers & up_modifier)
3480 abort ();
3481
3482 if (modifiers & alt_modifier) { *p++ = 'A'; *p++ = '-'; }
3483 if (modifiers & ctrl_modifier) { *p++ = 'C'; *p++ = '-'; }
3484 if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
3485 if (modifiers & meta_modifier) { *p++ = 'M'; *p++ = '-'; }
3486 if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
3487 if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
3488 if (modifiers & double_modifier) { strcpy (p, "double-"); p += 7; }
3489 if (modifiers & triple_modifier) { strcpy (p, "triple-"); p += 7; }
3490 if (modifiers & down_modifier) { strcpy (p, "down-"); p += 5; }
3491 if (modifiers & drag_modifier) { strcpy (p, "drag-"); p += 5; }
3492 /* The click modifier is denoted by the absence of other modifiers. */
3493
3494 *p = '\0';
3495
3496 mod_len = p - new_mods;
3497 }
3498
3499 {
3500 Lisp_Object new_name;
3501
3502 new_name = make_uninit_string (mod_len + base_len);
3503 bcopy (new_mods, XSTRING (new_name)->data, mod_len);
3504 bcopy (base, XSTRING (new_name)->data + mod_len, base_len);
3505
3506 return Fintern (new_name, Qnil);
3507 }
3508 }
3509
3510
3511 static char *modifier_names[] =
3512 {
3513 "up", "down", "drag", "click", "double", "triple", 0, 0,
3514 0, 0, 0, 0, 0, 0, 0, 0,
3515 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
3516 };
3517 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
3518
3519 static Lisp_Object modifier_symbols;
3520
3521 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
3522 static Lisp_Object
3523 lispy_modifier_list (modifiers)
3524 int modifiers;
3525 {
3526 Lisp_Object modifier_list;
3527 int i;
3528
3529 modifier_list = Qnil;
3530 for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
3531 if (modifiers & (1<<i))
3532 modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
3533 modifier_list);
3534
3535 return modifier_list;
3536 }
3537
3538
3539 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
3540 where UNMODIFIED is the unmodified form of SYMBOL,
3541 MASK is the set of modifiers present in SYMBOL's name.
3542 This is similar to parse_modifiers_uncached, but uses the cache in
3543 SYMBOL's Qevent_symbol_element_mask property, and maintains the
3544 Qevent_symbol_elements property. */
3545
3546 static Lisp_Object
3547 parse_modifiers (symbol)
3548 Lisp_Object symbol;
3549 {
3550 Lisp_Object elements;
3551
3552 elements = Fget (symbol, Qevent_symbol_element_mask);
3553 if (CONSP (elements))
3554 return elements;
3555 else
3556 {
3557 int end;
3558 int modifiers = parse_modifiers_uncached (symbol, &end);
3559 Lisp_Object unmodified;
3560 Lisp_Object mask;
3561
3562 unmodified = Fintern (make_string (XSYMBOL (symbol)->name->data + end,
3563 XSYMBOL (symbol)->name->size - end),
3564 Qnil);
3565
3566 if (modifiers & ~(((EMACS_INT)1 << VALBITS) - 1))
3567 abort ();
3568 XSETFASTINT (mask, modifiers);
3569 elements = Fcons (unmodified, Fcons (mask, Qnil));
3570
3571 /* Cache the parsing results on SYMBOL. */
3572 Fput (symbol, Qevent_symbol_element_mask,
3573 elements);
3574 Fput (symbol, Qevent_symbol_elements,
3575 Fcons (unmodified, lispy_modifier_list (modifiers)));
3576
3577 /* Since we know that SYMBOL is modifiers applied to unmodified,
3578 it would be nice to put that in unmodified's cache.
3579 But we can't, since we're not sure that parse_modifiers is
3580 canonical. */
3581
3582 return elements;
3583 }
3584 }
3585
3586 /* Apply the modifiers MODIFIERS to the symbol BASE.
3587 BASE must be unmodified.
3588
3589 This is like apply_modifiers_uncached, but uses BASE's
3590 Qmodifier_cache property, if present. It also builds
3591 Qevent_symbol_elements properties, since it has that info anyway.
3592
3593 apply_modifiers copies the value of BASE's Qevent_kind property to
3594 the modified symbol. */
3595 static Lisp_Object
3596 apply_modifiers (modifiers, base)
3597 int modifiers;
3598 Lisp_Object base;
3599 {
3600 Lisp_Object cache, index, entry, new_symbol;
3601
3602 /* Mask out upper bits. We don't know where this value's been. */
3603 modifiers &= ((EMACS_INT)1 << VALBITS) - 1;
3604
3605 /* The click modifier never figures into cache indices. */
3606 cache = Fget (base, Qmodifier_cache);
3607 XSETFASTINT (index, (modifiers & ~click_modifier));
3608 entry = assq_no_quit (index, cache);
3609
3610 if (CONSP (entry))
3611 new_symbol = XCONS (entry)->cdr;
3612 else
3613 {
3614 /* We have to create the symbol ourselves. */
3615 new_symbol = apply_modifiers_uncached (modifiers,
3616 XSYMBOL (base)->name->data,
3617 XSYMBOL (base)->name->size);
3618
3619 /* Add the new symbol to the base's cache. */
3620 entry = Fcons (index, new_symbol);
3621 Fput (base, Qmodifier_cache, Fcons (entry, cache));
3622
3623 /* We have the parsing info now for free, so add it to the caches. */
3624 XSETFASTINT (index, modifiers);
3625 Fput (new_symbol, Qevent_symbol_element_mask,
3626 Fcons (base, Fcons (index, Qnil)));
3627 Fput (new_symbol, Qevent_symbol_elements,
3628 Fcons (base, lispy_modifier_list (modifiers)));
3629 }
3630
3631 /* Make sure this symbol is of the same kind as BASE.
3632
3633 You'd think we could just set this once and for all when we
3634 intern the symbol above, but reorder_modifiers may call us when
3635 BASE's property isn't set right; we can't assume that just
3636 because it has a Qmodifier_cache property it must have its
3637 Qevent_kind set right as well. */
3638 if (NILP (Fget (new_symbol, Qevent_kind)))
3639 {
3640 Lisp_Object kind;
3641
3642 kind = Fget (base, Qevent_kind);
3643 if (! NILP (kind))
3644 Fput (new_symbol, Qevent_kind, kind);
3645 }
3646
3647 return new_symbol;
3648 }
3649
3650
3651 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
3652 return a symbol with the modifiers placed in the canonical order.
3653 Canonical order is alphabetical, except for down and drag, which
3654 always come last. The 'click' modifier is never written out.
3655
3656 Fdefine_key calls this to make sure that (for example) C-M-foo
3657 and M-C-foo end up being equivalent in the keymap. */
3658
3659 Lisp_Object
3660 reorder_modifiers (symbol)
3661 Lisp_Object symbol;
3662 {
3663 /* It's hopefully okay to write the code this way, since everything
3664 will soon be in caches, and no consing will be done at all. */
3665 Lisp_Object parsed;
3666
3667 parsed = parse_modifiers (symbol);
3668 return apply_modifiers ((int) XINT (XCONS (XCONS (parsed)->cdr)->car),
3669 XCONS (parsed)->car);
3670 }
3671
3672
3673 /* For handling events, we often want to produce a symbol whose name
3674 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
3675 to some base, like the name of a function key or mouse button.
3676 modify_event_symbol produces symbols of this sort.
3677
3678 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
3679 is the name of the i'th symbol. TABLE_SIZE is the number of elements
3680 in the table.
3681
3682 Alternatively, NAME_ALIST is an alist mapping codes into symbol names.
3683 NAME_ALIST is used if it is non-nil; otherwise NAME_TABLE is used.
3684
3685 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
3686 persist between calls to modify_event_symbol that it can use to
3687 store a cache of the symbols it's generated for this NAME_TABLE
3688 before. The object stored there may be a vector or an alist.
3689
3690 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
3691
3692 MODIFIERS is a set of modifier bits (as given in struct input_events)
3693 whose prefixes should be applied to the symbol name.
3694
3695 SYMBOL_KIND is the value to be placed in the event_kind property of
3696 the returned symbol.
3697
3698 The symbols we create are supposed to have an
3699 `event-symbol-elements' property, which lists the modifiers present
3700 in the symbol's name. */
3701
3702 static Lisp_Object
3703 modify_event_symbol (symbol_num, modifiers, symbol_kind, name_alist,
3704 name_table, symbol_table, table_size)
3705 int symbol_num;
3706 unsigned modifiers;
3707 Lisp_Object symbol_kind;
3708 Lisp_Object name_alist;
3709 char **name_table;
3710 Lisp_Object *symbol_table;
3711 int table_size;
3712 {
3713 Lisp_Object value;
3714 Lisp_Object symbol_int;
3715
3716 XSETINT (symbol_int, symbol_num);
3717
3718 /* Is this a request for a valid symbol? */
3719 if (symbol_num < 0 || symbol_num >= table_size)
3720 return Qnil;
3721
3722 if (CONSP (*symbol_table))
3723 value = Fcdr (assq_no_quit (symbol_int, *symbol_table));
3724
3725 /* If *symbol_table doesn't seem to be initialized properly, fix that.
3726 *symbol_table should be a lisp vector TABLE_SIZE elements long,
3727 where the Nth element is the symbol for NAME_TABLE[N], or nil if
3728 we've never used that symbol before. */
3729 else
3730 {
3731 if (! VECTORP (*symbol_table)
3732 || XVECTOR (*symbol_table)->size != table_size)
3733 {
3734 Lisp_Object size;
3735
3736 XSETFASTINT (size, table_size);
3737 *symbol_table = Fmake_vector (size, Qnil);
3738 }
3739
3740 value = XVECTOR (*symbol_table)->contents[symbol_num];
3741 }
3742
3743 /* Have we already used this symbol before? */
3744 if (NILP (value))
3745 {
3746 /* No; let's create it. */
3747 if (!NILP (name_alist))
3748 value = Fcdr_safe (Fassq (symbol_int, name_alist));
3749 else if (name_table[symbol_num])
3750 value = intern (name_table[symbol_num]);
3751
3752 if (NILP (value))
3753 {
3754 char buf[20];
3755 sprintf (buf, "key-%d", symbol_num);
3756 value = intern (buf);
3757 }
3758
3759 if (CONSP (*symbol_table))
3760 *symbol_table = Fcons (value, *symbol_table);
3761 else
3762 XVECTOR (*symbol_table)->contents[symbol_num] = value;
3763
3764 /* Fill in the cache entries for this symbol; this also
3765 builds the Qevent_symbol_elements property, which the user
3766 cares about. */
3767 apply_modifiers (modifiers & click_modifier, value);
3768 Fput (value, Qevent_kind, symbol_kind);
3769 }
3770
3771 /* Apply modifiers to that symbol. */
3772 return apply_modifiers (modifiers, value);
3773 }
3774 \f
3775 /* Convert a list that represents an event type,
3776 such as (ctrl meta backspace), into the usual representation of that
3777 event type as a number or a symbol. */
3778
3779 Lisp_Object
3780 convert_event_type_list (event)
3781 Lisp_Object event;
3782 {
3783 Lisp_Object base;
3784 int modifiers = 0;
3785 Lisp_Object rest;
3786
3787 base = Qnil;
3788 rest = event;
3789 while (CONSP (rest))
3790 {
3791 Lisp_Object elt;
3792 int this = 0;
3793
3794 elt = XCONS (rest)->car;
3795
3796 /* Given a symbol, see if it is a modifier name. */
3797 if (SYMBOLP (elt))
3798 this = parse_solitary_modifier (elt);
3799
3800 if (this != 0)
3801 modifiers |= this;
3802 else if (!NILP (base))
3803 error ("Two bases given in one event");
3804 else
3805 base = elt;
3806
3807 rest = XCONS (rest)->cdr;
3808 }
3809
3810 /* Let the symbol A refer to the character A. */
3811 if (SYMBOLP (base) && XSYMBOL (base)->name->size == 1)
3812 XSETINT (base, XSYMBOL (base)->name->data[0]);
3813
3814 if (INTEGERP (base))
3815 {
3816 /* Turn (shift a) into A. */
3817 if ((modifiers & shift_modifier) != 0
3818 && (XINT (base) >= 'a' && XINT (base) <= 'z'))
3819 {
3820 XSETINT (base, XINT (base) - ('a' - 'A'));
3821 modifiers &= ~shift_modifier;
3822 }
3823
3824 /* Turn (control a) into C-a. */
3825 if (modifiers & ctrl_modifier)
3826 return make_number ((modifiers & ~ctrl_modifier)
3827 | make_ctrl_char (XINT (base)));
3828 else
3829 return make_number (modifiers | XINT (base));
3830 }
3831 else if (SYMBOLP (base))
3832 return apply_modifiers (modifiers, base);
3833 else
3834 error ("Invalid base event");
3835 }
3836
3837 /* Try to recognize SYMBOL as a modifier name.
3838 Return the modifier flag bit, or 0 if not recognized. */
3839
3840 static int
3841 parse_solitary_modifier (symbol)
3842 Lisp_Object symbol;
3843 {
3844 struct Lisp_String *name = XSYMBOL (symbol)->name;
3845
3846 switch (name->data[0])
3847 {
3848 #define SINGLE_LETTER_MOD(BIT) \
3849 if (name->size == 1) \
3850 return BIT;
3851
3852 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
3853 if (LEN == name->size \
3854 && ! strncmp (name->data, NAME, LEN)) \
3855 return BIT;
3856
3857 case 'A':
3858 SINGLE_LETTER_MOD (alt_modifier);
3859 break;
3860
3861 case 'a':
3862 MULTI_LETTER_MOD (alt_modifier, "alt", 3);
3863 break;
3864
3865 case 'C':
3866 SINGLE_LETTER_MOD (ctrl_modifier);
3867 break;
3868
3869 case 'c':
3870 MULTI_LETTER_MOD (ctrl_modifier, "ctrl", 4);
3871 MULTI_LETTER_MOD (ctrl_modifier, "control", 7);
3872 break;
3873
3874 case 'H':
3875 SINGLE_LETTER_MOD (hyper_modifier);
3876 break;
3877
3878 case 'h':
3879 MULTI_LETTER_MOD (hyper_modifier, "hyper", 5);
3880 break;
3881
3882 case 'M':
3883 SINGLE_LETTER_MOD (meta_modifier);
3884 break;
3885
3886 case 'm':
3887 MULTI_LETTER_MOD (meta_modifier, "meta", 4);
3888 break;
3889
3890 case 'S':
3891 SINGLE_LETTER_MOD (shift_modifier);
3892 break;
3893
3894 case 's':
3895 MULTI_LETTER_MOD (shift_modifier, "shift", 5);
3896 MULTI_LETTER_MOD (super_modifier, "super", 5);
3897 SINGLE_LETTER_MOD (super_modifier);
3898 break;
3899
3900 case 'd':
3901 MULTI_LETTER_MOD (drag_modifier, "drag", 4);
3902 MULTI_LETTER_MOD (down_modifier, "down", 4);
3903 MULTI_LETTER_MOD (double_modifier, "double", 6);
3904 break;
3905
3906 case 't':
3907 MULTI_LETTER_MOD (triple_modifier, "triple", 6);
3908 break;
3909
3910 #undef SINGLE_LETTER_MOD
3911 #undef MULTI_LETTER_MOD
3912 }
3913
3914 return 0;
3915 }
3916
3917 /* Return 1 if EVENT is a list whose elements are all integers or symbols.
3918 Such a list is not valid as an event,
3919 but it can be a Lucid-style event type list. */
3920
3921 int
3922 lucid_event_type_list_p (object)
3923 Lisp_Object object;
3924 {
3925 Lisp_Object tail;
3926
3927 if (! CONSP (object))
3928 return 0;
3929
3930 for (tail = object; CONSP (tail); tail = XCONS (tail)->cdr)
3931 {
3932 Lisp_Object elt;
3933 elt = XCONS (tail)->car;
3934 if (! (INTEGERP (elt) || SYMBOLP (elt)))
3935 return 0;
3936 }
3937
3938 return NILP (tail);
3939 }
3940 \f
3941 /* Store into *addr a value nonzero if terminal input chars are available.
3942 Serves the purpose of ioctl (0, FIONREAD, addr)
3943 but works even if FIONREAD does not exist.
3944 (In fact, this may actually read some input.) */
3945
3946 static void
3947 get_input_pending (addr)
3948 int *addr;
3949 {
3950 /* First of all, have we already counted some input? */
3951 *addr = !NILP (Vquit_flag) || readable_events ();
3952
3953 /* If input is being read as it arrives, and we have none, there is none. */
3954 if (*addr > 0 || (interrupt_input && ! interrupts_deferred))
3955 return;
3956
3957 /* Try to read some input and see how much we get. */
3958 gobble_input (0);
3959 *addr = !NILP (Vquit_flag) || readable_events ();
3960 }
3961
3962 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
3963
3964 int
3965 gobble_input (expected)
3966 int expected;
3967 {
3968 #ifndef VMS
3969 #ifdef SIGIO
3970 if (interrupt_input)
3971 {
3972 SIGMASKTYPE mask;
3973 mask = sigblockx (SIGIO);
3974 read_avail_input (expected);
3975 sigsetmask (mask);
3976 }
3977 else
3978 #ifdef POLL_FOR_INPUT
3979 if (read_socket_hook && !interrupt_input && poll_suppress_count == 0)
3980 {
3981 SIGMASKTYPE mask;
3982 mask = sigblockx (SIGALRM);
3983 read_avail_input (expected);
3984 sigsetmask (mask);
3985 }
3986 else
3987 #endif
3988 #endif
3989 read_avail_input (expected);
3990 #endif
3991 }
3992
3993 /* Put a buffer_switch_event in the buffer
3994 so that read_key_sequence will notice the new current buffer. */
3995
3996 record_asynch_buffer_change ()
3997 {
3998 struct input_event event;
3999 Lisp_Object tem;
4000
4001 event.kind = buffer_switch_event;
4002 event.frame_or_window = Qnil;
4003
4004 #ifdef subprocesses
4005 /* We don't need a buffer-switch event unless Emacs is waiting for input.
4006 The purpose of the event is to make read_key_sequence look up the
4007 keymaps again. If we aren't in read_key_sequence, we don't need one,
4008 and the event could cause trouble by messing up (input-pending-p). */
4009 tem = Fwaiting_for_user_input_p ();
4010 if (NILP (tem))
4011 return;
4012 #else
4013 /* We never need these events if we have no asynchronous subprocesses. */
4014 return;
4015 #endif
4016
4017 /* Make sure no interrupt happens while storing the event. */
4018 #ifdef SIGIO
4019 if (interrupt_input)
4020 {
4021 SIGMASKTYPE mask;
4022 mask = sigblockx (SIGIO);
4023 kbd_buffer_store_event (&event);
4024 sigsetmask (mask);
4025 }
4026 else
4027 #endif
4028 {
4029 stop_polling ();
4030 kbd_buffer_store_event (&event);
4031 start_polling ();
4032 }
4033 }
4034 \f
4035 #ifndef VMS
4036
4037 /* Read any terminal input already buffered up by the system
4038 into the kbd_buffer, but do not wait.
4039
4040 EXPECTED should be nonzero if the caller knows there is some input.
4041
4042 Except on VMS, all input is read by this function.
4043 If interrupt_input is nonzero, this function MUST be called
4044 only when SIGIO is blocked.
4045
4046 Returns the number of keyboard chars read, or -1 meaning
4047 this is a bad time to try to read input. */
4048
4049 static int
4050 read_avail_input (expected)
4051 int expected;
4052 {
4053 struct input_event buf[KBD_BUFFER_SIZE];
4054 register int i;
4055 int nread;
4056
4057 if (read_socket_hook)
4058 /* No need for FIONREAD or fcntl; just say don't wait. */
4059 nread = (*read_socket_hook) (input_fd, buf, KBD_BUFFER_SIZE,
4060 expected, expected);
4061 else
4062 {
4063 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
4064 the kbd_buffer can really hold. That may prevent loss
4065 of characters on some systems when input is stuffed at us. */
4066 unsigned char cbuf[KBD_BUFFER_SIZE - 1];
4067 int n_to_read;
4068
4069 /* Determine how many characters we should *try* to read. */
4070 #ifdef WINDOWSNT
4071 return 0;
4072 #else /* not WINDOWSNT */
4073 #ifdef MSDOS
4074 n_to_read = dos_keysns ();
4075 if (n_to_read == 0)
4076 return 0;
4077 #else /* not MSDOS */
4078 #ifdef FIONREAD
4079 /* Find out how much input is available. */
4080 if (ioctl (input_fd, FIONREAD, &n_to_read) < 0)
4081 /* Formerly simply reported no input, but that sometimes led to
4082 a failure of Emacs to terminate.
4083 SIGHUP seems appropriate if we can't reach the terminal. */
4084 /* ??? Is it really right to send the signal just to this process
4085 rather than to the whole process group?
4086 Perhaps on systems with FIONREAD Emacs is alone in its group. */
4087 kill (getpid (), SIGHUP);
4088 if (n_to_read == 0)
4089 return 0;
4090 if (n_to_read > sizeof cbuf)
4091 n_to_read = sizeof cbuf;
4092 #else /* no FIONREAD */
4093 #if defined(USG) || defined(DGUX)
4094 /* Read some input if available, but don't wait. */
4095 n_to_read = sizeof cbuf;
4096 fcntl (input_fd, F_SETFL, O_NDELAY);
4097 #else
4098 you lose;
4099 #endif
4100 #endif
4101 #endif /* not MSDOS */
4102 #endif /* not WINDOWSNT */
4103
4104 /* Now read; for one reason or another, this will not block.
4105 NREAD is set to the number of chars read. */
4106 do
4107 {
4108 #ifdef MSDOS
4109 cbuf[0] = dos_keyread();
4110 nread = 1;
4111 #else
4112 nread = read (input_fd, cbuf, n_to_read);
4113 #endif
4114 #if defined (AIX) && (! defined (aix386) && defined (_BSD))
4115 /* The kernel sometimes fails to deliver SIGHUP for ptys.
4116 This looks incorrect, but it isn't, because _BSD causes
4117 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
4118 and that causes a value other than 0 when there is no input. */
4119 if (nread == 0)
4120 kill (0, SIGHUP);
4121 #endif
4122 }
4123 while (
4124 /* We used to retry the read if it was interrupted.
4125 But this does the wrong thing when O_NDELAY causes
4126 an EAGAIN error. Does anybody know of a situation
4127 where a retry is actually needed? */
4128 #if 0
4129 nread < 0 && (errno == EAGAIN
4130 #ifdef EFAULT
4131 || errno == EFAULT
4132 #endif
4133 #ifdef EBADSLT
4134 || errno == EBADSLT
4135 #endif
4136 )
4137 #else
4138 0
4139 #endif
4140 );
4141
4142 #ifndef FIONREAD
4143 #if defined (USG) || defined (DGUX)
4144 fcntl (input_fd, F_SETFL, 0);
4145 #endif /* USG or DGUX */
4146 #endif /* no FIONREAD */
4147 for (i = 0; i < nread; i++)
4148 {
4149 buf[i].kind = ascii_keystroke;
4150 buf[i].modifiers = 0;
4151 if (meta_key == 1 && (cbuf[i] & 0x80))
4152 buf[i].modifiers = meta_modifier;
4153 if (meta_key != 2)
4154 cbuf[i] &= ~0x80;
4155
4156 buf[i].code = cbuf[i];
4157 #ifdef MULTI_FRAME
4158 XSETFRAME (buf[i].frame_or_window, selected_frame);
4159 #else
4160 buf[i].frame_or_window = Qnil;
4161 #endif
4162 }
4163 }
4164
4165 /* Scan the chars for C-g and store them in kbd_buffer. */
4166 for (i = 0; i < nread; i++)
4167 {
4168 kbd_buffer_store_event (&buf[i]);
4169 /* Don't look at input that follows a C-g too closely.
4170 This reduces lossage due to autorepeat on C-g. */
4171 if (buf[i].kind == ascii_keystroke
4172 && buf[i].code == quit_char)
4173 break;
4174 }
4175
4176 return nread;
4177 }
4178 #endif /* not VMS */
4179 \f
4180 #ifdef SIGIO /* for entire page */
4181 /* Note SIGIO has been undef'd if FIONREAD is missing. */
4182
4183 SIGTYPE
4184 input_available_signal (signo)
4185 int signo;
4186 {
4187 /* Must preserve main program's value of errno. */
4188 int old_errno = errno;
4189 #ifdef BSD4_1
4190 extern int select_alarmed;
4191 #endif
4192
4193 #ifdef USG
4194 /* USG systems forget handlers when they are used;
4195 must reestablish each time */
4196 signal (signo, input_available_signal);
4197 #endif /* USG */
4198
4199 #ifdef BSD4_1
4200 sigisheld (SIGIO);
4201 #endif
4202
4203 if (input_available_clear_time)
4204 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
4205
4206 while (1)
4207 {
4208 int nread;
4209 nread = read_avail_input (1);
4210 /* -1 means it's not ok to read the input now.
4211 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
4212 0 means there was no keyboard input available. */
4213 if (nread <= 0)
4214 break;
4215
4216 #ifdef BSD4_1
4217 select_alarmed = 1; /* Force the select emulator back to life */
4218 #endif
4219 }
4220
4221 #ifdef BSD4_1
4222 sigfree ();
4223 #endif
4224 errno = old_errno;
4225 }
4226 #endif /* SIGIO */
4227
4228 /* Send ourselves a SIGIO.
4229
4230 This function exists so that the UNBLOCK_INPUT macro in
4231 blockinput.h can have some way to take care of input we put off
4232 dealing with, without assuming that every file which uses
4233 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
4234 void
4235 reinvoke_input_signal ()
4236 {
4237 #ifdef SIGIO
4238 kill (0, SIGIO);
4239 #endif
4240 }
4241
4242
4243 \f
4244 /* Return the prompt-string of a sparse keymap.
4245 This is the first element which is a string.
4246 Return nil if there is none. */
4247
4248 Lisp_Object
4249 map_prompt (map)
4250 Lisp_Object map;
4251 {
4252 while (CONSP (map))
4253 {
4254 register Lisp_Object tem;
4255 tem = Fcar (map);
4256 if (STRINGP (tem))
4257 return tem;
4258 map = Fcdr (map);
4259 }
4260 return Qnil;
4261 }
4262
4263 static void menu_bar_item ();
4264 static void menu_bar_one_keymap ();
4265
4266 /* These variables hold the vector under construction within
4267 menu_bar_items and its subroutines, and the current index
4268 for storing into that vector. */
4269 static Lisp_Object menu_bar_items_vector;
4270 static int menu_bar_items_index;
4271
4272 /* Return a vector of menu items for a menu bar, appropriate
4273 to the current buffer. Each item has three elements in the vector:
4274 KEY STRING MAPLIST.
4275
4276 OLD is an old vector we can optionally reuse, or nil. */
4277
4278 Lisp_Object
4279 menu_bar_items (old)
4280 Lisp_Object old;
4281 {
4282 /* The number of keymaps we're scanning right now, and the number of
4283 keymaps we have allocated space for. */
4284 int nmaps;
4285
4286 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
4287 in the current keymaps, or nil where it is not a prefix. */
4288 Lisp_Object *maps;
4289
4290 Lisp_Object def, tem, tail;
4291
4292 Lisp_Object result;
4293
4294 int mapno;
4295 Lisp_Object oquit;
4296
4297 int i;
4298
4299 struct gcpro gcpro1;
4300
4301 /* In order to build the menus, we need to call the keymap
4302 accessors. They all call QUIT. But this function is called
4303 during redisplay, during which a quit is fatal. So inhibit
4304 quitting while building the menus.
4305 We do this instead of specbind because (1) errors will clear it anyway
4306 and (2) this avoids risk of specpdl overflow. */
4307 oquit = Vinhibit_quit;
4308 Vinhibit_quit = Qt;
4309
4310 if (!NILP (old))
4311 menu_bar_items_vector = old;
4312 else
4313 menu_bar_items_vector = Fmake_vector (make_number (24), Qnil);
4314 menu_bar_items_index = 0;
4315
4316 GCPRO1 (menu_bar_items_vector);
4317
4318 /* Build our list of keymaps.
4319 If we recognize a function key and replace its escape sequence in
4320 keybuf with its symbol, or if the sequence starts with a mouse
4321 click and we need to switch buffers, we jump back here to rebuild
4322 the initial keymaps from the current buffer. */
4323 {
4324 Lisp_Object *tmaps;
4325
4326 /* Should overriding-local-map apply, here? */
4327 if (!NILP (Voverriding_local_map_menu_flag))
4328 {
4329 if (NILP (Voverriding_local_map))
4330 {
4331 /* Yes, and it is nil. Use just global map. */
4332 nmaps = 1;
4333 maps = (Lisp_Object *) alloca (nmaps * sizeof (maps[0]));
4334 }
4335 else
4336 {
4337 /* Yes, and it is non-nil. Use it and the global map. */
4338 nmaps = 2;
4339 maps = (Lisp_Object *) alloca (nmaps * sizeof (maps[0]));
4340 maps[0] = Voverriding_local_map;
4341 }
4342 }
4343 else
4344 {
4345 /* No, so use major and minor mode keymaps. */
4346 nmaps = current_minor_maps (NULL, &tmaps) + 2;
4347 maps = (Lisp_Object *) alloca (nmaps * sizeof (maps[0]));
4348 bcopy (tmaps, maps, (nmaps - 2) * sizeof (maps[0]));
4349 #ifdef USE_TEXT_PROPERTIES
4350 maps[nmaps-2] = get_local_map (PT, current_buffer);
4351 #else
4352 maps[nmaps-2] = current_buffer->keymap;
4353 #endif
4354 }
4355 maps[nmaps-1] = current_global_map;
4356 }
4357
4358 /* Look up in each map the dummy prefix key `menu-bar'. */
4359
4360 result = Qnil;
4361
4362 for (mapno = nmaps - 1; mapno >= 0; mapno--)
4363 {
4364 if (! NILP (maps[mapno]))
4365 def = get_keyelt (access_keymap (maps[mapno], Qmenu_bar, 1, 0));
4366 else
4367 def = Qnil;
4368
4369 tem = Fkeymapp (def);
4370 if (!NILP (tem))
4371 menu_bar_one_keymap (def);
4372 }
4373
4374 /* Move to the end those items that should be at the end. */
4375
4376 for (tail = Vmenu_bar_final_items; CONSP (tail); tail = XCONS (tail)->cdr)
4377 {
4378 int i;
4379 int end = menu_bar_items_index;
4380
4381 for (i = 0; i < end; i += 3)
4382 if (EQ (XCONS (tail)->car, XVECTOR (menu_bar_items_vector)->contents[i]))
4383 {
4384 Lisp_Object tem0, tem1, tem2;
4385 /* Move the item at index I to the end,
4386 shifting all the others forward. */
4387 tem0 = XVECTOR (menu_bar_items_vector)->contents[i + 0];
4388 tem1 = XVECTOR (menu_bar_items_vector)->contents[i + 1];
4389 tem2 = XVECTOR (menu_bar_items_vector)->contents[i + 2];
4390 if (end > i + 3)
4391 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 3],
4392 &XVECTOR (menu_bar_items_vector)->contents[i],
4393 (end - i - 3) * sizeof (Lisp_Object));
4394 XVECTOR (menu_bar_items_vector)->contents[end - 3] = tem0;
4395 XVECTOR (menu_bar_items_vector)->contents[end - 2] = tem1;
4396 XVECTOR (menu_bar_items_vector)->contents[end - 1] = tem2;
4397 break;
4398 }
4399 }
4400
4401 /* Add nil, nil, nil at the end. */
4402 i = menu_bar_items_index;
4403 if (i + 3 > XVECTOR (menu_bar_items_vector)->size)
4404 {
4405 Lisp_Object tem;
4406 int newsize = 2 * i;
4407 tem = Fmake_vector (make_number (2 * i), Qnil);
4408 bcopy (XVECTOR (menu_bar_items_vector)->contents,
4409 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
4410 menu_bar_items_vector = tem;
4411 }
4412 /* Add this item. */
4413 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
4414 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
4415 XVECTOR (menu_bar_items_vector)->contents[i++] = Qnil;
4416 menu_bar_items_index = i;
4417
4418 Vinhibit_quit = oquit;
4419 UNGCPRO;
4420 return menu_bar_items_vector;
4421 }
4422 \f
4423 /* Scan one map KEYMAP, accumulating any menu items it defines
4424 in menu_bar_items_vector. */
4425
4426 static void
4427 menu_bar_one_keymap (keymap)
4428 Lisp_Object keymap;
4429 {
4430 Lisp_Object tail, item, key, binding, item_string, table;
4431
4432 /* Loop over all keymap entries that have menu strings. */
4433 for (tail = keymap; CONSP (tail); tail = XCONS (tail)->cdr)
4434 {
4435 item = XCONS (tail)->car;
4436 if (CONSP (item))
4437 {
4438 key = XCONS (item)->car;
4439 binding = XCONS (item)->cdr;
4440 if (CONSP (binding))
4441 {
4442 item_string = XCONS (binding)->car;
4443 if (STRINGP (item_string))
4444 menu_bar_item (key, item_string, Fcdr (binding));
4445 }
4446 else if (EQ (binding, Qundefined))
4447 menu_bar_item (key, Qnil, binding);
4448 }
4449 else if (VECTORP (item))
4450 {
4451 /* Loop over the char values represented in the vector. */
4452 int len = XVECTOR (item)->size;
4453 int c;
4454 for (c = 0; c < len; c++)
4455 {
4456 Lisp_Object character;
4457 XSETFASTINT (character, c);
4458 binding = XVECTOR (item)->contents[c];
4459 if (CONSP (binding))
4460 {
4461 item_string = XCONS (binding)->car;
4462 if (STRINGP (item_string))
4463 menu_bar_item (key, item_string, Fcdr (binding));
4464 }
4465 else if (EQ (binding, Qundefined))
4466 menu_bar_item (key, Qnil, binding);
4467 }
4468 }
4469 }
4470 }
4471
4472 /* This is used as the handler when calling internal_condition_case_1. */
4473
4474 static Lisp_Object
4475 menu_bar_item_1 (arg)
4476 Lisp_Object arg;
4477 {
4478 return Qnil;
4479 }
4480
4481 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
4482 If there's already an item for KEY, add this DEF to it. */
4483
4484 static void
4485 menu_bar_item (key, item_string, def)
4486 Lisp_Object key, item_string, def;
4487 {
4488 Lisp_Object tem;
4489 Lisp_Object enabled;
4490 int i;
4491
4492 if (EQ (def, Qundefined))
4493 {
4494 /* If a map has an explicit `undefined' as definition,
4495 discard any previously made menu bar item. */
4496
4497 for (i = 0; i < menu_bar_items_index; i += 3)
4498 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
4499 {
4500 if (menu_bar_items_index > i + 3)
4501 bcopy (&XVECTOR (menu_bar_items_vector)->contents[i + 3],
4502 &XVECTOR (menu_bar_items_vector)->contents[i],
4503 (menu_bar_items_index - i - 3) * sizeof (Lisp_Object));
4504 menu_bar_items_index -= 3;
4505 return;
4506 }
4507
4508 /* If there's no definition for this key yet,
4509 just ignore `undefined'. */
4510 return;
4511 }
4512
4513 /* See if this entry is enabled. */
4514 enabled = Qt;
4515
4516 if (SYMBOLP (def))
4517 {
4518 /* No property, or nil, means enable.
4519 Otherwise, enable if value is not nil. */
4520 tem = Fget (def, Qmenu_enable);
4521 if (!NILP (tem))
4522 /* (condition-case nil (eval tem)
4523 (error nil)) */
4524 enabled = internal_condition_case_1 (Feval, tem, Qerror,
4525 menu_bar_item_1);
4526 }
4527
4528 /* Ignore this item if it's not enabled. */
4529 if (NILP (enabled))
4530 return;
4531
4532 /* Find any existing item for this KEY. */
4533 for (i = 0; i < menu_bar_items_index; i += 3)
4534 if (EQ (key, XVECTOR (menu_bar_items_vector)->contents[i]))
4535 break;
4536
4537 /* If we did not find this KEY, add it at the end. */
4538 if (i == menu_bar_items_index)
4539 {
4540 /* If vector is too small, get a bigger one. */
4541 if (i + 3 > XVECTOR (menu_bar_items_vector)->size)
4542 {
4543 Lisp_Object tem;
4544 int newsize = 2 * i;
4545 tem = Fmake_vector (make_number (2 * i), Qnil);
4546 bcopy (XVECTOR (menu_bar_items_vector)->contents,
4547 XVECTOR (tem)->contents, i * sizeof (Lisp_Object));
4548 menu_bar_items_vector = tem;
4549 }
4550 /* Add this item. */
4551 XVECTOR (menu_bar_items_vector)->contents[i++] = key;
4552 XVECTOR (menu_bar_items_vector)->contents[i++] = item_string;
4553 XVECTOR (menu_bar_items_vector)->contents[i++] = Fcons (def, Qnil);
4554 menu_bar_items_index = i;
4555 }
4556 /* We did find an item for this KEY. Add DEF to its list of maps. */
4557 else
4558 {
4559 Lisp_Object old;
4560 old = XVECTOR (menu_bar_items_vector)->contents[i + 2];
4561 XVECTOR (menu_bar_items_vector)->contents[i + 2] = Fcons (def, old);
4562 }
4563 }
4564 \f
4565 /* Read a character using menus based on maps in the array MAPS.
4566 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
4567 Return t if we displayed a menu but the user rejected it.
4568
4569 PREV_EVENT is the previous input event, or nil if we are reading
4570 the first event of a key sequence.
4571
4572 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
4573 if we used a mouse menu to read the input, or zero otherwise. If
4574 USED_MOUSE_MENU is null, we don't dereference it.
4575
4576 The prompting is done based on the prompt-string of the map
4577 and the strings associated with various map elements.
4578
4579 This can be done with X menus or with menus put in the minibuf.
4580 These are done in different ways, depending on how the input will be read.
4581 Menus using X are done after auto-saving in read-char, getting the input
4582 event from Fx_popup_menu; menus using the minibuf use read_char recursively
4583 and do auto-saving in the inner call of read_char. */
4584
4585 static Lisp_Object
4586 read_char_x_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
4587 int nmaps;
4588 Lisp_Object *maps;
4589 Lisp_Object prev_event;
4590 int *used_mouse_menu;
4591 {
4592 int mapno;
4593 register Lisp_Object name;
4594 Lisp_Object rest, vector;
4595
4596 if (used_mouse_menu)
4597 *used_mouse_menu = 0;
4598
4599 /* Use local over global Menu maps */
4600
4601 if (! menu_prompting)
4602 return Qnil;
4603
4604 /* Optionally disregard all but the global map. */
4605 if (inhibit_local_menu_bar_menus)
4606 {
4607 maps += (nmaps - 1);
4608 nmaps = 1;
4609 }
4610
4611 /* Get the menu name from the first map that has one (a prompt string). */
4612 for (mapno = 0; mapno < nmaps; mapno++)
4613 {
4614 name = map_prompt (maps[mapno]);
4615 if (!NILP (name))
4616 break;
4617 }
4618
4619 /* If we don't have any menus, just read a character normally. */
4620 if (mapno >= nmaps)
4621 return Qnil;
4622
4623 #if (defined (HAVE_X_WINDOWS) && defined (HAVE_X_MENU)) || defined (MSDOS)
4624 /* If we got to this point via a mouse click,
4625 use a real menu for mouse selection. */
4626 if (EVENT_HAS_PARAMETERS (prev_event)
4627 && !EQ (XCONS (prev_event)->car, Qmenu_bar))
4628 {
4629 /* Display the menu and get the selection. */
4630 Lisp_Object *realmaps
4631 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
4632 Lisp_Object value;
4633 int nmaps1 = 0;
4634
4635 /* Use the maps that are not nil. */
4636 for (mapno = 0; mapno < nmaps; mapno++)
4637 if (!NILP (maps[mapno]))
4638 realmaps[nmaps1++] = maps[mapno];
4639
4640 value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
4641 if (CONSP (value))
4642 {
4643 /* If we got more than one event, put all but the first
4644 onto this list to be read later.
4645 Return just the first event now. */
4646 Vunread_command_events
4647 = nconc2 (XCONS (value)->cdr, Vunread_command_events);
4648 value = XCONS (value)->car;
4649 }
4650 else if (NILP (value))
4651 value = Qt;
4652 if (used_mouse_menu)
4653 *used_mouse_menu = 1;
4654 return value;
4655 }
4656 #endif /* (HAVE_X_WINDOWS && HAVE_X_MENU) || MSDOS */
4657 return Qnil ;
4658 }
4659
4660 static Lisp_Object
4661 read_char_minibuf_menu_prompt (commandflag, nmaps, maps)
4662 int commandflag ;
4663 int nmaps;
4664 Lisp_Object *maps;
4665 {
4666 int mapno;
4667 register Lisp_Object name;
4668 int nlength;
4669 int width = FRAME_WIDTH (selected_frame) - 4;
4670 char *menu = (char *) alloca (width + 4);
4671 int idx = -1;
4672 int nobindings = 1;
4673 Lisp_Object rest, vector;
4674
4675 if (! menu_prompting)
4676 return Qnil;
4677
4678 /* Get the menu name from the first map that has one (a prompt string). */
4679 for (mapno = 0; mapno < nmaps; mapno++)
4680 {
4681 name = map_prompt (maps[mapno]);
4682 if (!NILP (name))
4683 break;
4684 }
4685
4686 /* If we don't have any menus, just read a character normally. */
4687 if (mapno >= nmaps)
4688 return Qnil;
4689
4690 /* Prompt string always starts with map's prompt, and a space. */
4691 strcpy (menu, XSTRING (name)->data);
4692 nlength = XSTRING (name)->size;
4693 menu[nlength++] = ':';
4694 menu[nlength++] = ' ';
4695 menu[nlength] = 0;
4696
4697 /* Start prompting at start of first map. */
4698 mapno = 0;
4699 rest = maps[mapno];
4700
4701 /* Present the documented bindings, a line at a time. */
4702 while (1)
4703 {
4704 int notfirst = 0;
4705 int i = nlength;
4706 Lisp_Object obj;
4707 int ch;
4708 Lisp_Object orig_defn_macro;
4709
4710 /* Loop over elements of map. */
4711 while (i < width)
4712 {
4713 Lisp_Object s, elt;
4714
4715 /* If reached end of map, start at beginning of next map. */
4716 if (NILP (rest))
4717 {
4718 mapno++;
4719 /* At end of last map, wrap around to first map if just starting,
4720 or end this line if already have something on it. */
4721 if (mapno == nmaps)
4722 {
4723 mapno = 0;
4724 if (notfirst || nobindings) break;
4725 }
4726 rest = maps[mapno];
4727 }
4728
4729 /* Look at the next element of the map. */
4730 if (idx >= 0)
4731 elt = XVECTOR (vector)->contents[idx];
4732 else
4733 elt = Fcar_safe (rest);
4734
4735 if (idx < 0 && VECTORP (elt))
4736 {
4737 /* If we found a dense table in the keymap,
4738 advanced past it, but start scanning its contents. */
4739 rest = Fcdr_safe (rest);
4740 vector = elt;
4741 idx = 0;
4742 }
4743 else
4744 {
4745 /* An ordinary element. */
4746 if ( idx < 0 )
4747 s = Fcar_safe (Fcdr_safe (elt)); /* alist */
4748 else
4749 s = Fcar_safe(elt); /* vector */
4750 if (!STRINGP (s))
4751 /* Ignore the element if it has no prompt string. */
4752 ;
4753 /* If we have room for the prompt string, add it to this line.
4754 If this is the first on the line, always add it. */
4755 else if (XSTRING (s)->size + i + 2 < width
4756 || !notfirst)
4757 {
4758 int thiswidth;
4759
4760 /* Punctuate between strings. */
4761 if (notfirst)
4762 {
4763 strcpy (menu + i, ", ");
4764 i += 2;
4765 }
4766 notfirst = 1;
4767 nobindings = 0 ;
4768
4769 /* Add as much of string as fits. */
4770 thiswidth = XSTRING (s)->size;
4771 if (thiswidth + i > width)
4772 thiswidth = width - i;
4773 bcopy (XSTRING (s)->data, menu + i, thiswidth);
4774 i += thiswidth;
4775 menu[i] = 0;
4776 }
4777 else
4778 {
4779 /* If this element does not fit, end the line now,
4780 and save the element for the next line. */
4781 strcpy (menu + i, "...");
4782 break;
4783 }
4784
4785 /* Move past this element. */
4786 if (idx >= 0 && idx + 1 >= XVECTOR (vector)->size)
4787 /* Handle reaching end of dense table. */
4788 idx = -1;
4789 if (idx >= 0)
4790 idx++;
4791 else
4792 rest = Fcdr_safe (rest);
4793 }
4794 }
4795
4796 /* Prompt with that and read response. */
4797 message1 (menu);
4798
4799 /* Make believe its not a keyboard macro in case the help char
4800 is pressed. Help characters are not recorded because menu prompting
4801 is not used on replay.
4802 */
4803 orig_defn_macro = current_kboard->defining_kbd_macro;
4804 current_kboard->defining_kbd_macro = Qnil;
4805 do
4806 obj = read_char (commandflag, 0, 0, Qnil, 0);
4807 while (BUFFERP (obj));
4808 current_kboard->defining_kbd_macro = orig_defn_macro;
4809
4810 if (!INTEGERP (obj))
4811 return obj;
4812 else
4813 ch = XINT (obj);
4814
4815 if (! EQ (obj, menu_prompt_more_char)
4816 && (!INTEGERP (menu_prompt_more_char)
4817 || ! EQ (obj, make_number (Ctl (XINT (menu_prompt_more_char))))))
4818 {
4819 if (!NILP (current_kboard->defining_kbd_macro))
4820 store_kbd_macro_char (obj);
4821 return obj;
4822 }
4823 /* Help char - go round again */
4824 }
4825 }
4826 \f
4827 /* Reading key sequences. */
4828
4829 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
4830 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
4831 keymap, or nil otherwise. Return the index of the first keymap in
4832 which KEY has any binding, or NMAPS if no map has a binding.
4833
4834 If KEY is a meta ASCII character, treat it like meta-prefix-char
4835 followed by the corresponding non-meta character. Keymaps in
4836 CURRENT with non-prefix bindings for meta-prefix-char become nil in
4837 NEXT.
4838
4839 If KEY has no bindings in any of the CURRENT maps, NEXT is left
4840 unmodified.
4841
4842 NEXT may == CURRENT. */
4843
4844 static int
4845 follow_key (key, nmaps, current, defs, next)
4846 Lisp_Object key;
4847 Lisp_Object *current, *defs, *next;
4848 int nmaps;
4849 {
4850 int i, first_binding;
4851
4852 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
4853 followed by the corresponding non-meta character. */
4854 if (INTEGERP (key) && (XINT (key) & CHAR_META))
4855 {
4856 for (i = 0; i < nmaps; i++)
4857 if (! NILP (current[i]))
4858 {
4859 next[i] =
4860 get_keyelt (access_keymap (current[i], meta_prefix_char, 1, 0));
4861
4862 /* Note that since we pass the resulting bindings through
4863 get_keymap_1, non-prefix bindings for meta-prefix-char
4864 disappear. */
4865 next[i] = get_keymap_1 (next[i], 0, 1);
4866 }
4867 else
4868 next[i] = Qnil;
4869
4870 current = next;
4871 XSETINT (key, XFASTINT (key) & ~CHAR_META);
4872 }
4873
4874 first_binding = nmaps;
4875 for (i = nmaps - 1; i >= 0; i--)
4876 {
4877 if (! NILP (current[i]))
4878 {
4879 defs[i] = get_keyelt (access_keymap (current[i], key, 1, 0));
4880 if (! NILP (defs[i]))
4881 first_binding = i;
4882 }
4883 else
4884 defs[i] = Qnil;
4885 }
4886
4887 /* Given the set of bindings we've found, produce the next set of maps. */
4888 if (first_binding < nmaps)
4889 for (i = 0; i < nmaps; i++)
4890 next[i] = NILP (defs[i]) ? Qnil : get_keymap_1 (defs[i], 0, 1);
4891
4892 return first_binding;
4893 }
4894
4895 /* Read a sequence of keys that ends with a non prefix character,
4896 storing it in KEYBUF, a buffer of size BUFSIZE.
4897 Prompt with PROMPT.
4898 Return the length of the key sequence stored.
4899 Return -1 if the user rejected a command menu.
4900
4901 Echo starting immediately unless `prompt' is 0.
4902
4903 Where a key sequence ends depends on the currently active keymaps.
4904 These include any minor mode keymaps active in the current buffer,
4905 the current buffer's local map, and the global map.
4906
4907 If a key sequence has no other bindings, we check Vfunction_key_map
4908 to see if some trailing subsequence might be the beginning of a
4909 function key's sequence. If so, we try to read the whole function
4910 key, and substitute its symbolic name into the key sequence.
4911
4912 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
4913 `double-' events into similar click events, if that would make them
4914 bound. We try to turn `triple-' events first into `double-' events,
4915 then into clicks.
4916
4917 If we get a mouse click in a mode line, vertical divider, or other
4918 non-text area, we treat the click as if it were prefixed by the
4919 symbol denoting that area - `mode-line', `vertical-line', or
4920 whatever.
4921
4922 If the sequence starts with a mouse click, we read the key sequence
4923 with respect to the buffer clicked on, not the current buffer.
4924
4925 If the user switches frames in the midst of a key sequence, we put
4926 off the switch-frame event until later; the next call to
4927 read_char will return it. */
4928
4929 static int
4930 read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
4931 can_return_switch_frame)
4932 Lisp_Object *keybuf;
4933 int bufsize;
4934 Lisp_Object prompt;
4935 int dont_downcase_last;
4936 int can_return_switch_frame;
4937 {
4938 int count = specpdl_ptr - specpdl;
4939
4940 /* How many keys there are in the current key sequence. */
4941 int t;
4942
4943 /* The length of the echo buffer when we started reading, and
4944 the length of this_command_keys when we started reading. */
4945 int echo_start;
4946 int keys_start;
4947
4948 /* The number of keymaps we're scanning right now, and the number of
4949 keymaps we have allocated space for. */
4950 int nmaps;
4951 int nmaps_allocated = 0;
4952
4953 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
4954 the current keymaps. */
4955 Lisp_Object *defs;
4956
4957 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
4958 in the current keymaps, or nil where it is not a prefix. */
4959 Lisp_Object *submaps;
4960
4961 /* The local map to start out with at start of key sequence. */
4962 Lisp_Object orig_local_map;
4963
4964 /* 1 if we have already considered switching to the local-map property
4965 of the place where a mouse click occurred. */
4966 int localized_local_map = 0;
4967
4968 /* The index in defs[] of the first keymap that has a binding for
4969 this key sequence. In other words, the lowest i such that
4970 defs[i] is non-nil. */
4971 int first_binding;
4972
4973 /* If t < mock_input, then KEYBUF[t] should be read as the next
4974 input key.
4975
4976 We use this to recover after recognizing a function key. Once we
4977 realize that a suffix of the current key sequence is actually a
4978 function key's escape sequence, we replace the suffix with the
4979 function key's binding from Vfunction_key_map. Now keybuf
4980 contains a new and different key sequence, so the echo area,
4981 this_command_keys, and the submaps and defs arrays are wrong. In
4982 this situation, we set mock_input to t, set t to 0, and jump to
4983 restart_sequence; the loop will read keys from keybuf up until
4984 mock_input, thus rebuilding the state; and then it will resume
4985 reading characters from the keyboard. */
4986 int mock_input = 0;
4987
4988 /* If the sequence is unbound in submaps[], then
4989 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
4990 and fkey_map is its binding.
4991
4992 These might be > t, indicating that all function key scanning
4993 should hold off until t reaches them. We do this when we've just
4994 recognized a function key, to avoid searching for the function
4995 key's again in Vfunction_key_map. */
4996 int fkey_start = 0, fkey_end = 0;
4997 Lisp_Object fkey_map;
4998
4999 /* Likewise, for key_translation_map. */
5000 int keytran_start = 0, keytran_end = 0;
5001 Lisp_Object keytran_map;
5002
5003 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
5004 we put it off for later. While we're reading, we keep the event here. */
5005 Lisp_Object delayed_switch_frame;
5006
5007 /* See the comment below... */
5008 #if defined (GOBBLE_FIRST_EVENT)
5009 Lisp_Object first_event;
5010 #endif
5011
5012 Lisp_Object original_uppercase;
5013 int original_uppercase_position = -1;
5014
5015 /* Gets around Microsoft compiler limitations. */
5016 int dummyflag = 0;
5017
5018 struct buffer *starting_buffer;
5019
5020 /* Nonzero if we seem to have got the beginning of a binding
5021 in function_key_map. */
5022 int function_key_possible = 0;
5023 int key_translation_possible = 0;
5024
5025 int junk;
5026
5027 last_nonmenu_event = Qnil;
5028
5029 delayed_switch_frame = Qnil;
5030 fkey_map = Vfunction_key_map;
5031 keytran_map = Vkey_translation_map;
5032
5033 /* If there is no function-key-map, turn off function key scanning. */
5034 if (NILP (Fkeymapp (Vfunction_key_map)))
5035 fkey_start = fkey_end = bufsize + 1;
5036
5037 /* If there is no key-translation-map, turn off scanning. */
5038 if (NILP (Fkeymapp (Vkey_translation_map)))
5039 keytran_start = keytran_end = bufsize + 1;
5040
5041 if (INTERACTIVE)
5042 {
5043 if (!NILP (prompt))
5044 echo_prompt (XSTRING (prompt)->data);
5045 else if (cursor_in_echo_area && echo_keystrokes)
5046 /* This doesn't put in a dash if the echo buffer is empty, so
5047 you don't always see a dash hanging out in the minibuffer. */
5048 echo_dash ();
5049 }
5050
5051 /* Record the initial state of the echo area and this_command_keys;
5052 we will need to restore them if we replay a key sequence. */
5053 if (INTERACTIVE)
5054 echo_start = echo_length ();
5055 keys_start = this_command_key_count;
5056
5057 #if defined (GOBBLE_FIRST_EVENT)
5058 /* This doesn't quite work, because some of the things that read_char
5059 does cannot safely be bypassed. It seems too risky to try to make
5060 this work right. */
5061
5062 /* Read the first char of the sequence specially, before setting
5063 up any keymaps, in case a filter runs and switches buffers on us. */
5064 first_event = read_char (NILP (prompt), 0, submaps, last_nonmenu_event,
5065 &junk);
5066 #endif /* GOBBLE_FIRST_EVENT */
5067
5068 orig_local_map = get_local_map (PT, current_buffer);
5069
5070 /* We jump here when the key sequence has been thoroughly changed, and
5071 we need to rescan it starting from the beginning. When we jump here,
5072 keybuf[0..mock_input] holds the sequence we should reread. */
5073 replay_sequence:
5074
5075 starting_buffer = current_buffer;
5076 function_key_possible = 0;
5077 key_translation_possible = 0;
5078
5079 /* Build our list of keymaps.
5080 If we recognize a function key and replace its escape sequence in
5081 keybuf with its symbol, or if the sequence starts with a mouse
5082 click and we need to switch buffers, we jump back here to rebuild
5083 the initial keymaps from the current buffer. */
5084 {
5085 Lisp_Object *maps;
5086
5087 if (!NILP (Voverriding_local_map))
5088 {
5089 nmaps = 2;
5090 if (nmaps > nmaps_allocated)
5091 {
5092 submaps = (Lisp_Object *) alloca (nmaps * sizeof (submaps[0]));
5093 defs = (Lisp_Object *) alloca (nmaps * sizeof (defs[0]));
5094 nmaps_allocated = nmaps;
5095 }
5096 submaps[0] = Voverriding_local_map;
5097 }
5098 else
5099 {
5100 nmaps = current_minor_maps (0, &maps) + 2;
5101 if (nmaps > nmaps_allocated)
5102 {
5103 submaps = (Lisp_Object *) alloca (nmaps * sizeof (submaps[0]));
5104 defs = (Lisp_Object *) alloca (nmaps * sizeof (defs[0]));
5105 nmaps_allocated = nmaps;
5106 }
5107 bcopy (maps, submaps, (nmaps - 2) * sizeof (submaps[0]));
5108 #ifdef USE_TEXT_PROPERTIES
5109 submaps[nmaps-2] = orig_local_map;
5110 #else
5111 submaps[nmaps-2] = current_buffer->keymap;
5112 #endif
5113 }
5114 submaps[nmaps-1] = current_global_map;
5115 }
5116
5117 /* Find an accurate initial value for first_binding. */
5118 for (first_binding = 0; first_binding < nmaps; first_binding++)
5119 if (! NILP (submaps[first_binding]))
5120 break;
5121
5122 /* Start from the beginning in keybuf. */
5123 t = 0;
5124
5125 /* These are no-ops the first time through, but if we restart, they
5126 revert the echo area and this_command_keys to their original state. */
5127 this_command_key_count = keys_start;
5128 if (INTERACTIVE && t < mock_input)
5129 echo_truncate (echo_start);
5130
5131 /* If the best binding for the current key sequence is a keymap, or
5132 we may be looking at a function key's escape sequence, keep on
5133 reading. */
5134 while ((first_binding < nmaps && ! NILP (submaps[first_binding]))
5135 || (first_binding >= nmaps
5136 && fkey_start < t
5137 /* mock input is never part of a function key's sequence. */
5138 && mock_input <= fkey_start)
5139 || (first_binding >= nmaps
5140 && keytran_start < t && key_translation_possible)
5141 /* Don't return in the middle of a possible function key sequence,
5142 if the only bindings we found were via case conversion.
5143 Thus, if ESC O a has a function-key-map translation
5144 and ESC o has a binding, don't return after ESC O,
5145 so that we can translate ESC O plus the next character. */
5146 )
5147 {
5148 Lisp_Object key;
5149 int used_mouse_menu = 0;
5150
5151 /* Where the last real key started. If we need to throw away a
5152 key that has expanded into more than one element of keybuf
5153 (say, a mouse click on the mode line which is being treated
5154 as [mode-line (mouse-...)], then we backtrack to this point
5155 of keybuf. */
5156 int last_real_key_start;
5157
5158 /* These variables are analogous to echo_start and keys_start;
5159 while those allow us to restart the entire key sequence,
5160 echo_local_start and keys_local_start allow us to throw away
5161 just one key. */
5162 int echo_local_start, keys_local_start, local_first_binding;
5163
5164 if (t >= bufsize)
5165 error ("key sequence too long");
5166
5167 if (INTERACTIVE)
5168 echo_local_start = echo_length ();
5169 keys_local_start = this_command_key_count;
5170 local_first_binding = first_binding;
5171
5172 replay_key:
5173 /* These are no-ops, unless we throw away a keystroke below and
5174 jumped back up to replay_key; in that case, these restore the
5175 variables to their original state, allowing us to replay the
5176 loop. */
5177 if (INTERACTIVE && t < mock_input)
5178 echo_truncate (echo_local_start);
5179 this_command_key_count = keys_local_start;
5180 first_binding = local_first_binding;
5181
5182 /* By default, assume each event is "real". */
5183 last_real_key_start = t;
5184
5185 /* Does mock_input indicate that we are re-reading a key sequence? */
5186 if (t < mock_input)
5187 {
5188 key = keybuf[t];
5189 add_command_key (key);
5190 if (echo_keystrokes)
5191 echo_char (key);
5192 }
5193
5194 /* If not, we should actually read a character. */
5195 else
5196 {
5197 struct buffer *buf = current_buffer;
5198
5199 {
5200 #ifdef MULTI_KBOARD
5201 KBOARD *interrupted_kboard = current_kboard;
5202 struct frame *interrupted_frame = selected_frame;
5203 if (setjmp (wrong_kboard_jmpbuf))
5204 {
5205 if (!NILP (delayed_switch_frame))
5206 {
5207 interrupted_kboard->kbd_queue
5208 = Fcons (delayed_switch_frame,
5209 interrupted_kboard->kbd_queue);
5210 delayed_switch_frame = Qnil;
5211 }
5212 while (t > 0)
5213 interrupted_kboard->kbd_queue
5214 = Fcons (keybuf[--t], interrupted_kboard->kbd_queue);
5215
5216 /* If the side queue is non-empty, ensure it begins with a
5217 switch-frame, so we'll replay it in the right context. */
5218 if (CONSP (interrupted_kboard->kbd_queue)
5219 && (key = XCONS (interrupted_kboard->kbd_queue)->car,
5220 !(EVENT_HAS_PARAMETERS (key)
5221 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key)),
5222 Qswitch_frame))))
5223 {
5224 Lisp_Object frame;
5225 XSETFRAME (frame, interrupted_frame);
5226 interrupted_kboard->kbd_queue
5227 = Fcons (make_lispy_switch_frame (frame),
5228 interrupted_kboard->kbd_queue);
5229 }
5230 mock_input = 0;
5231 orig_local_map = get_local_map (PT, current_buffer);
5232 goto replay_sequence;
5233 }
5234 #endif
5235 key = read_char (NILP (prompt), nmaps, submaps, last_nonmenu_event,
5236 &used_mouse_menu);
5237 }
5238
5239 /* read_char returns t when it shows a menu and the user rejects it.
5240 Just return -1. */
5241 if (EQ (key, Qt))
5242 return -1;
5243
5244 /* read_char returns -1 at the end of a macro.
5245 Emacs 18 handles this by returning immediately with a
5246 zero, so that's what we'll do. */
5247 if (INTEGERP (key) && XINT (key) == -1)
5248 {
5249 t = 0;
5250 /* The Microsoft C compiler can't handle the goto that
5251 would go here. */
5252 dummyflag = 1;
5253 break;
5254 }
5255
5256 /* If the current buffer has been changed from under us, the
5257 keymap may have changed, so replay the sequence. */
5258 if (BUFFERP (key))
5259 {
5260 mock_input = t;
5261 orig_local_map = get_local_map (PT, current_buffer);
5262 goto replay_sequence;
5263 }
5264
5265 /* If we have a quit that was typed in another frame, and
5266 quit_throw_to_read_char switched buffers,
5267 replay to get the right keymap. */
5268 if (XINT (key) == quit_char && current_buffer != starting_buffer)
5269 {
5270 keybuf[t++] = key;
5271 mock_input = t;
5272 Vquit_flag = Qnil;
5273 orig_local_map = get_local_map (PT, current_buffer);
5274 goto replay_sequence;
5275 }
5276
5277 Vquit_flag = Qnil;
5278 }
5279
5280 /* Clicks in non-text areas get prefixed by the symbol
5281 in their CHAR-ADDRESS field. For example, a click on
5282 the mode line is prefixed by the symbol `mode-line'.
5283
5284 Furthermore, key sequences beginning with mouse clicks
5285 are read using the keymaps of the buffer clicked on, not
5286 the current buffer. So we may have to switch the buffer
5287 here.
5288
5289 When we turn one event into two events, we must make sure
5290 that neither of the two looks like the original--so that,
5291 if we replay the events, they won't be expanded again.
5292 If not for this, such reexpansion could happen either here
5293 or when user programs play with this-command-keys. */
5294 if (EVENT_HAS_PARAMETERS (key))
5295 {
5296 Lisp_Object kind;
5297
5298 kind = EVENT_HEAD_KIND (EVENT_HEAD (key));
5299 if (EQ (kind, Qmouse_click))
5300 {
5301 Lisp_Object window, posn;
5302
5303 window = POSN_WINDOW (EVENT_START (key));
5304 posn = POSN_BUFFER_POSN (EVENT_START (key));
5305 if (CONSP (posn))
5306 {
5307 /* We're looking at the second event of a
5308 sequence which we expanded before. Set
5309 last_real_key_start appropriately. */
5310 if (t > 0)
5311 last_real_key_start = t - 1;
5312 }
5313
5314 /* Key sequences beginning with mouse clicks are
5315 read using the keymaps in the buffer clicked on,
5316 not the current buffer. If we're at the
5317 beginning of a key sequence, switch buffers. */
5318 if (last_real_key_start == 0
5319 && WINDOWP (window)
5320 && BUFFERP (XWINDOW (window)->buffer)
5321 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
5322 {
5323 keybuf[t] = key;
5324 mock_input = t + 1;
5325
5326 /* Arrange to go back to the original buffer once we're
5327 done reading the key sequence. Note that we can't
5328 use save_excursion_{save,restore} here, because they
5329 save point as well as the current buffer; we don't
5330 want to save point, because redisplay may change it,
5331 to accommodate a Fset_window_start or something. We
5332 don't want to do this at the top of the function,
5333 because we may get input from a subprocess which
5334 wants to change the selected window and stuff (say,
5335 emacsclient). */
5336 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
5337
5338 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
5339 orig_local_map = get_local_map (PT, current_buffer);
5340 goto replay_sequence;
5341 }
5342 /* For a mouse click, get the local text-property keymap
5343 of the place clicked on, rather than point. */
5344 if (last_real_key_start == 0 && CONSP (XCONS (key)->cdr)
5345 && ! localized_local_map)
5346 {
5347 Lisp_Object map_here, start, pos;
5348
5349 localized_local_map = 1;
5350 start = EVENT_START (key);
5351 if (CONSP (start) && CONSP (XCONS (start)->cdr))
5352 {
5353 pos = POSN_BUFFER_POSN (start);
5354 if (INTEGERP (pos))
5355 {
5356 map_here = get_local_map (XINT (pos), current_buffer);
5357 if (!EQ (map_here, orig_local_map))
5358 {
5359 orig_local_map = map_here;
5360 keybuf[t] = key;
5361 mock_input = t + 1;
5362
5363 goto replay_sequence;
5364 }
5365 }
5366 }
5367 }
5368
5369 /* Expand mode-line and scroll-bar events into two events:
5370 use posn as a fake prefix key. */
5371 if (SYMBOLP (posn))
5372 {
5373 if (t + 1 >= bufsize)
5374 error ("key sequence too long");
5375 keybuf[t] = posn;
5376 keybuf[t+1] = key;
5377 mock_input = t + 2;
5378
5379 /* Zap the position in key, so we know that we've
5380 expanded it, and don't try to do so again. */
5381 POSN_BUFFER_POSN (EVENT_START (key))
5382 = Fcons (posn, Qnil);
5383 goto replay_key;
5384 }
5385 }
5386 else if (EQ (kind, Qswitch_frame))
5387 {
5388 /* If we're at the beginning of a key sequence, and the caller
5389 says it's okay, go ahead and return this event. If we're
5390 in the midst of a key sequence, delay it until the end. */
5391 if (t > 0 || !can_return_switch_frame)
5392 {
5393 delayed_switch_frame = key;
5394 goto replay_key;
5395 }
5396 }
5397 else if (CONSP (XCONS (key)->cdr)
5398 && CONSP (EVENT_START (key))
5399 && CONSP (XCONS (EVENT_START (key))->cdr))
5400 {
5401 Lisp_Object posn;
5402
5403 posn = POSN_BUFFER_POSN (EVENT_START (key));
5404 /* Handle menu-bar events:
5405 insert the dummy prefix event `menu-bar'. */
5406 if (EQ (posn, Qmenu_bar))
5407 {
5408 if (t + 1 >= bufsize)
5409 error ("key sequence too long");
5410 /* Run the Lucid hook. */
5411 if (!NILP (Vrun_hooks))
5412 call1 (Vrun_hooks, Qactivate_menubar_hook);
5413 /* If it has changed current-menubar from previous value,
5414 really recompute the menubar from the value. */
5415 if (! NILP (Vlucid_menu_bar_dirty_flag))
5416 call0 (Qrecompute_lucid_menubar);
5417 keybuf[t] = posn;
5418 keybuf[t+1] = key;
5419
5420 /* Zap the position in key, so we know that we've
5421 expanded it, and don't try to do so again. */
5422 POSN_BUFFER_POSN (EVENT_START (key))
5423 = Fcons (posn, Qnil);
5424
5425 mock_input = t + 2;
5426 goto replay_sequence;
5427 }
5428 else if (CONSP (posn))
5429 {
5430 /* We're looking at the second event of a
5431 sequence which we expanded before. Set
5432 last_real_key_start appropriately. */
5433 if (last_real_key_start == t && t > 0)
5434 last_real_key_start = t - 1;
5435 }
5436 }
5437 }
5438
5439 /* We have finally decided that KEY is something we might want
5440 to look up. */
5441 first_binding = (follow_key (key,
5442 nmaps - first_binding,
5443 submaps + first_binding,
5444 defs + first_binding,
5445 submaps + first_binding)
5446 + first_binding);
5447
5448 /* If KEY wasn't bound, we'll try some fallbacks. */
5449 if (first_binding >= nmaps)
5450 {
5451 Lisp_Object head;
5452
5453 head = EVENT_HEAD (key);
5454 if (EQ (head, Vhelp_char))
5455 {
5456 read_key_sequence_cmd = Vprefix_help_command;
5457 keybuf[t++] = key;
5458 last_nonmenu_event = key;
5459 /* The Microsoft C compiler can't handle the goto that
5460 would go here. */
5461 dummyflag = 1;
5462 break;
5463 }
5464
5465 if (SYMBOLP (head))
5466 {
5467 Lisp_Object breakdown;
5468 int modifiers;
5469
5470 breakdown = parse_modifiers (head);
5471 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
5472 /* Attempt to reduce an unbound mouse event to a simpler
5473 event that is bound:
5474 Drags reduce to clicks.
5475 Double-clicks reduce to clicks.
5476 Triple-clicks reduce to double-clicks, then to clicks.
5477 Down-clicks are eliminated.
5478 Double-downs reduce to downs, then are eliminated.
5479 Triple-downs reduce to double-downs, then to downs,
5480 then are eliminated. */
5481 if (modifiers & (down_modifier | drag_modifier
5482 | double_modifier | triple_modifier))
5483 {
5484 while (modifiers & (down_modifier | drag_modifier
5485 | double_modifier | triple_modifier))
5486 {
5487 Lisp_Object new_head, new_click;
5488 if (modifiers & triple_modifier)
5489 modifiers ^= (double_modifier | triple_modifier);
5490 else if (modifiers & double_modifier)
5491 modifiers &= ~double_modifier;
5492 else if (modifiers & drag_modifier)
5493 modifiers &= ~drag_modifier;
5494 else
5495 {
5496 /* Dispose of this `down' event by simply jumping
5497 back to replay_key, to get another event.
5498
5499 Note that if this event came from mock input,
5500 then just jumping back to replay_key will just
5501 hand it to us again. So we have to wipe out any
5502 mock input.
5503
5504 We could delete keybuf[t] and shift everything
5505 after that to the left by one spot, but we'd also
5506 have to fix up any variable that points into
5507 keybuf, and shifting isn't really necessary
5508 anyway.
5509
5510 Adding prefixes for non-textual mouse clicks
5511 creates two characters of mock input, and both
5512 must be thrown away. If we're only looking at
5513 the prefix now, we can just jump back to
5514 replay_key. On the other hand, if we've already
5515 processed the prefix, and now the actual click
5516 itself is giving us trouble, then we've lost the
5517 state of the keymaps we want to backtrack to, and
5518 we need to replay the whole sequence to rebuild
5519 it.
5520
5521 Beyond that, only function key expansion could
5522 create more than two keys, but that should never
5523 generate mouse events, so it's okay to zero
5524 mock_input in that case too.
5525
5526 Isn't this just the most wonderful code ever? */
5527 if (t == last_real_key_start)
5528 {
5529 mock_input = 0;
5530 goto replay_key;
5531 }
5532 else
5533 {
5534 mock_input = last_real_key_start;
5535 goto replay_sequence;
5536 }
5537 }
5538
5539 new_head
5540 = apply_modifiers (modifiers, XCONS (breakdown)->car);
5541 new_click
5542 = Fcons (new_head, Fcons (EVENT_START (key), Qnil));
5543
5544 /* Look for a binding for this new key. follow_key
5545 promises that it didn't munge submaps the
5546 last time we called it, since key was unbound. */
5547 first_binding
5548 = (follow_key (new_click,
5549 nmaps - local_first_binding,
5550 submaps + local_first_binding,
5551 defs + local_first_binding,
5552 submaps + local_first_binding)
5553 + local_first_binding);
5554
5555 /* If that click is bound, go for it. */
5556 if (first_binding < nmaps)
5557 {
5558 key = new_click;
5559 break;
5560 }
5561 /* Otherwise, we'll leave key set to the drag event. */
5562 }
5563 }
5564 }
5565 }
5566
5567 keybuf[t++] = key;
5568 /* Normally, last_nonmenu_event gets the previous key we read.
5569 But when a mouse popup menu is being used,
5570 we don't update last_nonmenu_event; it continues to hold the mouse
5571 event that preceded the first level of menu. */
5572 if (!used_mouse_menu)
5573 last_nonmenu_event = key;
5574
5575 /* If the sequence is unbound, see if we can hang a function key
5576 off the end of it. We only want to scan real keyboard input
5577 for function key sequences, so if mock_input says that we're
5578 re-reading old events, don't examine it. */
5579 if (first_binding >= nmaps
5580 && t >= mock_input)
5581 {
5582 Lisp_Object fkey_next;
5583
5584 /* Continue scan from fkey_end until we find a bound suffix.
5585 If we fail, increment fkey_start
5586 and start fkey_end from there. */
5587 while (fkey_end < t)
5588 {
5589 Lisp_Object key;
5590
5591 key = keybuf[fkey_end++];
5592 /* Look up meta-characters by prefixing them
5593 with meta_prefix_char. I hate this. */
5594 if (INTEGERP (key) && XINT (key) & meta_modifier)
5595 {
5596 fkey_next
5597 = get_keymap_1
5598 (get_keyelt
5599 (access_keymap (fkey_map, meta_prefix_char, 1, 0)),
5600 0, 1);
5601 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
5602 }
5603 else
5604 fkey_next = fkey_map;
5605
5606 fkey_next
5607 = get_keyelt (access_keymap (fkey_next, key, 1, 0));
5608
5609 #if 0 /* I didn't turn this on, because it might cause trouble
5610 for the mapping of return into C-m and tab into C-i. */
5611 /* Optionally don't map function keys into other things.
5612 This enables the user to redefine kp- keys easily. */
5613 if (SYMBOLP (key) && !NILP (Vinhibit_function_key_mapping))
5614 fkey_next = Qnil;
5615 #endif
5616
5617 /* If the function key map gives a function, not an
5618 array, then call the function with no args and use
5619 its value instead. */
5620 if (SYMBOLP (fkey_next) && ! NILP (Ffboundp (fkey_next))
5621 && fkey_end == t)
5622 {
5623 struct gcpro gcpro1, gcpro2, gcpro3;
5624 Lisp_Object tem;
5625 tem = fkey_next;
5626
5627 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
5628 fkey_next = call1 (fkey_next, prompt);
5629 UNGCPRO;
5630 /* If the function returned something invalid,
5631 barf--don't ignore it.
5632 (To ignore it safely, we would need to gcpro a bunch of
5633 other variables.) */
5634 if (! (VECTORP (fkey_next) || STRINGP (fkey_next)))
5635 error ("Function in function-key-map returns invalid key sequence");
5636 }
5637
5638 function_key_possible = ! NILP (fkey_next);
5639
5640 /* If keybuf[fkey_start..fkey_end] is bound in the
5641 function key map and it's a suffix of the current
5642 sequence (i.e. fkey_end == t), replace it with
5643 the binding and restart with fkey_start at the end. */
5644 if ((VECTORP (fkey_next) || STRINGP (fkey_next))
5645 && fkey_end == t)
5646 {
5647 int len = XFASTINT (Flength (fkey_next));
5648
5649 t = fkey_start + len;
5650 if (t >= bufsize)
5651 error ("key sequence too long");
5652
5653 if (VECTORP (fkey_next))
5654 bcopy (XVECTOR (fkey_next)->contents,
5655 keybuf + fkey_start,
5656 (t - fkey_start) * sizeof (keybuf[0]));
5657 else if (STRINGP (fkey_next))
5658 {
5659 int i;
5660
5661 for (i = 0; i < len; i++)
5662 XSETFASTINT (keybuf[fkey_start + i],
5663 XSTRING (fkey_next)->data[i]);
5664 }
5665
5666 mock_input = t;
5667 fkey_start = fkey_end = t;
5668 fkey_map = Vfunction_key_map;
5669
5670 /* Do pass the results through key-translation-map. */
5671 keytran_start = keytran_end = 0;
5672 keytran_map = Vkey_translation_map;
5673
5674 goto replay_sequence;
5675 }
5676
5677 fkey_map = get_keymap_1 (fkey_next, 0, 1);
5678
5679 /* If we no longer have a bound suffix, try a new positions for
5680 fkey_start. */
5681 if (NILP (fkey_map))
5682 {
5683 fkey_end = ++fkey_start;
5684 fkey_map = Vfunction_key_map;
5685 function_key_possible = 0;
5686 }
5687 }
5688 }
5689
5690 /* Look for this sequence in key-translation-map. */
5691 {
5692 Lisp_Object keytran_next;
5693
5694 /* Scan from keytran_end until we find a bound suffix. */
5695 while (keytran_end < t)
5696 {
5697 Lisp_Object key;
5698
5699 key = keybuf[keytran_end++];
5700 /* Look up meta-characters by prefixing them
5701 with meta_prefix_char. I hate this. */
5702 if (INTEGERP (key) && XINT (key) & meta_modifier)
5703 {
5704 keytran_next
5705 = get_keymap_1
5706 (get_keyelt
5707 (access_keymap (keytran_map, meta_prefix_char, 1, 0)),
5708 0, 1);
5709 XSETFASTINT (key, XFASTINT (key) & ~meta_modifier);
5710 }
5711 else
5712 keytran_next = keytran_map;
5713
5714 keytran_next
5715 = get_keyelt (access_keymap (keytran_next, key, 1, 0));
5716
5717 /* If the key translation map gives a function, not an
5718 array, then call the function with no args and use
5719 its value instead. */
5720 if (SYMBOLP (keytran_next) && ! NILP (Ffboundp (keytran_next))
5721 && keytran_end == t)
5722 {
5723 struct gcpro gcpro1, gcpro2, gcpro3;
5724 Lisp_Object tem;
5725 tem = keytran_next;
5726
5727 GCPRO3 (fkey_map, keytran_map, delayed_switch_frame);
5728 keytran_next = call1 (keytran_next, prompt);
5729 UNGCPRO;
5730 /* If the function returned something invalid,
5731 barf--don't ignore it.
5732 (To ignore it safely, we would need to gcpro a bunch of
5733 other variables.) */
5734 if (! (VECTORP (keytran_next) || STRINGP (keytran_next)))
5735 error ("Function in key-translation-map returns invalid key sequence");
5736 }
5737
5738 key_translation_possible = ! NILP (keytran_next);
5739
5740 /* If keybuf[keytran_start..keytran_end] is bound in the
5741 key translation map and it's a suffix of the current
5742 sequence (i.e. keytran_end == t), replace it with
5743 the binding and restart with keytran_start at the end. */
5744 if ((VECTORP (keytran_next) || STRINGP (keytran_next))
5745 && keytran_end == t)
5746 {
5747 int len = XFASTINT (Flength (keytran_next));
5748
5749 t = keytran_start + len;
5750 if (t >= bufsize)
5751 error ("key sequence too long");
5752
5753 if (VECTORP (keytran_next))
5754 bcopy (XVECTOR (keytran_next)->contents,
5755 keybuf + keytran_start,
5756 (t - keytran_start) * sizeof (keybuf[0]));
5757 else if (STRINGP (keytran_next))
5758 {
5759 int i;
5760
5761 for (i = 0; i < len; i++)
5762 XSETFASTINT (keybuf[keytran_start + i],
5763 XSTRING (keytran_next)->data[i]);
5764 }
5765
5766 mock_input = t;
5767 keytran_start = keytran_end = t;
5768 keytran_map = Vkey_translation_map;
5769
5770 /* Don't pass the results of key-translation-map
5771 through function-key-map. */
5772 fkey_start = fkey_end = t;
5773 fkey_map = Vkey_translation_map;
5774
5775 goto replay_sequence;
5776 }
5777
5778 keytran_map = get_keymap_1 (keytran_next, 0, 1);
5779
5780 /* If we no longer have a bound suffix, try a new positions for
5781 keytran_start. */
5782 if (NILP (keytran_map))
5783 {
5784 keytran_end = ++keytran_start;
5785 keytran_map = Vkey_translation_map;
5786 key_translation_possible = 0;
5787 }
5788 }
5789 }
5790
5791 /* If KEY is not defined in any of the keymaps,
5792 and cannot be part of a function key or translation,
5793 and is an upper case letter
5794 use the corresponding lower-case letter instead. */
5795 if (first_binding == nmaps && ! function_key_possible
5796 && ! key_translation_possible
5797 && INTEGERP (key)
5798 && ((((XINT (key) & 0x3ffff)
5799 < XSTRING (current_buffer->downcase_table)->size)
5800 && UPPERCASEP (XINT (key) & 0x3ffff))
5801 || (XINT (key) & shift_modifier)))
5802 {
5803 original_uppercase = key;
5804 original_uppercase_position = t - 1;
5805
5806 if (XINT (key) & shift_modifier)
5807 XSETINT (key, XINT (key) & ~shift_modifier);
5808 else
5809 XSETINT (key, (DOWNCASE (XINT (key) & 0x3ffff)
5810 | (XINT (key) & ~0x3ffff)));
5811
5812 keybuf[t - 1] = key;
5813 mock_input = t;
5814 goto replay_sequence;
5815 }
5816 /* If KEY is not defined in any of the keymaps,
5817 and cannot be part of a function key or translation,
5818 and is a shifted function key,
5819 use the corresponding unshifted function key instead. */
5820 if (first_binding == nmaps && ! function_key_possible
5821 && ! key_translation_possible
5822 && SYMBOLP (key))
5823 {
5824 Lisp_Object breakdown;
5825 int modifiers;
5826
5827 original_uppercase = key;
5828 original_uppercase_position = t - 1;
5829
5830 breakdown = parse_modifiers (key);
5831 modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
5832 if (modifiers & shift_modifier)
5833 {
5834 modifiers &= ~shift_modifier;
5835 key = apply_modifiers (modifiers,
5836 XCONS (breakdown)->car);
5837
5838 keybuf[t - 1] = key;
5839 mock_input = t;
5840 goto replay_sequence;
5841 }
5842 }
5843 }
5844
5845 if (!dummyflag)
5846 read_key_sequence_cmd = (first_binding < nmaps
5847 ? defs[first_binding]
5848 : Qnil);
5849
5850 unread_switch_frame = delayed_switch_frame;
5851 unbind_to (count, Qnil);
5852
5853 if (dont_downcase_last && t - 1 == original_uppercase_position)
5854 keybuf[t - 1] = original_uppercase;
5855
5856 /* Occasionally we fabricate events, perhaps by expanding something
5857 according to function-key-map, or by adding a prefix symbol to a
5858 mouse click in the scroll bar or modeline. In this cases, return
5859 the entire generated key sequence, even if we hit an unbound
5860 prefix or a definition before the end. This means that you will
5861 be able to push back the event properly, and also means that
5862 read-key-sequence will always return a logical unit.
5863
5864 Better ideas? */
5865 for (; t < mock_input; t++)
5866 {
5867 if (echo_keystrokes)
5868 echo_char (keybuf[t]);
5869 add_command_key (keybuf[t]);
5870 }
5871
5872 return t;
5873 }
5874
5875 #if 0 /* This doc string is too long for some compilers.
5876 This commented-out definition serves for DOC. */
5877 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
5878 "Read a sequence of keystrokes and return as a string or vector.\n\
5879 The sequence is sufficient to specify a non-prefix command in the\n\
5880 current local and global maps.\n\
5881 \n\
5882 First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
5883 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
5884 as a continuation of the previous key.\n\
5885 \n\
5886 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not\n\
5887 convert the last event to lower case. (Normally any upper case event\n\
5888 is converted to lower case if the original event is undefined and the lower\n\
5889 case equivalent is defined.) A non-nil value is appropriate for reading\n\
5890 a key sequence to be defined.\n\
5891 \n\
5892 A C-g typed while in this function is treated like any other character,\n\
5893 and `quit-flag' is not set.\n\
5894 \n\
5895 If the key sequence starts with a mouse click, then the sequence is read\n\
5896 using the keymaps of the buffer of the window clicked in, not the buffer\n\
5897 of the selected window as normal.\n\
5898 ""\n\
5899 `read-key-sequence' drops unbound button-down events, since you normally\n\
5900 only care about the click or drag events which follow them. If a drag\n\
5901 or multi-click event is unbound, but the corresponding click event would\n\
5902 be bound, `read-key-sequence' turns the event into a click event at the\n\
5903 drag's starting position. This means that you don't have to distinguish\n\
5904 between click and drag, double, or triple events unless you want to.\n\
5905 \n\
5906 `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
5907 lines separating windows, and scroll bars with imaginary keys\n\
5908 `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
5909 \n\
5910 Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this\n\
5911 function will process a switch-frame event if the user switches frames\n\
5912 before typing anything. If the user switches frames in the middle of a\n\
5913 key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME\n\
5914 is nil, then the event will be put off until after the current key sequence.\n\
5915 \n\
5916 `read-key-sequence' checks `function-key-map' for function key\n\
5917 sequences, where they wouldn't conflict with ordinary bindings. See\n\
5918 `function-key-map' for more details.")
5919 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame)
5920 #endif
5921
5922 DEFUN ("read-key-sequence", Fread_key_sequence, Sread_key_sequence, 1, 4, 0,
5923 0)
5924 (prompt, continue_echo, dont_downcase_last, can_return_switch_frame)
5925 Lisp_Object prompt, continue_echo, dont_downcase_last;
5926 Lisp_Object can_return_switch_frame;
5927 {
5928 Lisp_Object keybuf[30];
5929 register int i;
5930 struct gcpro gcpro1, gcpro2;
5931
5932 if (!NILP (prompt))
5933 CHECK_STRING (prompt, 0);
5934 QUIT;
5935
5936 bzero (keybuf, sizeof keybuf);
5937 GCPRO1 (keybuf[0]);
5938 gcpro1.nvars = (sizeof keybuf/sizeof (keybuf[0]));
5939
5940 if (NILP (continue_echo))
5941 this_command_key_count = 0;
5942
5943 i = read_key_sequence (keybuf, (sizeof keybuf/sizeof (keybuf[0])),
5944 prompt, ! NILP (dont_downcase_last),
5945 ! NILP (can_return_switch_frame));
5946
5947 if (i == -1)
5948 {
5949 Vquit_flag = Qt;
5950 QUIT;
5951 }
5952 UNGCPRO;
5953 return make_event_array (i, keybuf);
5954 }
5955 \f
5956 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 2, 0,
5957 "Execute CMD as an editor command.\n\
5958 CMD must be a symbol that satisfies the `commandp' predicate.\n\
5959 Optional second arg RECORD-FLAG non-nil\n\
5960 means unconditionally put this command in `command-history'.\n\
5961 Otherwise, that is done only if an arg is read using the minibuffer.")
5962 (cmd, record)
5963 Lisp_Object cmd, record;
5964 {
5965 register Lisp_Object final;
5966 register Lisp_Object tem;
5967 Lisp_Object prefixarg;
5968 struct backtrace backtrace;
5969 extern int debug_on_next_call;
5970
5971 prefixarg = current_kboard->Vprefix_arg;
5972 current_kboard->Vprefix_arg = Qnil;
5973 Vcurrent_prefix_arg = prefixarg;
5974 debug_on_next_call = 0;
5975
5976 if (SYMBOLP (cmd))
5977 {
5978 tem = Fget (cmd, Qdisabled);
5979 if (!NILP (tem) && !NILP (Vrun_hooks))
5980 return call1 (Vrun_hooks, Qdisabled_command_hook);
5981 }
5982
5983 while (1)
5984 {
5985 final = Findirect_function (cmd);
5986
5987 if (CONSP (final) && (tem = Fcar (final), EQ (tem, Qautoload)))
5988 do_autoload (final, cmd);
5989 else
5990 break;
5991 }
5992
5993 if (STRINGP (final) || VECTORP (final))
5994 {
5995 /* If requested, place the macro in the command history. For
5996 other sorts of commands, call-interactively takes care of
5997 this. */
5998 if (!NILP (record))
5999 Vcommand_history
6000 = Fcons (Fcons (Qexecute_kbd_macro,
6001 Fcons (final, Fcons (prefixarg, Qnil))),
6002 Vcommand_history);
6003
6004 return Fexecute_kbd_macro (final, prefixarg);
6005 }
6006 if (CONSP (final) || SUBRP (final) || COMPILEDP (final))
6007 {
6008 backtrace.next = backtrace_list;
6009 backtrace_list = &backtrace;
6010 backtrace.function = &Qcall_interactively;
6011 backtrace.args = &cmd;
6012 backtrace.nargs = 1;
6013 backtrace.evalargs = 0;
6014
6015 tem = Fcall_interactively (cmd, record);
6016
6017 backtrace_list = backtrace.next;
6018 return tem;
6019 }
6020 return Qnil;
6021 }
6022 \f
6023 DEFUN ("execute-extended-command", Fexecute_extended_command, Sexecute_extended_command,
6024 1, 1, "P",
6025 "Read function name, then read its arguments and call it.")
6026 (prefixarg)
6027 Lisp_Object prefixarg;
6028 {
6029 Lisp_Object function;
6030 char buf[40];
6031 Lisp_Object saved_keys;
6032 struct gcpro gcpro1;
6033
6034 saved_keys = Fvector (this_command_key_count,
6035 XVECTOR (this_command_keys)->contents);
6036 buf[0] = 0;
6037 GCPRO1 (saved_keys);
6038
6039 if (EQ (prefixarg, Qminus))
6040 strcpy (buf, "- ");
6041 else if (CONSP (prefixarg) && XINT (XCONS (prefixarg)->car) == 4)
6042 strcpy (buf, "C-u ");
6043 else if (CONSP (prefixarg) && INTEGERP (XCONS (prefixarg)->car))
6044 {
6045 if (sizeof (int) == sizeof (EMACS_INT))
6046 sprintf (buf, "%d ", XINT (XCONS (prefixarg)->car));
6047 else if (sizeof (long) == sizeof (EMACS_INT))
6048 sprintf (buf, "%ld ", XINT (XCONS (prefixarg)->car));
6049 else
6050 abort ();
6051 }
6052 else if (INTEGERP (prefixarg))
6053 {
6054 if (sizeof (int) == sizeof (EMACS_INT))
6055 sprintf (buf, "%d ", XINT (prefixarg));
6056 else if (sizeof (long) == sizeof (EMACS_INT))
6057 sprintf (buf, "%ld ", XINT (prefixarg));
6058 else
6059 abort ();
6060 }
6061
6062 /* This isn't strictly correct if execute-extended-command
6063 is bound to anything else. Perhaps it should use
6064 this_command_keys? */
6065 strcat (buf, "M-x ");
6066
6067 /* Prompt with buf, and then read a string, completing from and
6068 restricting to the set of all defined commands. Don't provide
6069 any initial input. Save the command read on the extended-command
6070 history list. */
6071 function = Fcompleting_read (build_string (buf),
6072 Vobarray, Qcommandp,
6073 Qt, Qnil, Qextended_command_history);
6074
6075 /* Set this_command_keys to the concatenation of saved_keys and
6076 function, followed by a RET. */
6077 {
6078 struct Lisp_String *str;
6079 Lisp_Object *keys;
6080 int i;
6081 Lisp_Object tem;
6082
6083 this_command_key_count = 0;
6084
6085 keys = XVECTOR (saved_keys)->contents;
6086 for (i = 0; i < XVECTOR (saved_keys)->size; i++)
6087 add_command_key (keys[i]);
6088
6089 str = XSTRING (function);
6090 for (i = 0; i < str->size; i++)
6091 {
6092 XSETFASTINT (tem, str->data[i]);
6093 add_command_key (tem);
6094 }
6095
6096 XSETFASTINT (tem, '\015');
6097 add_command_key (tem);
6098 }
6099
6100 UNGCPRO;
6101
6102 function = Fintern (function, Qnil);
6103 current_kboard->Vprefix_arg = prefixarg;
6104 this_command = function;
6105
6106 return Fcommand_execute (function, Qt);
6107 }
6108 \f
6109
6110 detect_input_pending ()
6111 {
6112 if (!input_pending)
6113 get_input_pending (&input_pending);
6114
6115 return input_pending;
6116 }
6117
6118 /* This is called in some cases before a possible quit.
6119 It cases the next call to detect_input_pending to recompute input_pending.
6120 So calling this function unnecessarily can't do any harm. */
6121 clear_input_pending ()
6122 {
6123 input_pending = 0;
6124 }
6125
6126 DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
6127 "T if command input is currently available with no waiting.\n\
6128 Actually, the value is nil only if we can be sure that no input is available.")
6129 ()
6130 {
6131 if (!NILP (Vunread_command_events) || unread_command_char != -1)
6132 return (Qt);
6133
6134 return detect_input_pending () ? Qt : Qnil;
6135 }
6136
6137 DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
6138 "Return vector of last 100 events, not counting those from keyboard macros.")
6139 ()
6140 {
6141 Lisp_Object *keys = XVECTOR (recent_keys)->contents;
6142 Lisp_Object val;
6143
6144 if (total_keys < NUM_RECENT_KEYS)
6145 return Fvector (total_keys, keys);
6146 else
6147 {
6148 val = Fvector (NUM_RECENT_KEYS, keys);
6149 bcopy (keys + recent_keys_index,
6150 XVECTOR (val)->contents,
6151 (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
6152 bcopy (keys,
6153 XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
6154 recent_keys_index * sizeof (Lisp_Object));
6155 return val;
6156 }
6157 }
6158
6159 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
6160 "Return the key sequence that invoked this command.\n\
6161 The value is a string or a vector.")
6162 ()
6163 {
6164 return make_event_array (this_command_key_count,
6165 XVECTOR (this_command_keys)->contents);
6166 }
6167
6168 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
6169 "Return the current depth in recursive edits.")
6170 ()
6171 {
6172 Lisp_Object temp;
6173 XSETFASTINT (temp, command_loop_level + minibuf_level);
6174 return temp;
6175 }
6176
6177 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
6178 "FOpen dribble file: ",
6179 "Start writing all keyboard characters to a dribble file called FILE.\n\
6180 If FILE is nil, close any open dribble file.")
6181 (file)
6182 Lisp_Object file;
6183 {
6184 if (NILP (file))
6185 {
6186 if (dribble)
6187 {
6188 fclose (dribble);
6189 dribble = 0;
6190 }
6191 }
6192 else
6193 {
6194 file = Fexpand_file_name (file, Qnil);
6195 dribble = fopen (XSTRING (file)->data, "w");
6196 }
6197 return Qnil;
6198 }
6199
6200 DEFUN ("discard-input", Fdiscard_input, Sdiscard_input, 0, 0, 0,
6201 "Discard the contents of the terminal input buffer.\n\
6202 Also cancel any kbd macro being defined.")
6203 ()
6204 {
6205 current_kboard->defining_kbd_macro = Qnil;
6206 update_mode_lines++;
6207
6208 Vunread_command_events = Qnil;
6209 unread_command_char = -1;
6210
6211 discard_tty_input ();
6212
6213 /* Without the cast, GCC complains that this assignment loses the
6214 volatile qualifier of kbd_store_ptr. Is there anything wrong
6215 with that? */
6216 kbd_fetch_ptr = (struct input_event *) kbd_store_ptr;
6217 Ffillarray (kbd_buffer_frame_or_window, Qnil);
6218 input_pending = 0;
6219
6220 return Qnil;
6221 }
6222 \f
6223 DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
6224 "Stop Emacs and return to superior process. You can resume later.\n\
6225 If `cannot-suspend' is non-nil, or if the system doesn't support job\n\
6226 control, run a subshell instead.\n\n\
6227 If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
6228 to be read as terminal input by Emacs's parent, after suspension.\n\
6229 \n\
6230 Before suspending, run the normal hook `suspend-hook'.\n\
6231 After resumption run the normal hook `suspend-resume-hook'.\n\
6232 \n\
6233 Some operating systems cannot stop the Emacs process and resume it later.\n\
6234 On such systems, Emacs starts a subshell instead of suspending.")
6235 (stuffstring)
6236 Lisp_Object stuffstring;
6237 {
6238 Lisp_Object tem;
6239 int count = specpdl_ptr - specpdl;
6240 int old_height, old_width;
6241 int width, height;
6242 struct gcpro gcpro1, gcpro2;
6243 extern init_sys_modes ();
6244
6245 if (!NILP (stuffstring))
6246 CHECK_STRING (stuffstring, 0);
6247
6248 /* Run the functions in suspend-hook. */
6249 if (!NILP (Vrun_hooks))
6250 call1 (Vrun_hooks, intern ("suspend-hook"));
6251
6252 GCPRO1 (stuffstring);
6253 get_frame_size (&old_width, &old_height);
6254 reset_sys_modes ();
6255 /* sys_suspend can get an error if it tries to fork a subshell
6256 and the system resources aren't available for that. */
6257 record_unwind_protect (init_sys_modes, 0);
6258 stuff_buffered_input (stuffstring);
6259 if (cannot_suspend)
6260 sys_subshell ();
6261 else
6262 sys_suspend ();
6263 unbind_to (count, Qnil);
6264
6265 /* Check if terminal/window size has changed.
6266 Note that this is not useful when we are running directly
6267 with a window system; but suspend should be disabled in that case. */
6268 get_frame_size (&width, &height);
6269 if (width != old_width || height != old_height)
6270 change_frame_size (selected_frame, height, width, 0, 0);
6271
6272 /* Run suspend-resume-hook. */
6273 if (!NILP (Vrun_hooks))
6274 call1 (Vrun_hooks, intern ("suspend-resume-hook"));
6275
6276 UNGCPRO;
6277 return Qnil;
6278 }
6279
6280 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
6281 Then in any case stuff anything Emacs has read ahead and not used. */
6282
6283 stuff_buffered_input (stuffstring)
6284 Lisp_Object stuffstring;
6285 {
6286 /* stuff_char works only in BSD, versions 4.2 and up. */
6287 #ifdef BSD
6288 #ifndef BSD4_1
6289 register unsigned char *p;
6290
6291 if (STRINGP (stuffstring))
6292 {
6293 register int count;
6294
6295 p = XSTRING (stuffstring)->data;
6296 count = XSTRING (stuffstring)->size;
6297 while (count-- > 0)
6298 stuff_char (*p++);
6299 stuff_char ('\n');
6300 }
6301 /* Anything we have read ahead, put back for the shell to read. */
6302 /* ?? What should this do when we have multiple keyboards??
6303 Should we ignore anything that was typed in at the "wrong" kboard? */
6304 for (; kbd_fetch_ptr != kbd_store_ptr; kbd_fetch_ptr++)
6305 {
6306 if (kbd_fetch_ptr == kbd_buffer + KBD_BUFFER_SIZE)
6307 kbd_fetch_ptr = kbd_buffer;
6308 if (kbd_fetch_ptr->kind == ascii_keystroke)
6309 stuff_char (kbd_fetch_ptr->code);
6310 kbd_fetch_ptr->kind = no_event;
6311 (XVECTOR (kbd_buffer_frame_or_window)->contents[kbd_fetch_ptr
6312 - kbd_buffer]
6313 = Qnil);
6314 }
6315 input_pending = 0;
6316 #endif
6317 #endif /* BSD and not BSD4_1 */
6318 }
6319 \f
6320 set_waiting_for_input (time_to_clear)
6321 EMACS_TIME *time_to_clear;
6322 {
6323 input_available_clear_time = time_to_clear;
6324
6325 /* Tell interrupt_signal to throw back to read_char, */
6326 waiting_for_input = 1;
6327
6328 /* If interrupt_signal was called before and buffered a C-g,
6329 make it run again now, to avoid timing error. */
6330 if (!NILP (Vquit_flag))
6331 quit_throw_to_read_char ();
6332 }
6333
6334 clear_waiting_for_input ()
6335 {
6336 /* Tell interrupt_signal not to throw back to read_char, */
6337 waiting_for_input = 0;
6338 input_available_clear_time = 0;
6339 }
6340
6341 /* This routine is called at interrupt level in response to C-G.
6342 If interrupt_input, this is the handler for SIGINT.
6343 Otherwise, it is called from kbd_buffer_store_event,
6344 in handling SIGIO or SIGTINT.
6345
6346 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
6347 immediately throw back to read_char.
6348
6349 Otherwise it sets the Lisp variable quit-flag not-nil.
6350 This causes eval to throw, when it gets a chance.
6351 If quit-flag is already non-nil, it stops the job right away. */
6352
6353 SIGTYPE
6354 interrupt_signal (signalnum) /* If we don't have an argument, */
6355 int signalnum; /* some compilers complain in signal calls. */
6356 {
6357 char c;
6358 /* Must preserve main program's value of errno. */
6359 int old_errno = errno;
6360
6361 #ifdef USG
6362 if (!read_socket_hook && NILP (Vwindow_system))
6363 {
6364 /* USG systems forget handlers when they are used;
6365 must reestablish each time */
6366 signal (SIGINT, interrupt_signal);
6367 signal (SIGQUIT, interrupt_signal);
6368 }
6369 #endif /* USG */
6370
6371 cancel_echoing ();
6372
6373 if (!NILP (Vquit_flag) && FRAME_TERMCAP_P (selected_frame))
6374 {
6375 fflush (stdout);
6376 reset_sys_modes ();
6377 sigfree ();
6378 #ifdef SIGTSTP /* Support possible in later USG versions */
6379 /*
6380 * On systems which can suspend the current process and return to the original
6381 * shell, this command causes the user to end up back at the shell.
6382 * The "Auto-save" and "Abort" questions are not asked until
6383 * the user elects to return to emacs, at which point he can save the current
6384 * job and either dump core or continue.
6385 */
6386 sys_suspend ();
6387 #else
6388 #ifdef VMS
6389 if (sys_suspend () == -1)
6390 {
6391 printf ("Not running as a subprocess;\n");
6392 printf ("you can continue or abort.\n");
6393 }
6394 #else /* not VMS */
6395 /* Perhaps should really fork an inferior shell?
6396 But that would not provide any way to get back
6397 to the original shell, ever. */
6398 printf ("No support for stopping a process on this operating system;\n");
6399 printf ("you can continue or abort.\n");
6400 #endif /* not VMS */
6401 #endif /* not SIGTSTP */
6402 #ifdef MSDOS
6403 /* We must remain inside the screen area when the internal terminal
6404 is used. Note that [Enter] is not echoed by dos. */
6405 cursor_to (0, 0);
6406 #endif
6407 printf ("Auto-save? (y or n) ");
6408 fflush (stdout);
6409 if (((c = getchar ()) & ~040) == 'Y')
6410 {
6411 Fdo_auto_save (Qt, Qnil);
6412 #ifdef MSDOS
6413 printf ("\r\nAuto-save done");
6414 #else /* not MSDOS */
6415 printf ("Auto-save done\n");
6416 #endif /* not MSDOS */
6417 }
6418 while (c != '\n') c = getchar ();
6419 #ifdef MSDOS
6420 printf ("\r\nAbort? (y or n) ");
6421 #else /* not MSDOS */
6422 #ifdef VMS
6423 printf ("Abort (and enter debugger)? (y or n) ");
6424 #else /* not VMS */
6425 printf ("Abort (and dump core)? (y or n) ");
6426 #endif /* not VMS */
6427 #endif /* not MSDOS */
6428 fflush (stdout);
6429 if (((c = getchar ()) & ~040) == 'Y')
6430 abort ();
6431 while (c != '\n') c = getchar ();
6432 #ifdef MSDOS
6433 printf ("\r\nContinuing...\r\n");
6434 #else /* not MSDOS */
6435 printf ("Continuing...\n");
6436 #endif /* not MSDOS */
6437 fflush (stdout);
6438 init_sys_modes ();
6439 }
6440 else
6441 {
6442 /* If executing a function that wants to be interrupted out of
6443 and the user has not deferred quitting by binding `inhibit-quit'
6444 then quit right away. */
6445 if (immediate_quit && NILP (Vinhibit_quit))
6446 {
6447 immediate_quit = 0;
6448 sigfree ();
6449 Fsignal (Qquit, Qnil);
6450 }
6451 else
6452 /* Else request quit when it's safe */
6453 Vquit_flag = Qt;
6454 }
6455
6456 if (waiting_for_input && !echoing)
6457 quit_throw_to_read_char ();
6458
6459 errno = old_errno;
6460 }
6461
6462 /* Handle a C-g by making read_char return C-g. */
6463
6464 quit_throw_to_read_char ()
6465 {
6466 quit_error_check ();
6467 sigfree ();
6468 /* Prevent another signal from doing this before we finish. */
6469 clear_waiting_for_input ();
6470 input_pending = 0;
6471
6472 Vunread_command_events = Qnil;
6473 unread_command_char = -1;
6474
6475 #ifdef POLL_FOR_INPUT
6476 /* May be > 1 if in recursive minibuffer. */
6477 if (poll_suppress_count == 0)
6478 abort ();
6479 #endif
6480 #ifdef MULTI_FRAME
6481 if (FRAMEP (internal_last_event_frame)
6482 && XFRAME (internal_last_event_frame) != selected_frame)
6483 Fhandle_switch_frame (make_lispy_switch_frame (internal_last_event_frame));
6484 #endif
6485
6486 _longjmp (getcjmp, 1);
6487 }
6488 \f
6489 DEFUN ("set-input-mode", Fset_input_mode, Sset_input_mode, 3, 4, 0,
6490 "Set mode of reading keyboard input.\n\
6491 First arg INTERRUPT non-nil means use input interrupts;\n\
6492 nil means use CBREAK mode.\n\
6493 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
6494 (no effect except in CBREAK mode).\n\
6495 Third arg META t means accept 8-bit input (for a Meta key).\n\
6496 META nil means ignore the top bit, on the assumption it is parity.\n\
6497 Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
6498 Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\
6499 See also `current-input-mode'.")
6500 (interrupt, flow, meta, quit)
6501 Lisp_Object interrupt, flow, meta, quit;
6502 {
6503 if (!NILP (quit)
6504 && (!INTEGERP (quit) || XINT (quit) < 0 || XINT (quit) > 0400))
6505 error ("set-input-mode: QUIT must be an ASCII character");
6506
6507 #ifdef POLL_FOR_INPUT
6508 stop_polling ();
6509 #endif
6510
6511 reset_sys_modes ();
6512 #ifdef SIGIO
6513 /* Note SIGIO has been undef'd if FIONREAD is missing. */
6514 if (read_socket_hook)
6515 {
6516 /* When using X, don't give the user a real choice,
6517 because we haven't implemented the mechanisms to support it. */
6518 #ifdef NO_SOCK_SIGIO
6519 interrupt_input = 0;
6520 #else /* not NO_SOCK_SIGIO */
6521 interrupt_input = 1;
6522 #endif /* NO_SOCK_SIGIO */
6523 }
6524 else
6525 interrupt_input = !NILP (interrupt);
6526 #else /* not SIGIO */
6527 interrupt_input = 0;
6528 #endif /* not SIGIO */
6529
6530 /* Our VMS input only works by interrupts, as of now. */
6531 #ifdef VMS
6532 interrupt_input = 1;
6533 #endif
6534
6535 flow_control = !NILP (flow);
6536 if (NILP (meta))
6537 meta_key = 0;
6538 else if (EQ (meta, Qt))
6539 meta_key = 1;
6540 else
6541 meta_key = 2;
6542 if (!NILP (quit))
6543 /* Don't let this value be out of range. */
6544 quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
6545
6546 init_sys_modes ();
6547
6548 #ifdef POLL_FOR_INPUT
6549 poll_suppress_count = 1;
6550 start_polling ();
6551 #endif
6552 return Qnil;
6553 }
6554
6555 DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
6556 "Return information about the way Emacs currently reads keyboard input.\n\
6557 The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
6558 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
6559 nil, Emacs is using CBREAK mode.\n\
6560 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
6561 terminal; this does not apply if Emacs uses interrupt-driven input.\n\
6562 META is t if accepting 8-bit input with 8th bit as Meta flag.\n\
6563 META nil means ignoring the top bit, on the assumption it is parity.\n\
6564 META is neither t nor nil if accepting 8-bit input and using\n\
6565 all 8 bits as the character code.\n\
6566 QUIT is the character Emacs currently uses to quit.\n\
6567 The elements of this list correspond to the arguments of\n\
6568 `set-input-mode'.")
6569 ()
6570 {
6571 Lisp_Object val[4];
6572
6573 val[0] = interrupt_input ? Qt : Qnil;
6574 val[1] = flow_control ? Qt : Qnil;
6575 val[2] = meta_key == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
6576 XSETFASTINT (val[3], quit_char);
6577
6578 return Flist (sizeof (val) / sizeof (val[0]), val);
6579 }
6580
6581 \f
6582 /*
6583 * Set up a new kboard object with reasonable initial values.
6584 */
6585 void
6586 init_kboard (kb)
6587 KBOARD *kb;
6588 {
6589 kb->Vprefix_arg = Qnil;
6590 kb->kbd_queue = Qnil;
6591 kb->kbd_queue_has_data = 0;
6592 kb->immediate_echo = 0;
6593 kb->echoptr = kb->echobuf;
6594 kb->echo_after_prompt = -1;
6595 kb->kbd_macro_buffer = 0;
6596 kb->kbd_macro_bufsize = 0;
6597 kb->defining_kbd_macro = Qnil;
6598 kb->Vlast_kbd_macro = Qnil;
6599 kb->reference_count = 0;
6600 kb->Vsystem_key_alist = Qnil;
6601 kb->Vdefault_minibuffer_frame = Qnil;
6602 }
6603
6604 /*
6605 * Destroy the contents of a kboard object, but not the object itself.
6606 * We use this just before deleteing it, or if we're going to initialize
6607 * it a second time.
6608 */
6609 static void
6610 wipe_kboard (kb)
6611 KBOARD *kb;
6612 {
6613 if (kb->kbd_macro_buffer)
6614 xfree (kb->kbd_macro_buffer);
6615 }
6616
6617 #ifdef MULTI_KBOARD
6618 void
6619 delete_kboard (kb)
6620 KBOARD *kb;
6621 {
6622 KBOARD **kbp;
6623 for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
6624 if (*kbp == NULL)
6625 abort ();
6626 *kbp = kb->next_kboard;
6627 wipe_kboard (kb);
6628 xfree (kb);
6629 }
6630 #endif
6631
6632 init_keyboard ()
6633 {
6634 /* This is correct before outermost invocation of the editor loop */
6635 command_loop_level = -1;
6636 immediate_quit = 0;
6637 quit_char = Ctl ('g');
6638 Vunread_command_events = Qnil;
6639 unread_command_char = -1;
6640 total_keys = 0;
6641 recent_keys_index = 0;
6642 kbd_fetch_ptr = kbd_buffer;
6643 kbd_store_ptr = kbd_buffer;
6644 kbd_buffer_frame_or_window
6645 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
6646 #ifdef HAVE_MOUSE
6647 do_mouse_tracking = Qnil;
6648 #endif
6649 input_pending = 0;
6650
6651 #ifdef MULTI_FRAME
6652 /* This means that command_loop_1 won't try to select anything the first
6653 time through. */
6654 internal_last_event_frame = Qnil;
6655 Vlast_event_frame = internal_last_event_frame;
6656 #endif
6657
6658 #ifdef MULTI_KBOARD
6659 current_kboard = initial_kboard;
6660 #endif
6661 wipe_kboard (current_kboard);
6662 init_kboard (current_kboard);
6663
6664 if (initialized)
6665 Ffillarray (kbd_buffer_frame_or_window, Qnil);
6666
6667 kbd_buffer_frame_or_window
6668 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
6669 if (!noninteractive && !read_socket_hook && NILP (Vwindow_system))
6670 {
6671 signal (SIGINT, interrupt_signal);
6672 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
6673 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
6674 SIGQUIT and we can't tell which one it will give us. */
6675 signal (SIGQUIT, interrupt_signal);
6676 #endif /* HAVE_TERMIO */
6677 }
6678 /* Note SIGIO has been undef'd if FIONREAD is missing. */
6679 #ifdef SIGIO
6680 if (!noninteractive)
6681 signal (SIGIO, input_available_signal);
6682 #endif /* SIGIO */
6683
6684 /* Use interrupt input by default, if it works and noninterrupt input
6685 has deficiencies. */
6686
6687 #ifdef INTERRUPT_INPUT
6688 interrupt_input = 1;
6689 #else
6690 interrupt_input = 0;
6691 #endif
6692
6693 /* Our VMS input only works by interrupts, as of now. */
6694 #ifdef VMS
6695 interrupt_input = 1;
6696 #endif
6697
6698 sigfree ();
6699 dribble = 0;
6700
6701 if (keyboard_init_hook)
6702 (*keyboard_init_hook) ();
6703
6704 #ifdef POLL_FOR_INPUT
6705 poll_suppress_count = 1;
6706 start_polling ();
6707 #endif
6708 }
6709
6710 /* This type's only use is in syms_of_keyboard, to initialize the
6711 event header symbols and put properties on them. */
6712 struct event_head {
6713 Lisp_Object *var;
6714 char *name;
6715 Lisp_Object *kind;
6716 };
6717
6718 struct event_head head_table[] = {
6719 &Qmouse_movement, "mouse-movement", &Qmouse_movement,
6720 &Qscroll_bar_movement, "scroll-bar-movement", &Qmouse_movement,
6721 &Qswitch_frame, "switch-frame", &Qswitch_frame,
6722 &Qdelete_frame, "delete-frame", &Qdelete_frame,
6723 &Qiconify_frame, "iconify-frame", &Qiconify_frame,
6724 &Qmake_frame_visible, "make-frame-visible", &Qmake_frame_visible,
6725 };
6726
6727 syms_of_keyboard ()
6728 {
6729 Qdisabled_command_hook = intern ("disabled-command-hook");
6730 staticpro (&Qdisabled_command_hook);
6731
6732 Qself_insert_command = intern ("self-insert-command");
6733 staticpro (&Qself_insert_command);
6734
6735 Qforward_char = intern ("forward-char");
6736 staticpro (&Qforward_char);
6737
6738 Qbackward_char = intern ("backward-char");
6739 staticpro (&Qbackward_char);
6740
6741 Qdisabled = intern ("disabled");
6742 staticpro (&Qdisabled);
6743
6744 Qundefined = intern ("undefined");
6745 staticpro (&Qundefined);
6746
6747 Qpre_command_hook = intern ("pre-command-hook");
6748 staticpro (&Qpre_command_hook);
6749
6750 Qpost_command_hook = intern ("post-command-hook");
6751 staticpro (&Qpost_command_hook);
6752
6753 Qdeferred_action_function = intern ("deferred-action-function");
6754 staticpro (&Qdeferred_action_function);
6755
6756 Qcommand_hook_internal = intern ("command-hook-internal");
6757 staticpro (&Qcommand_hook_internal);
6758
6759 Qfunction_key = intern ("function-key");
6760 staticpro (&Qfunction_key);
6761 Qmouse_click = intern ("mouse-click");
6762 staticpro (&Qmouse_click);
6763
6764 Qmenu_enable = intern ("menu-enable");
6765 staticpro (&Qmenu_enable);
6766
6767 Qmode_line = intern ("mode-line");
6768 staticpro (&Qmode_line);
6769 Qvertical_line = intern ("vertical-line");
6770 staticpro (&Qvertical_line);
6771 Qvertical_scroll_bar = intern ("vertical-scroll-bar");
6772 staticpro (&Qvertical_scroll_bar);
6773 Qmenu_bar = intern ("menu-bar");
6774 staticpro (&Qmenu_bar);
6775
6776 Qabove_handle = intern ("above-handle");
6777 staticpro (&Qabove_handle);
6778 Qhandle = intern ("handle");
6779 staticpro (&Qhandle);
6780 Qbelow_handle = intern ("below-handle");
6781 staticpro (&Qbelow_handle);
6782
6783 Qevent_kind = intern ("event-kind");
6784 staticpro (&Qevent_kind);
6785 Qevent_symbol_elements = intern ("event-symbol-elements");
6786 staticpro (&Qevent_symbol_elements);
6787 Qevent_symbol_element_mask = intern ("event-symbol-element-mask");
6788 staticpro (&Qevent_symbol_element_mask);
6789 Qmodifier_cache = intern ("modifier-cache");
6790 staticpro (&Qmodifier_cache);
6791
6792 Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
6793 staticpro (&Qrecompute_lucid_menubar);
6794 Qactivate_menubar_hook = intern ("activate-menubar-hook");
6795 staticpro (&Qactivate_menubar_hook);
6796
6797 Qpolling_period = intern ("polling-period");
6798 staticpro (&Qpolling_period);
6799
6800 {
6801 struct event_head *p;
6802
6803 for (p = head_table;
6804 p < head_table + (sizeof (head_table) / sizeof (head_table[0]));
6805 p++)
6806 {
6807 *p->var = intern (p->name);
6808 staticpro (p->var);
6809 Fput (*p->var, Qevent_kind, *p->kind);
6810 Fput (*p->var, Qevent_symbol_elements, Fcons (*p->var, Qnil));
6811 }
6812 }
6813
6814 button_down_location = Fmake_vector (make_number (NUM_MOUSE_BUTTONS), Qnil);
6815 staticpro (&button_down_location);
6816
6817 {
6818 int i;
6819 int len = sizeof (modifier_names) / sizeof (modifier_names[0]);
6820
6821 modifier_symbols = Fmake_vector (make_number (len), Qnil);
6822 for (i = 0; i < len; i++)
6823 if (modifier_names[i])
6824 XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
6825 staticpro (&modifier_symbols);
6826 }
6827
6828 recent_keys = Fmake_vector (make_number (NUM_RECENT_KEYS), Qnil);
6829 staticpro (&recent_keys);
6830
6831 this_command_keys = Fmake_vector (make_number (40), Qnil);
6832 staticpro (&this_command_keys);
6833
6834 Qextended_command_history = intern ("extended-command-history");
6835 Fset (Qextended_command_history, Qnil);
6836 staticpro (&Qextended_command_history);
6837
6838 kbd_buffer_frame_or_window
6839 = Fmake_vector (make_number (KBD_BUFFER_SIZE), Qnil);
6840 staticpro (&kbd_buffer_frame_or_window);
6841
6842 accent_key_syms = Qnil;
6843 staticpro (&accent_key_syms);
6844
6845 func_key_syms = Qnil;
6846 staticpro (&func_key_syms);
6847
6848 system_key_syms = Qnil;
6849 staticpro (&system_key_syms);
6850
6851 mouse_syms = Qnil;
6852 staticpro (&mouse_syms);
6853
6854 unread_switch_frame = Qnil;
6855 staticpro (&unread_switch_frame);
6856
6857 defsubr (&Sread_key_sequence);
6858 defsubr (&Srecursive_edit);
6859 #ifdef HAVE_MOUSE
6860 defsubr (&Strack_mouse);
6861 #endif
6862 defsubr (&Sinput_pending_p);
6863 defsubr (&Scommand_execute);
6864 defsubr (&Srecent_keys);
6865 defsubr (&Sthis_command_keys);
6866 defsubr (&Ssuspend_emacs);
6867 defsubr (&Sabort_recursive_edit);
6868 defsubr (&Sexit_recursive_edit);
6869 defsubr (&Srecursion_depth);
6870 defsubr (&Stop_level);
6871 defsubr (&Sdiscard_input);
6872 defsubr (&Sopen_dribble_file);
6873 defsubr (&Sset_input_mode);
6874 defsubr (&Scurrent_input_mode);
6875 defsubr (&Sexecute_extended_command);
6876
6877 DEFVAR_LISP ("last-command-char", &last_command_char,
6878 "Last input event that was part of a command.");
6879
6880 DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char,
6881 "Last input event that was part of a command.");
6882
6883 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
6884 "Last input event in a command, except for mouse menu events.\n\
6885 Mouse menus give back keys that don't look like mouse events;\n\
6886 this variable holds the actual mouse event that led to the menu,\n\
6887 so that you can determine whether the command was run by mouse or not.");
6888
6889 DEFVAR_LISP ("last-input-char", &last_input_char,
6890 "Last input event.");
6891
6892 DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char,
6893 "Last input event.");
6894
6895 DEFVAR_LISP ("unread-command-events", &Vunread_command_events,
6896 "List of objects to be read as next command input events.");
6897
6898 DEFVAR_INT ("unread-command-char", &unread_command_char,
6899 "If not -1, an object to be read as next command input event.");
6900
6901 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
6902 "Meta-prefix character code. Meta-foo as command input\n\
6903 turns into this character followed by foo.");
6904 XSETINT (meta_prefix_char, 033);
6905
6906 DEFVAR_LISP ("last-command", &last_command,
6907 "The last command executed. Normally a symbol with a function definition,\n\
6908 but can be whatever was found in the keymap, or whatever the variable\n\
6909 `this-command' was set to by that command.\n\
6910 \n\
6911 The value `mode-exit' is special; it means that the previous command\n\
6912 read an event that told it to exit, and it did so and unread that event.\n\
6913 In other words, the present command is the event that made the previous\n\
6914 command exit.\n\
6915 \n\
6916 The value `kill-region' is special; it means that the previous command\n\
6917 was a kill command.");
6918 last_command = Qnil;
6919
6920 DEFVAR_LISP ("this-command", &this_command,
6921 "The command now being executed.\n\
6922 The command can set this variable; whatever is put here\n\
6923 will be in `last-command' during the following command.");
6924 this_command = Qnil;
6925
6926 DEFVAR_INT ("auto-save-interval", &auto_save_interval,
6927 "*Number of keyboard input characters between auto-saves.\n\
6928 Zero means disable autosaving due to number of characters typed.");
6929 auto_save_interval = 300;
6930
6931 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout,
6932 "*Number of seconds idle time before auto-save.\n\
6933 Zero or nil means disable auto-saving due to idleness.\n\
6934 After auto-saving due to this many seconds of idle time,\n\
6935 Emacs also does a garbage collection if that seems to be warranted.");
6936 XSETFASTINT (Vauto_save_timeout, 30);
6937
6938 DEFVAR_INT ("echo-keystrokes", &echo_keystrokes,
6939 "*Nonzero means echo unfinished commands after this many seconds of pause.");
6940 echo_keystrokes = 1;
6941
6942 DEFVAR_INT ("polling-period", &polling_period,
6943 "*Interval between polling for input during Lisp execution.\n\
6944 The reason for polling is to make C-g work to stop a running program.\n\
6945 Polling is needed only when using X windows and SIGIO does not work.\n\
6946 Polling is automatically disabled in all other cases.");
6947 polling_period = 2;
6948
6949 DEFVAR_LISP ("double-click-time", &Vdouble_click_time,
6950 "*Maximum time between mouse clicks to make a double-click.\n\
6951 Measured in milliseconds. nil means disable double-click recognition;\n\
6952 t means double-clicks have no time limit and are detected\n\
6953 by position only.");
6954 Vdouble_click_time = make_number (500);
6955
6956 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus,
6957 "*Non-nil means inhibit local map menu bar menus.");
6958 inhibit_local_menu_bar_menus = 0;
6959
6960 DEFVAR_INT ("num-input-keys", &num_input_keys,
6961 "Number of complete keys read from the keyboard so far.");
6962 num_input_keys = 0;
6963
6964 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame,
6965 "The frame in which the most recently read event occurred.\n\
6966 If the last event came from a keyboard macro, this is set to `macro'.");
6967 Vlast_event_frame = Qnil;
6968
6969 DEFVAR_LISP ("help-char", &Vhelp_char,
6970 "Character to recognize as meaning Help.\n\
6971 When it is read, do `(eval help-form)', and display result if it's a string.\n\
6972 If the value of `help-form' is nil, this char can be read normally.");
6973 XSETINT (Vhelp_char, Ctl ('H'));
6974
6975 DEFVAR_LISP ("help-form", &Vhelp_form,
6976 "Form to execute when character `help-char' is read.\n\
6977 If the form returns a string, that string is displayed.\n\
6978 If `help-form' is nil, the help char is not recognized.");
6979 Vhelp_form = Qnil;
6980
6981 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command,
6982 "Command to run when `help-char' character follows a prefix key.\n\
6983 This command is used only when there is no actual binding\n\
6984 for that character after that prefix key.");
6985 Vprefix_help_command = Qnil;
6986
6987 DEFVAR_LISP ("top-level", &Vtop_level,
6988 "Form to evaluate when Emacs starts up.\n\
6989 Useful to set before you dump a modified Emacs.");
6990 Vtop_level = Qnil;
6991
6992 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table,
6993 "String used as translate table for keyboard input, or nil.\n\
6994 Each character is looked up in this string and the contents used instead.\n\
6995 If string is of length N, character codes N and up are untranslated.");
6996 Vkeyboard_translate_table = Qnil;
6997
6998 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map,
6999 "Keymap of key translations that can override keymaps.\n\
7000 This keymap works like `function-key-map', but comes after that,\n\
7001 and applies even for keys that have ordinary bindings.");
7002 Vkey_translation_map = Qnil;
7003
7004 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend,
7005 "Non-nil means to always spawn a subshell instead of suspending,\n\
7006 even if the operating system has support for stopping a process.");
7007 cannot_suspend = 0;
7008
7009 DEFVAR_BOOL ("menu-prompting", &menu_prompting,
7010 "Non-nil means prompt with menus when appropriate.\n\
7011 This is done when reading from a keymap that has a prompt string,\n\
7012 for elements that have prompt strings.\n\
7013 The menu is displayed on the screen\n\
7014 if X menus were enabled at configuration\n\
7015 time and the previous event was a mouse click prefix key.\n\
7016 Otherwise, menu prompting uses the echo area.");
7017 menu_prompting = 1;
7018
7019 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char,
7020 "Character to see next line of menu prompt.\n\
7021 Type this character while in a menu prompt to rotate around the lines of it.");
7022 XSETINT (menu_prompt_more_char, ' ');
7023
7024 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
7025 "A mask of additional modifier keys to use with every keyboard character.\n\
7026 Emacs applies the modifiers of the character stored here to each keyboard\n\
7027 character it reads. For example, after evaluating the expression\n\
7028 (setq extra-keyboard-modifiers ?\\C-x)\n\
7029 all input characters will have the control modifier applied to them.\n\
7030 \n\
7031 Note that the character ?\\C-@, equivalent to the integer zero, does\n\
7032 not count as a control character; rather, it counts as a character\n\
7033 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\
7034 cancels any modification.");
7035 extra_keyboard_modifiers = 0;
7036
7037 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
7038 "If an editing command sets this to t, deactivate the mark afterward.\n\
7039 The command loop sets this to nil before each command,\n\
7040 and tests the value when the command returns.\n\
7041 Buffer modification stores t in this variable.");
7042 Vdeactivate_mark = Qnil;
7043
7044 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
7045 "Temporary storage of pre-command-hook or post-command-hook.");
7046 Vcommand_hook_internal = Qnil;
7047
7048 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
7049 "Normal hook run before each command is executed.\n\
7050 While the hook is run, its value is temporarily set to nil\n\
7051 to avoid an unbreakable infinite loop if a hook function gets an error.\n\
7052 As a result, a hook function cannot straightforwardly alter the value of\n\
7053 `pre-command-hook'. See the Emacs Lisp manual for a way of\n\
7054 implementing hook functions that alter the set of hook functions.");
7055 Vpre_command_hook = Qnil;
7056
7057 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
7058 "Normal hook run after each command is executed.\n\
7059 While the hook is run, its value is temporarily set to nil\n\
7060 to avoid an unbreakable infinite loop if a hook function gets an error.\n\
7061 As a result, a hook function cannot straightforwardly alter the value of\n\
7062 `post-command-hook'. See the Emacs Lisp manual for a way of\n\
7063 implementing hook functions that alter the set of hook functions.");
7064 Vpost_command_hook = Qnil;
7065
7066 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
7067 "t means menu bar, specified Lucid style, needs to be recomputed.");
7068 Vlucid_menu_bar_dirty_flag = Qnil;
7069
7070 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items,
7071 "List of menu bar items to move to the end of the menu bar.\n\
7072 The elements of the list are event types that may have menu bar bindings.");
7073 Vmenu_bar_final_items = Qnil;
7074
7075 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map,
7076 "Keymap that overrides all other local keymaps.\n\
7077 If this variable is non-nil, it is used as a keymap instead of the\n\
7078 buffer's local map, and the minor mode keymaps and text property keymaps.");
7079 Voverriding_local_map = Qnil;
7080
7081 DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag,
7082 "Non-nil means `overriding-local-map' applies to the menu bar.\n\
7083 Otherwise, the menu bar continues to reflect the buffer's local map\n\
7084 and the minor mode maps regardless of `overriding-local-map'.");
7085 Voverriding_local_map_menu_flag = Qnil;
7086
7087 DEFVAR_LISP ("track-mouse", &do_mouse_tracking,
7088 "*Non-nil means generate motion events for mouse motion.");
7089
7090 DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist,
7091 "Alist of system-specific X windows key symbols.\n\
7092 Each element should have the form (N . SYMBOL) where N is the\n\
7093 numeric keysym code (sans the \"system-specific\" bit 1<<28)\n\
7094 and SYMBOL is its name.");
7095
7096 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list,
7097 "List of deferred actions to be performed at a later time.\n\
7098 The precise format isn't relevant here; we just check whether it is nil.");
7099 Vdeferred_action_list = Qnil;
7100
7101 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function,
7102 "Function to call to handle deferred actions, after each command.\n\
7103 This function is called with no arguments after each command\n\
7104 whenever `deferred-action-list' is non-nil.");
7105 Vdeferred_action_function = Qnil;
7106 }
7107
7108 keys_of_keyboard ()
7109 {
7110 initial_define_key (global_map, Ctl ('Z'), "suspend-emacs");
7111 initial_define_key (control_x_map, Ctl ('Z'), "suspend-emacs");
7112 initial_define_key (meta_map, Ctl ('C'), "exit-recursive-edit");
7113 initial_define_key (global_map, Ctl (']'), "abort-recursive-edit");
7114 initial_define_key (meta_map, 'x', "execute-extended-command");
7115 }