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