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