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