1 /* Keyboard and mouse input; editor command loop.
2 Copyright (C) 1985,86,87,88,89,93,94,95 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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)
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.
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. */
20 /* Allow config.h to undefine symbols found here. */
28 #include "termhooks.h"
35 #include "dispextern.h"
37 #include "intervals.h"
38 #include "blockinput.h"
47 #include <sys/ioctl.h>
49 #endif /* not MSDOS */
51 #include "syssignal.h"
54 /* This is to get the definitions of the XK_ symbols. */
59 /* Include systime.h after xterm.h to avoid double inclusion of time.h. */
64 /* Variables for blockinput.h: */
66 /* Non-zero if interrupt input is blocked right now. */
67 int interrupt_input_blocked
;
69 /* Nonzero means an input interrupt has arrived
70 during the current critical section. */
71 int interrupt_input_pending
;
74 /* File descriptor to use for input. */
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 */
84 /* Following definition copied from eval.c */
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
98 KBOARD
*initial_kboard
;
99 KBOARD
*current_kboard
;
103 KBOARD the_only_kboard
;
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
;
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 */
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
;
123 /* Record values of this_command_key_count and echo_length ()
124 before this command was read. */
125 static int before_command_key_count
;
126 static int before_command_echo_length
;
127 /* Values of before_command_key_count and before_command_echo_length
128 saved by reset-this-command-lengths. */
129 static int before_command_key_count_1
;
130 static int before_command_echo_length_1
;
131 /* Flag set by reset-this-command-lengths,
132 saying to reset the lengths when add_command_key is called. */
133 static int before_command_restore_flag
;
135 extern int minbuf_level
;
137 extern struct backtrace
*backtrace_list
;
139 /* Nonzero means do menu prompting. */
140 static int menu_prompting
;
142 /* Character to see next line of menu prompt. */
143 static Lisp_Object menu_prompt_more_char
;
145 /* For longjmp to where kbd input is being done. */
146 static jmp_buf getcjmp
;
148 /* True while doing kbd input. */
149 int waiting_for_input
;
151 /* True while displaying for echoing. Delays C-g throwing. */
154 /* Nonzero means disregard local maps for the menu bar. */
155 static int inhibit_local_menu_bar_menus
;
157 /* Nonzero means C-g should cause immediate error-signal. */
160 /* Character to recognize as the help char. */
161 Lisp_Object Vhelp_char
;
163 /* List of other event types to recognize as meaning "help". */
164 Lisp_Object Vhelp_event_list
;
166 /* Form to execute when help char is typed. */
167 Lisp_Object Vhelp_form
;
169 /* Command to run when the help character follows a prefix key. */
170 Lisp_Object Vprefix_help_command
;
172 /* List of items that should move to the end of the menu bar. */
173 Lisp_Object Vmenu_bar_final_items
;
175 /* Character that causes a quit. Normally C-g.
177 If we are running on an ordinary terminal, this must be an ordinary
178 ASCII char, since we want to make it our interrupt character.
180 If we are not running on an ordinary terminal, it still needs to be
181 an ordinary ASCII char. This character needs to be recognized in
182 the input interrupt handler. At this point, the keystroke is
183 represented as a struct input_event, while the desired quit
184 character is specified as a lispy event. The mapping from struct
185 input_events to lispy events cannot run in an interrupt handler,
186 and the reverse mapping is difficult for anything but ASCII
189 FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
193 extern Lisp_Object current_global_map
;
194 extern int minibuf_level
;
196 /* If non-nil, this is a map that overrides all other local maps. */
197 Lisp_Object Voverriding_local_map
;
199 /* If non-nil, Voverriding_local_map applies to the menu bar. */
200 Lisp_Object Voverriding_local_map_menu_flag
;
202 /* Keymap that defines special misc events that should
203 be processed immediately at a low level. */
204 Lisp_Object Vspecial_event_map
;
206 /* Current depth in recursive edits. */
207 int command_loop_level
;
209 /* Total number of times command_loop has read a key sequence. */
212 /* Last input character read as a command. */
213 Lisp_Object last_command_char
;
215 /* Last input character read as a command, not counting menus
216 reached by the mouse. */
217 Lisp_Object last_nonmenu_event
;
219 /* Last input character read for any purpose. */
220 Lisp_Object last_input_char
;
222 /* If not Qnil, a list of objects to be read as subsequent command input. */
223 Lisp_Object Vunread_command_events
;
225 /* If not -1, an event to be read as subsequent command input. */
226 int unread_command_char
;
228 /* If not Qnil, this is a switch-frame event which we decided to put
229 off until the end of a key sequence. This should be read as the
230 next command input, after any unread_command_events.
232 read_key_sequence uses this to delay switch-frame events until the
233 end of the key sequence; Fread_char uses it to put off switch-frame
234 events until a non-ASCII event is acceptable as input. */
235 Lisp_Object unread_switch_frame
;
237 /* A mask of extra modifier bits to put into every keyboard char. */
238 int extra_keyboard_modifiers
;
240 /* Char to use as prefix when a meta character is typed in.
241 This is bound on entry to minibuffer in case ESC is changed there. */
243 Lisp_Object meta_prefix_char
;
245 /* Last size recorded for a current buffer which is not a minibuffer. */
246 static int last_non_minibuf_size
;
248 /* Number of idle seconds before an auto-save and garbage collection. */
249 static Lisp_Object Vauto_save_timeout
;
251 /* Total number of times read_char has returned. */
254 /* Total number of times read_char has returned, outside of macros. */
255 int num_nonmacro_input_chars
;
257 /* Auto-save automatically when this many characters have been typed
258 since the last time. */
260 static int auto_save_interval
;
262 /* Value of num_nonmacro_input_chars as of last auto save. */
266 /* The command being executed by the command loop.
267 Commands may set this, and the value set will be copied into
268 current_kboard->Vlast_command instead of the actual command. */
269 Lisp_Object this_command
;
271 /* The value of point when the last command was executed. */
272 int last_point_position
;
274 /* The buffer that was current when the last command was started. */
275 Lisp_Object last_point_position_buffer
;
278 /* The frame in which the last input event occurred, or Qmacro if the
279 last event came from a macro. We use this to determine when to
280 generate switch-frame events. This may be cleared by functions
281 like Fselect_frame, to make sure that a switch-frame event is
282 generated by the next character. */
283 Lisp_Object internal_last_event_frame
;
286 /* A user-visible version of the above, intended to allow users to
287 figure out where the last event came from, if the event doesn't
288 carry that information itself (i.e. if it was a character). */
289 Lisp_Object Vlast_event_frame
;
291 /* The timestamp of the last input event we received from the X server.
292 X Windows wants this for selection ownership. */
293 unsigned long last_event_timestamp
;
295 Lisp_Object Qself_insert_command
;
296 Lisp_Object Qforward_char
;
297 Lisp_Object Qbackward_char
;
298 Lisp_Object Qundefined
;
300 /* read_key_sequence stores here the command definition of the
301 key sequence that it reads. */
302 Lisp_Object read_key_sequence_cmd
;
304 /* Form to evaluate (if non-nil) when Emacs is started. */
305 Lisp_Object Vtop_level
;
307 /* User-supplied string to translate input characters through. */
308 Lisp_Object Vkeyboard_translate_table
;
310 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
311 extern Lisp_Object Vfunction_key_map
;
313 /* Keymap mapping ASCII function key sequences onto their preferred forms. */
314 Lisp_Object Vkey_translation_map
;
316 /* Non-nil means deactivate the mark at end of this command. */
317 Lisp_Object Vdeactivate_mark
;
319 /* Menu bar specified in Lucid Emacs fashion. */
321 Lisp_Object Vlucid_menu_bar_dirty_flag
;
322 Lisp_Object Qrecompute_lucid_menubar
, Qactivate_menubar_hook
;
324 /* Hooks to run before and after each command. */
325 Lisp_Object Qpre_command_hook
, Qpost_command_hook
;
326 Lisp_Object Vpre_command_hook
, Vpost_command_hook
;
327 Lisp_Object Qcommand_hook_internal
, Vcommand_hook_internal
;
329 /* List of deferred actions to be performed at a later time.
330 The precise format isn't relevant here; we just check whether it is nil. */
331 Lisp_Object Vdeferred_action_list
;
333 /* Function to call to handle deferred actions, when there are any. */
334 Lisp_Object Vdeferred_action_function
;
335 Lisp_Object Qdeferred_action_function
;
337 /* File in which we write all commands we read. */
340 /* Nonzero if input is available. */
343 /* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
344 keep 0200 bit in input chars. 0 to ignore the 0200 bit. */
348 extern char *pending_malloc_warning
;
350 /* Circular buffer for pre-read keyboard input. */
351 static struct input_event kbd_buffer
[KBD_BUFFER_SIZE
];
353 /* Vector to GCPRO the frames and windows mentioned in kbd_buffer.
355 The interrupt-level event handlers will never enqueue an event on a
356 frame which is not in Vframe_list, and once an event is dequeued,
357 internal_last_event_frame or the event itself points to the frame.
360 But while the event is sitting in the queue, it's completely
361 unprotected. Suppose the user types one command which will run for
362 a while and then delete a frame, and then types another event at
363 the frame that will be deleted, before the command gets around to
364 it. Suppose there are no references to this frame elsewhere in
365 Emacs, and a GC occurs before the second event is dequeued. Now we
366 have an event referring to a freed frame, which will crash Emacs
369 Similar things happen when an event on a scroll bar is enqueued; the
370 window may be deleted while the event is in the queue.
372 So, we use this vector to protect the frame_or_window field in the
373 event queue. That way, they'll be dequeued as dead frames or
374 windows, but still valid lisp objects.
376 If kbd_buffer[i].kind != no_event, then
377 (XVECTOR (kbd_buffer_frame_or_window)->contents[i]
378 == kbd_buffer[i].frame_or_window. */
379 static Lisp_Object kbd_buffer_frame_or_window
;
381 /* Pointer to next available character in kbd_buffer.
382 If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty.
383 This may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the the
384 next available char is in kbd_buffer[0]. */
385 static struct input_event
*kbd_fetch_ptr
;
387 /* Pointer to next place to store character in kbd_buffer. This
388 may be kbd_buffer + KBD_BUFFER_SIZE, meaning that the next
389 character should go in kbd_buffer[0]. */
390 static volatile struct input_event
*kbd_store_ptr
;
392 /* The above pair of variables forms a "queue empty" flag. When we
393 enqueue a non-hook event, we increment kbd_store_ptr. When we
394 dequeue a non-hook event, we increment kbd_fetch_ptr. We say that
395 there is input available iff the two pointers are not equal.
397 Why not just have a flag set and cleared by the enqueuing and
398 dequeuing functions? Such a flag could be screwed up by interrupts
399 at inopportune times. */
401 /* If this flag is non-nil, we check mouse_moved to see when the
402 mouse moves, and motion events will appear in the input stream.
403 Otherwise, mouse motion is ignored. */
404 static Lisp_Object do_mouse_tracking
;
406 /* Symbols to head events. */
407 Lisp_Object Qmouse_movement
;
408 Lisp_Object Qscroll_bar_movement
;
409 Lisp_Object Qswitch_frame
;
410 Lisp_Object Qdelete_frame
;
411 Lisp_Object Qiconify_frame
;
412 Lisp_Object Qmake_frame_visible
;
414 /* Symbols to denote kinds of events. */
415 Lisp_Object Qfunction_key
;
416 Lisp_Object Qmouse_click
;
417 /* Lisp_Object Qmouse_movement; - also an event header */
419 /* Properties of event headers. */
420 Lisp_Object Qevent_kind
;
421 Lisp_Object Qevent_symbol_elements
;
423 Lisp_Object Qmenu_enable
;
425 /* An event header symbol HEAD may have a property named
426 Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
427 BASE is the base, unmodified version of HEAD, and MODIFIERS is the
428 mask of modifiers applied to it. If present, this is used to help
429 speed up parse_modifiers. */
430 Lisp_Object Qevent_symbol_element_mask
;
432 /* An unmodified event header BASE may have a property named
433 Qmodifier_cache, which is an alist mapping modifier masks onto
434 modified versions of BASE. If present, this helps speed up
436 Lisp_Object Qmodifier_cache
;
438 /* Symbols to use for parts of windows. */
439 Lisp_Object Qmode_line
;
440 Lisp_Object Qvertical_line
;
441 Lisp_Object Qvertical_scroll_bar
;
442 Lisp_Object Qmenu_bar
;
444 extern Lisp_Object Qmenu_enable
;
446 Lisp_Object
recursive_edit_unwind (), command_loop ();
447 Lisp_Object
Fthis_command_keys ();
448 Lisp_Object Qextended_command_history
;
450 extern char *x_get_keysym_name ();
452 Lisp_Object Qpolling_period
;
454 extern Lisp_Object Vprint_level
, Vprint_length
;
456 /* Address (if not 0) of EMACS_TIME to zero out if a SIGIO interrupt
458 EMACS_TIME
*input_available_clear_time
;
460 /* Nonzero means use SIGIO interrupts; zero means use CBREAK mode.
461 Default is 1 if INTERRUPT_INPUT is defined. */
464 /* Nonzero while interrupts are temporarily deferred during redisplay. */
465 int interrupts_deferred
;
467 /* nonzero means use ^S/^Q for flow control. */
470 /* Allow m- file to inhibit use of FIONREAD. */
471 #ifdef BROKEN_FIONREAD
475 /* We are unable to use interrupts if FIONREAD is not available,
476 so flush SIGIO so we won't try. */
483 /* If we support X Windows, turn on the code to poll periodically
484 to detect C-g. It isn't actually used when doing interrupt input. */
485 #ifdef HAVE_X_WINDOWS
486 #define POLL_FOR_INPUT
489 /* Global variable declarations. */
491 /* Function for init_keyboard to call with no args (if nonzero). */
492 void (*keyboard_init_hook
) ();
494 static int read_avail_input ();
495 static void get_input_pending ();
496 static int readable_events ();
497 static Lisp_Object
read_char_x_menu_prompt ();
498 static Lisp_Object
read_char_minibuf_menu_prompt ();
499 static Lisp_Object
make_lispy_event ();
501 static Lisp_Object
make_lispy_movement ();
503 static Lisp_Object
modify_event_symbol ();
504 static Lisp_Object
make_lispy_switch_frame ();
505 static int parse_solitary_modifier ();
507 /* > 0 if we are to echo keystrokes. */
508 static int echo_keystrokes
;
510 /* Nonzero means don't try to suspend even if the operating system seems
512 static int cannot_suspend
;
514 #define min(a,b) ((a)<(b)?(a):(b))
515 #define max(a,b) ((a)>(b)?(a):(b))
517 /* Install the string STR as the beginning of the string of echoing,
518 so that it serves as a prompt for the next character.
519 Also start echoing. */
524 int len
= strlen (str
);
526 if (len
> ECHOBUFSIZE
- 4)
527 len
= ECHOBUFSIZE
- 4;
528 bcopy (str
, current_kboard
->echobuf
, len
);
529 current_kboard
->echoptr
= current_kboard
->echobuf
+ len
;
530 *current_kboard
->echoptr
= '\0';
532 current_kboard
->echo_after_prompt
= len
;
537 /* Add C to the echo string, if echoing is going on.
538 C can be a character, which is printed prettily ("M-C-x" and all that
539 jazz), or a symbol, whose name is printed. */
544 extern char *push_key_description ();
546 if (current_kboard
->immediate_echo
)
548 char *ptr
= current_kboard
->echoptr
;
550 if (ptr
!= current_kboard
->echobuf
)
553 /* If someone has passed us a composite event, use its head symbol. */
558 if (ptr
- current_kboard
->echobuf
> ECHOBUFSIZE
- 6)
561 ptr
= push_key_description (XINT (c
), ptr
);
563 else if (SYMBOLP (c
))
565 struct Lisp_String
*name
= XSYMBOL (c
)->name
;
566 if ((ptr
- current_kboard
->echobuf
) + name
->size
+ 4 > ECHOBUFSIZE
)
568 bcopy (name
->data
, ptr
, name
->size
);
572 if (current_kboard
->echoptr
== current_kboard
->echobuf
575 strcpy (ptr
, " (Type ? for further options)");
580 current_kboard
->echoptr
= ptr
;
586 /* Temporarily add a dash to the end of the echo string if it's not
587 empty, so that it serves as a mini-prompt for the very next character. */
591 if (!current_kboard
->immediate_echo
592 && current_kboard
->echoptr
== current_kboard
->echobuf
)
594 /* Do nothing if we just printed a prompt. */
595 if (current_kboard
->echo_after_prompt
596 == current_kboard
->echoptr
- current_kboard
->echobuf
)
598 /* Do nothing if not echoing at all. */
599 if (current_kboard
->echoptr
== 0)
602 /* Put a dash at the end of the buffer temporarily,
603 but make it go away when the next character is added. */
604 current_kboard
->echoptr
[0] = '-';
605 current_kboard
->echoptr
[1] = 0;
610 /* Display the current echo string, and begin echoing if not already
615 if (!current_kboard
->immediate_echo
)
618 current_kboard
->immediate_echo
= 1;
620 for (i
= 0; i
< this_command_key_count
; i
++)
623 c
= XVECTOR (this_command_keys
)->contents
[i
];
624 if (! (EVENT_HAS_PARAMETERS (c
)
625 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c
)), Qmouse_movement
)))
632 message1_nolog (current_kboard
->echobuf
);
635 if (waiting_for_input
&& !NILP (Vquit_flag
))
636 quit_throw_to_read_char ();
639 /* Turn off echoing, for the start of a new command. */
643 current_kboard
->immediate_echo
= 0;
644 current_kboard
->echoptr
= current_kboard
->echobuf
;
645 current_kboard
->echo_after_prompt
= -1;
648 /* Return the length of the current echo string. */
653 return current_kboard
->echoptr
- current_kboard
->echobuf
;
656 /* Truncate the current echo message to its first LEN chars.
657 This and echo_char get used by read_key_sequence when the user
658 switches frames while entering a key sequence. */
664 current_kboard
->echobuf
[len
] = '\0';
665 current_kboard
->echoptr
= current_kboard
->echobuf
+ len
;
666 truncate_echo_area (len
);
670 /* Functions for manipulating this_command_keys. */
672 add_command_key (key
)
675 int size
= XVECTOR (this_command_keys
)->size
;
677 /* If reset-this-command-length was called recently, obey it now.
678 See the doc string of that function for an explanation of why. */
679 if (before_command_restore_flag
)
681 this_command_key_count
= before_command_key_count_1
;
682 echo_truncate (before_command_echo_length_1
);
683 before_command_restore_flag
= 0;
686 if (this_command_key_count
>= size
)
688 Lisp_Object new_keys
;
690 new_keys
= Fmake_vector (make_number (size
* 2), Qnil
);
691 bcopy (XVECTOR (this_command_keys
)->contents
,
692 XVECTOR (new_keys
)->contents
,
693 size
* sizeof (Lisp_Object
));
695 this_command_keys
= new_keys
;
698 XVECTOR (this_command_keys
)->contents
[this_command_key_count
++] = key
;
704 int count
= specpdl_ptr
- specpdl
;
707 if (command_loop_level
> 0)
709 specbind (Qstandard_output
, Qt
);
710 specbind (Qstandard_input
, Qt
);
713 val
= command_loop ();
715 Fsignal (Qquit
, Qnil
);
717 return unbind_to (count
, Qnil
);
720 /* When an auto-save happens, record the "time", and don't do again soon. */
724 last_auto_save
= num_nonmacro_input_chars
;
727 /* Make an auto save happen as soon as possible at command level. */
729 force_auto_save_soon ()
731 last_auto_save
= - auto_save_interval
- 1;
733 record_asynch_buffer_change ();
736 DEFUN ("recursive-edit", Frecursive_edit
, Srecursive_edit
, 0, 0, "",
737 "Invoke the editor command loop recursively.\n\
738 To get out of the recursive edit, a command can do `(throw 'exit nil)';\n\
739 that tells this function to return.\n\
740 Alternately, `(throw 'exit t)' makes this function signal an error.\n\
741 This function is called by the editor initialization to begin editing.")
744 int count
= specpdl_ptr
- specpdl
;
747 command_loop_level
++;
748 update_mode_lines
= 1;
750 record_unwind_protect (recursive_edit_unwind
,
752 && current_buffer
!= XBUFFER (XWINDOW (selected_window
)->buffer
))
756 return unbind_to (count
, Qnil
);
760 recursive_edit_unwind (buffer
)
764 Fset_buffer (buffer
);
766 command_loop_level
--;
767 update_mode_lines
= 1;
775 #if 0 /* Theory: if there's anything in Vunread_command_events,
776 it will right away be read by read_key_sequence,
777 and then if we do switch KBOARDS, it will go into the side
778 queue then. So we don't need to do anything special here -- rms. */
779 if (CONSP (Vunread_command_events
))
781 current_kboard
->kbd_queue
782 = nconc2 (Vunread_command_events
, current_kboard
->kbd_queue
);
783 current_kboard
->kbd_queue_has_data
= 1;
785 Vunread_command_events
= Qnil
;
791 /* Switch to the single-kboard state, making current_kboard
792 the only KBOARD from which further input is accepted. */
795 single_kboard_state ()
802 /* Maintain a stack of kboards, so other parts of Emacs
803 can switch temporarily to the kboard of a given frame
804 and then revert to the previous status. */
809 struct kboard_stack
*next
;
812 static struct kboard_stack
*kboard_stack
;
815 push_frame_kboard (f
)
819 struct kboard_stack
*p
820 = (struct kboard_stack
*) xmalloc (sizeof (struct kboard_stack
));
822 p
->next
= kboard_stack
;
823 p
->kboard
= current_kboard
;
826 current_kboard
= FRAME_KBOARD (f
);
834 struct kboard_stack
*p
= kboard_stack
;
835 current_kboard
= p
->kboard
;
836 kboard_stack
= p
->next
;
841 /* Handle errors that are not handled at inner levels
842 by printing an error message and returning to the editor command loop. */
848 Lisp_Object old_level
, old_length
;
850 Vstandard_output
= Qt
;
851 Vstandard_input
= Qt
;
852 Vexecuting_macro
= Qnil
;
853 current_kboard
->Vprefix_arg
= Qnil
;
856 /* Avoid unquittable loop if data contains a circular list. */
857 old_level
= Vprint_level
;
858 old_length
= Vprint_length
;
859 XSETFASTINT(Vprint_level
, 10);
860 XSETFASTINT(Vprint_length
, 10);
861 cmd_error_internal (data
, NULL
);
862 Vprint_level
= old_level
;
863 Vprint_length
= old_length
;
867 Vinhibit_quit
= Qnil
;
872 return make_number (0);
875 cmd_error_internal (data
, context
)
879 Lisp_Object errmsg
, tail
, errname
, file_error
;
886 echo_area_glyphs
= 0;
888 /* If the window system or terminal frame hasn't been initialized
889 yet, or we're not interactive, it's best to dump this message out
890 to stderr and exit. */
891 if (! FRAME_MESSAGE_BUF (selected_frame
)
893 stream
= Qexternal_debugging_output
;
902 write_string_1 (context
, -1, stream
);
904 errname
= Fcar (data
);
906 if (EQ (errname
, Qerror
))
909 if (!CONSP (data
)) data
= Qnil
;
910 errmsg
= Fcar (data
);
915 errmsg
= Fget (errname
, Qerror_message
);
916 file_error
= Fmemq (Qfile_error
,
917 Fget (errname
, Qerror_conditions
));
920 /* Print an error message including the data items.
921 This is done by printing it into a scratch buffer
922 and then making a copy of the text in the buffer. */
924 if (!CONSP (data
)) data
= Qnil
;
928 /* For file-error, make error message by concatenating
929 all the data items. They are all strings. */
930 if (!NILP (file_error
) && !NILP (tail
))
931 errmsg
= XCONS (tail
)->car
, tail
= XCONS (tail
)->cdr
;
933 if (STRINGP (errmsg
))
934 Fprinc (errmsg
, stream
);
936 write_string_1 ("peculiar error", -1, stream
);
938 for (i
= 0; CONSP (tail
); tail
= Fcdr (tail
), i
++)
940 write_string_1 (i
? ", " : ": ", 2, stream
);
941 if (!NILP (file_error
))
942 Fprinc (Fcar (tail
), stream
);
944 Fprin1 (Fcar (tail
), stream
);
948 /* If the window system or terminal frame hasn't been initialized
949 yet, or we're in -batch mode, this error should cause Emacs to exit. */
950 if (! FRAME_MESSAGE_BUF (selected_frame
)
954 Fkill_emacs (make_number (-1));
958 Lisp_Object
command_loop_1 ();
959 Lisp_Object
command_loop_2 ();
960 Lisp_Object
top_level_1 ();
962 /* Entry to editor-command-loop.
963 This level has the catches for exiting/returning to editor command loop.
964 It returns nil to exit recursive edit, t to abort it. */
969 if (command_loop_level
> 0 || minibuf_level
> 0)
971 return internal_catch (Qexit
, command_loop_2
, Qnil
);
976 internal_catch (Qtop_level
, top_level_1
, Qnil
);
977 internal_catch (Qtop_level
, command_loop_2
, Qnil
);
979 /* End of file in -batch run causes exit here. */
985 /* Here we catch errors in execution of commands within the
986 editing loop, and reenter the editing loop.
987 When there is an error, cmd_error runs and returns a non-nil
988 value to us. A value of nil means that cmd_loop_1 itself
989 returned due to end of file (or end of kbd macro). */
994 register Lisp_Object val
;
997 val
= internal_condition_case (command_loop_1
, Qerror
, cmd_error
);
1006 return Feval (Vtop_level
);
1012 /* On entry to the outer level, run the startup file */
1013 if (!NILP (Vtop_level
))
1014 internal_condition_case (top_level_2
, Qerror
, cmd_error
);
1015 else if (!NILP (Vpurify_flag
))
1016 message ("Bare impure Emacs (standard Lisp code not loaded)");
1018 message ("Bare Emacs (standard Lisp code not loaded)");
1022 DEFUN ("top-level", Ftop_level
, Stop_level
, 0, 0, "",
1023 "Exit all recursive editing levels.")
1026 Fthrow (Qtop_level
, Qnil
);
1029 DEFUN ("exit-recursive-edit", Fexit_recursive_edit
, Sexit_recursive_edit
, 0, 0, "",
1030 "Exit from the innermost recursive edit or minibuffer.")
1033 if (command_loop_level
> 0 || minibuf_level
> 0)
1034 Fthrow (Qexit
, Qnil
);
1036 error ("No recursive edit is in progress");
1039 DEFUN ("abort-recursive-edit", Fabort_recursive_edit
, Sabort_recursive_edit
, 0, 0, "",
1040 "Abort the command that requested this recursive edit or minibuffer input.")
1043 if (command_loop_level
> 0 || minibuf_level
> 0)
1046 error ("No recursive edit is in progress");
1049 /* This is the actual command reading loop,
1050 sans error-handling encapsulation. */
1052 Lisp_Object
Fcommand_execute ();
1053 static int read_key_sequence ();
1054 static void safe_run_hooks ();
1059 Lisp_Object cmd
, tem
;
1062 Lisp_Object keybuf
[30];
1067 struct buffer
*prev_buffer
;
1069 int was_locked
= single_kboard
;
1072 current_kboard
->Vprefix_arg
= Qnil
;
1073 Vdeactivate_mark
= Qnil
;
1074 waiting_for_input
= 0;
1079 this_command_key_count
= 0;
1081 /* Make sure this hook runs after commands that get errors and
1082 throw to top level. */
1083 /* Note that the value cell will never directly contain nil
1084 if the symbol is a local variable. */
1085 if (!NILP (XSYMBOL (Qpost_command_hook
)->value
) && !NILP (Vrun_hooks
))
1086 safe_run_hooks (Qpost_command_hook
);
1088 if (!NILP (Vdeferred_action_list
))
1089 call0 (Vdeferred_action_function
);
1091 /* Do this after running Vpost_command_hook, for consistency. */
1092 current_kboard
->Vlast_command
= this_command
;
1096 /* Make sure the current window's buffer is selected. */
1097 if (XBUFFER (XWINDOW (selected_window
)->buffer
) != current_buffer
)
1098 set_buffer_internal (XBUFFER (XWINDOW (selected_window
)->buffer
));
1100 /* Display any malloc warning that just came out. Use while because
1101 displaying one warning can cause another. */
1103 while (pending_malloc_warning
)
1104 display_malloc_warning ();
1108 Vdeactivate_mark
= Qnil
;
1110 /* If minibuffer on and echo area in use,
1111 wait 2 sec and redraw minibuffer. */
1113 if (minibuf_level
&& echo_area_glyphs
1114 && EQ (minibuf_window
, echo_area_window
))
1116 /* Bind inhibit-quit to t so that C-g gets read in
1117 rather than quitting back to the minibuffer. */
1118 int count
= specpdl_ptr
- specpdl
;
1119 specbind (Qinhibit_quit
, Qt
);
1120 Fsit_for (make_number (2), Qnil
, Qnil
);
1121 unbind_to (count
, Qnil
);
1123 echo_area_glyphs
= 0;
1125 if (!NILP (Vquit_flag
))
1128 Vunread_command_events
= Fcons (make_number (quit_char
), Qnil
);
1133 alloca (0); /* Cause a garbage collection now */
1134 /* Since we can free the most stuff here. */
1135 #endif /* C_ALLOCA */
1139 /* Select the frame that the last event came from. Usually,
1140 switch-frame events will take care of this, but if some lisp
1141 code swallows a switch-frame event, we'll fix things up here.
1142 Is this a good idea? */
1143 if (FRAMEP (internal_last_event_frame
)
1144 && XFRAME (internal_last_event_frame
) != selected_frame
)
1145 Fselect_frame (internal_last_event_frame
, Qnil
);
1148 /* If it has changed current-menubar from previous value,
1149 really recompute the menubar from the value. */
1150 if (! NILP (Vlucid_menu_bar_dirty_flag
)
1151 && !NILP (Ffboundp (Qrecompute_lucid_menubar
)))
1152 call0 (Qrecompute_lucid_menubar
);
1154 before_command_key_count
= this_command_key_count
;
1155 before_command_echo_length
= echo_length ();
1157 /* Read next key sequence; i gets its length. */
1158 i
= read_key_sequence (keybuf
, sizeof keybuf
/ sizeof keybuf
[0],
1163 /* Now we have read a key sequence of length I,
1164 or else I is 0 and we found end of file. */
1166 if (i
== 0) /* End of file -- happens only in */
1167 return Qnil
; /* a kbd macro, at the end. */
1168 /* -1 means read_key_sequence got a menu that was rejected.
1169 Just loop around and read another command. */
1173 this_command_key_count
= 0;
1177 last_command_char
= keybuf
[i
- 1];
1179 /* If the previous command tried to force a specific window-start,
1180 forget about that, in case this command moves point far away
1181 from that position. But also throw away beg_unchanged and
1182 end_unchanged information in that case, so that redisplay will
1183 update the whole window properly. */
1184 if (!NILP (XWINDOW (selected_window
)->force_start
))
1186 XWINDOW (selected_window
)->force_start
= Qnil
;
1187 beg_unchanged
= end_unchanged
= 0;
1190 cmd
= read_key_sequence_cmd
;
1191 if (!NILP (Vexecuting_macro
))
1193 if (!NILP (Vquit_flag
))
1195 Vexecuting_macro
= Qt
;
1196 QUIT
; /* Make some noise. */
1197 /* Will return since macro now empty. */
1201 /* Do redisplay processing after this command except in special
1202 cases identified below that set no_redisplay to 1.
1203 (actually, there's currently no way to prevent the redisplay,
1204 and no_redisplay is ignored.
1205 Perhaps someday we will really implement it.) */
1208 prev_buffer
= current_buffer
;
1209 prev_modiff
= MODIFF
;
1210 last_point_position
= PT
;
1211 XSETBUFFER (last_point_position_buffer
, prev_buffer
);
1213 /* Execute the command. */
1216 /* Note that the value cell will never directly contain nil
1217 if the symbol is a local variable. */
1218 if (!NILP (XSYMBOL (Qpre_command_hook
)->value
) && !NILP (Vrun_hooks
))
1219 safe_run_hooks (Qpre_command_hook
);
1221 if (NILP (this_command
))
1223 /* nil means key is undefined. */
1225 current_kboard
->defining_kbd_macro
= Qnil
;
1226 update_mode_lines
= 1;
1227 current_kboard
->Vprefix_arg
= Qnil
;
1231 if (NILP (current_kboard
->Vprefix_arg
) && ! no_direct
)
1233 /* Recognize some common commands in common situations and
1234 do them directly. */
1235 if (EQ (this_command
, Qforward_char
) && PT
< ZV
)
1237 struct Lisp_Vector
*dp
1238 = window_display_table (XWINDOW (selected_window
));
1239 lose
= FETCH_CHAR (PT
);
1242 ? (VECTORP (DISP_CHAR_VECTOR (dp
, lose
))
1243 ? XVECTOR (DISP_CHAR_VECTOR (dp
, lose
))->size
== 1
1244 : (NILP (DISP_CHAR_VECTOR (dp
, lose
))
1245 && (lose
>= 0x20 && lose
< 0x7f)))
1246 : (lose
>= 0x20 && lose
< 0x7f))
1247 && (XFASTINT (XWINDOW (selected_window
)->last_modified
)
1249 && (XFASTINT (XWINDOW (selected_window
)->last_point
)
1251 && !windows_or_buffers_changed
1252 && EQ (current_buffer
->selective_display
, Qnil
)
1253 && !detect_input_pending ()
1254 && NILP (Vexecuting_macro
))
1255 no_redisplay
= direct_output_forward_char (1);
1258 else if (EQ (this_command
, Qbackward_char
) && PT
> BEGV
)
1260 struct Lisp_Vector
*dp
1261 = window_display_table (XWINDOW (selected_window
));
1263 lose
= FETCH_CHAR (PT
);
1265 ? (VECTORP (DISP_CHAR_VECTOR (dp
, lose
))
1266 ? XVECTOR (DISP_CHAR_VECTOR (dp
, lose
))->size
== 1
1267 : (NILP (DISP_CHAR_VECTOR (dp
, lose
))
1268 && (lose
>= 0x20 && lose
< 0x7f)))
1269 : (lose
>= 0x20 && lose
< 0x7f))
1270 && (XFASTINT (XWINDOW (selected_window
)->last_modified
)
1272 && (XFASTINT (XWINDOW (selected_window
)->last_point
)
1274 && !windows_or_buffers_changed
1275 && EQ (current_buffer
->selective_display
, Qnil
)
1276 && !detect_input_pending ()
1277 && NILP (Vexecuting_macro
))
1278 no_redisplay
= direct_output_forward_char (-1);
1281 else if (EQ (this_command
, Qself_insert_command
)
1282 /* Try this optimization only on ascii keystrokes. */
1283 && INTEGERP (last_command_char
))
1285 unsigned char c
= XINT (last_command_char
);
1288 if (NILP (Vexecuting_macro
)
1289 && !EQ (minibuf_window
, selected_window
))
1291 if (!nonundocount
|| nonundocount
>= 20)
1298 lose
= ((XFASTINT (XWINDOW (selected_window
)->last_modified
)
1300 || (XFASTINT (XWINDOW (selected_window
)->last_point
)
1302 || MODIFF
<= SAVE_MODIFF
1303 || windows_or_buffers_changed
1304 || !EQ (current_buffer
->selective_display
, Qnil
)
1305 || detect_input_pending ()
1306 || !NILP (Vexecuting_macro
));
1307 value
= internal_self_insert (c
, 0);
1314 && (PT
== ZV
|| FETCH_CHAR (PT
) == '\n'))
1316 struct Lisp_Vector
*dp
1317 = window_display_table (XWINDOW (selected_window
));
1324 obj
= DISP_CHAR_VECTOR (dp
, lose
);
1327 /* Do it only for char codes
1328 that by default display as themselves. */
1329 if (lose
>= 0x20 && lose
<= 0x7e)
1330 no_redisplay
= direct_output_for_insert (lose
);
1332 else if (VECTORP (obj
)
1333 && XVECTOR (obj
)->size
== 1
1334 && (obj
= XVECTOR (obj
)->contents
[0],
1336 /* Insist face not specified in glyph. */
1337 && (XINT (obj
) & ((-1) << 8)) == 0)
1339 = direct_output_for_insert (XINT (obj
));
1343 if (lose
>= 0x20 && lose
<= 0x7e)
1344 no_redisplay
= direct_output_for_insert (lose
);
1351 /* Here for a command that isn't executed directly */
1354 if (NILP (current_kboard
->Vprefix_arg
))
1356 Fcommand_execute (this_command
, Qnil
);
1361 /* Note that the value cell will never directly contain nil
1362 if the symbol is a local variable. */
1363 if (!NILP (XSYMBOL (Qpost_command_hook
)->value
) && !NILP (Vrun_hooks
))
1364 safe_run_hooks (Qpost_command_hook
);
1366 if (!NILP (Vdeferred_action_list
))
1367 safe_run_hooks (Qdeferred_action_function
);
1369 /* If there is a prefix argument,
1370 1) We don't want Vlast_command to be ``universal-argument''
1371 (that would be dumb), so don't set Vlast_command,
1372 2) we want to leave echoing on so that the prefix will be
1373 echoed as part of this key sequence, so don't call
1375 3) we want to leave this_command_key_count non-zero, so that
1376 read_char will realize that it is re-reading a character, and
1377 not echo it a second time.
1379 If the command didn't actually create a prefix arg,
1380 but is merely a frame event that is transparent to prefix args,
1381 then the above doesn't apply. */
1382 if (NILP (current_kboard
->Vprefix_arg
) || CONSP (last_command_char
))
1384 current_kboard
->Vlast_command
= this_command
;
1386 this_command_key_count
= 0;
1389 if (!NILP (current_buffer
->mark_active
) && !NILP (Vrun_hooks
))
1391 if (!NILP (Vdeactivate_mark
) && !NILP (Vtransient_mark_mode
))
1393 current_buffer
->mark_active
= Qnil
;
1394 call1 (Vrun_hooks
, intern ("deactivate-mark-hook"));
1396 else if (current_buffer
!= prev_buffer
|| MODIFF
!= prev_modiff
)
1397 call1 (Vrun_hooks
, intern ("activate-mark-hook"));
1401 /* Install chars successfully executed in kbd macro. */
1403 if (!NILP (current_kboard
->defining_kbd_macro
)
1404 && NILP (current_kboard
->Vprefix_arg
))
1405 finalize_kbd_macro_chars ();
1409 any_kboard_state ();
1414 /* Subroutine for safe_run_hooks: run the hook HOOK. */
1417 safe_run_hooks_1 (hook
)
1420 return call1 (Vrun_hooks
, Vinhibit_quit
);
1423 /* Subroutine for safe_run_hooks: handle an error by clearing out the hook. */
1426 safe_run_hooks_error (data
)
1429 Fset (Vinhibit_quit
, Qnil
);
1432 /* If we get an error while running the hook, cause the hook variable
1433 to be nil. Also inhibit quits, so that C-g won't cause the hook
1434 to mysteriously evaporate. */
1437 safe_run_hooks (hook
)
1441 int count
= specpdl_ptr
- specpdl
;
1442 specbind (Qinhibit_quit
, hook
);
1444 internal_condition_case (safe_run_hooks_1
, Qerror
, safe_run_hooks_error
);
1446 unbind_to (count
, Qnil
);
1449 /* Number of seconds between polling for input. */
1452 /* Nonzero means polling for input is temporarily suppressed. */
1453 int poll_suppress_count
;
1455 /* Nonzero if polling_for_input is actually being used. */
1456 int polling_for_input
;
1458 #ifdef POLL_FOR_INPUT
1460 /* Handle an alarm once each second and read pending input
1461 so as to handle a C-g if it comces in. */
1464 input_poll_signal (signalnum
) /* If we don't have an argument, */
1465 int signalnum
; /* some compilers complain in signal calls. */
1467 if (interrupt_input_blocked
== 0
1468 && !waiting_for_input
)
1469 read_avail_input (0);
1470 signal (SIGALRM
, input_poll_signal
);
1471 alarm (polling_period
);
1476 /* Begin signals to poll for input, if they are appropriate.
1477 This function is called unconditionally from various places. */
1481 #ifdef POLL_FOR_INPUT
1482 if (read_socket_hook
&& !interrupt_input
)
1484 poll_suppress_count
--;
1485 if (poll_suppress_count
== 0)
1487 signal (SIGALRM
, input_poll_signal
);
1488 polling_for_input
= 1;
1489 alarm (polling_period
);
1495 /* Nonzero if we are using polling to handle input asynchronously. */
1498 input_polling_used ()
1500 #ifdef POLL_FOR_INPUT
1501 return read_socket_hook
&& !interrupt_input
;
1507 /* Turn off polling. */
1511 #ifdef POLL_FOR_INPUT
1512 if (read_socket_hook
&& !interrupt_input
)
1514 if (poll_suppress_count
== 0)
1516 polling_for_input
= 0;
1519 poll_suppress_count
++;
1524 /* Set the value of poll_suppress_count to COUNT
1525 and start or stop polling accordingly. */
1528 set_poll_suppress_count (count
)
1531 #ifdef POLL_FOR_INPUT
1532 if (count
== 0 && poll_suppress_count
!= 0)
1534 poll_suppress_count
= 1;
1537 else if (count
!= 0 && poll_suppress_count
== 0)
1541 poll_suppress_count
= count
;
1545 /* Bind polling_period to a value at least N.
1546 But don't decrease it. */
1548 bind_polling_period (n
)
1551 #ifdef POLL_FOR_INPUT
1552 int new = polling_period
;
1558 specbind (Qpolling_period
, make_number (new));
1559 /* Start a new alarm with the new period. */
1564 /* Apply the control modifier to CHARACTER. */
1570 /* Save the upper bits here. */
1571 int upper
= c
& ~0177;
1575 /* Everything in the columns containing the upper-case letters
1576 denotes a control character. */
1577 if (c
>= 0100 && c
< 0140)
1581 /* Set the shift modifier for a control char
1582 made from a shifted letter. But only for letters! */
1583 if (oc
>= 'A' && oc
<= 'Z')
1584 c
|= shift_modifier
;
1587 /* The lower-case letters denote control characters too. */
1588 else if (c
>= 'a' && c
<= 'z')
1591 /* Include the bits for control and shift
1592 only if the basic ASCII code can't indicate them. */
1596 /* Replace the high bits. */
1597 c
|= (upper
& ~ctrl_modifier
);
1604 /* Input of single characters from keyboard */
1606 Lisp_Object
print_help ();
1607 static Lisp_Object
kbd_buffer_get_event ();
1608 static void record_char ();
1611 static jmp_buf wrong_kboard_jmpbuf
;
1614 /* read a character from the keyboard; call the redisplay if needed */
1615 /* commandflag 0 means do not do auto-saving, but do do redisplay.
1616 -1 means do not do redisplay, but do do autosaving.
1619 /* The arguments MAPS and NMAPS are for menu prompting.
1620 MAPS is an array of keymaps; NMAPS is the length of MAPS.
1622 PREV_EVENT is the previous input event, or nil if we are reading
1623 the first event of a key sequence.
1625 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
1626 if we used a mouse menu to read the input, or zero otherwise. If
1627 USED_MOUSE_MENU is null, we don't dereference it.
1629 Value is t if we showed a menu and the user rejected it. */
1632 read_char (commandflag
, nmaps
, maps
, prev_event
, used_mouse_menu
)
1636 Lisp_Object prev_event
;
1637 int *used_mouse_menu
;
1639 register Lisp_Object c
;
1641 jmp_buf local_getcjmp
;
1643 int key_already_recorded
= 0;
1644 Lisp_Object tem
, save
;
1645 Lisp_Object also_record
;
1648 before_command_key_count
= this_command_key_count
;
1649 before_command_echo_length
= echo_length ();
1653 if (CONSP (Vunread_command_events
))
1655 c
= XCONS (Vunread_command_events
)->car
;
1656 Vunread_command_events
= XCONS (Vunread_command_events
)->cdr
;
1658 if (this_command_key_count
== 0)
1664 if (unread_command_char
!= -1)
1666 XSETINT (c
, unread_command_char
);
1667 unread_command_char
= -1;
1669 if (this_command_key_count
== 0)
1675 /* If there is no function key translated before
1676 reset-this-command-lengths takes effect, forget about it. */
1677 before_command_restore_flag
= 0;
1679 if (!NILP (Vexecuting_macro
))
1682 /* We set this to Qmacro; since that's not a frame, nobody will
1683 try to switch frames on us, and the selected window will
1686 Since this event came from a macro, it would be misleading to
1687 leave internal_last_event_frame set to wherever the last
1688 real event came from. Normally, a switch-frame event selects
1689 internal_last_event_frame after each command is read, but
1690 events read from a macro should never cause a new frame to be
1692 Vlast_event_frame
= internal_last_event_frame
= Qmacro
;
1695 /* Exit the macro if we are at the end.
1696 Also, some things replace the macro with t
1697 to force an early exit. */
1698 if (EQ (Vexecuting_macro
, Qt
)
1699 || executing_macro_index
>= XFASTINT (Flength (Vexecuting_macro
)))
1705 c
= Faref (Vexecuting_macro
, make_number (executing_macro_index
));
1706 if (STRINGP (Vexecuting_macro
)
1707 && (XINT (c
) & 0x80))
1708 XSETFASTINT (c
, CHAR_META
| (XINT (c
) & ~0x80));
1710 executing_macro_index
++;
1715 if (!NILP (unread_switch_frame
))
1717 c
= unread_switch_frame
;
1718 unread_switch_frame
= Qnil
;
1720 /* This event should make it into this_command_keys, and get echoed
1721 again, so we go to reread_first, rather than reread. */
1725 if (commandflag
>= 0 && !input_pending
&& !detect_input_pending ())
1728 /* Message turns off echoing unless more keystrokes turn it on again. */
1729 if (echo_area_glyphs
&& *echo_area_glyphs
1730 && echo_area_glyphs
!= current_kboard
->echobuf
)
1733 /* If already echoing, continue. */
1736 /* Try reading a character via menu prompting in the minibuf.
1737 Try this before the sit-for, because the sit-for
1738 would do the wrong thing if we are supposed to do
1739 menu prompting. If EVENT_HAS_PARAMETERS then we are reading
1740 after a mouse event so don't try a minibuf menu. */
1742 if (nmaps
> 0 && INTERACTIVE
1743 && !NILP (prev_event
) && ! EVENT_HAS_PARAMETERS (prev_event
)
1744 /* Don't bring up a menu if we already have another event. */
1745 && NILP (Vunread_command_events
)
1746 && unread_command_char
< 0
1747 && !detect_input_pending ())
1749 c
= read_char_minibuf_menu_prompt (commandflag
, nmaps
, maps
);
1752 key_already_recorded
= 1;
1757 /* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
1758 We will do that below, temporarily for short sections of code,
1759 when appropriate. local_getcjmp must be in effect
1760 around any call to sit_for or kbd_buffer_get_event;
1761 it *must not* be in effect when we call redisplay. */
1763 if (_setjmp (local_getcjmp
))
1765 XSETINT (c
, quit_char
);
1767 XSETFRAME (internal_last_event_frame
, selected_frame
);
1768 Vlast_event_frame
= internal_last_event_frame
;
1770 /* If we report the quit char as an event,
1771 don't do so more than once. */
1772 if (!NILP (Vinhibit_quit
))
1777 KBOARD
*kb
= FRAME_KBOARD (selected_frame
);
1778 if (kb
!= current_kboard
)
1780 Lisp_Object
*tailp
= &kb
->kbd_queue
;
1781 /* We shouldn't get here if we were in single-kboard mode! */
1784 while (CONSP (*tailp
))
1785 tailp
= &XCONS (*tailp
)->cdr
;
1788 *tailp
= Fcons (c
, Qnil
);
1789 kb
->kbd_queue_has_data
= 1;
1790 current_kboard
= kb
;
1791 longjmp (wrong_kboard_jmpbuf
, 1);
1798 /* If in middle of key sequence and minibuffer not active,
1799 start echoing if enough time elapses. */
1801 if (minibuf_level
== 0 && !current_kboard
->immediate_echo
1802 && this_command_key_count
> 0
1804 && echo_keystrokes
> 0
1805 && (echo_area_glyphs
== 0 || *echo_area_glyphs
== 0))
1809 /* After a mouse event, start echoing right away.
1810 This is because we are probably about to display a menu,
1811 and we don't want to delay before doing so. */
1812 if (EVENT_HAS_PARAMETERS (prev_event
))
1816 save_getcjmp (save_jump
);
1817 restore_getcjmp (local_getcjmp
);
1818 tem0
= sit_for (echo_keystrokes
, 0, 1, 1);
1819 restore_getcjmp (save_jump
);
1825 /* Maybe auto save due to number of keystrokes. */
1827 if (commandflag
!= 0
1828 && auto_save_interval
> 0
1829 && num_nonmacro_input_chars
- last_auto_save
> max (auto_save_interval
, 20)
1830 && !detect_input_pending ())
1832 Fdo_auto_save (Qnil
, Qnil
);
1833 /* Hooks can actually change some buffers in auto save. */
1837 /* Try reading using an X menu.
1838 This is never confused with reading using the minibuf
1839 because the recursive call of read_char in read_char_minibuf_menu_prompt
1840 does not pass on any keymaps. */
1842 if (nmaps
> 0 && INTERACTIVE
1843 && !NILP (prev_event
)
1844 && EVENT_HAS_PARAMETERS (prev_event
)
1845 && !EQ (XCONS (prev_event
)->car
, Qmenu_bar
)
1846 /* Don't bring up a menu if we already have another event. */
1847 && NILP (Vunread_command_events
)
1848 && unread_command_char
< 0)
1849 c
= read_char_x_menu_prompt (nmaps
, maps
, prev_event
, used_mouse_menu
);
1851 /* Maybe autosave and/or garbage collect due to idleness. */
1853 if (INTERACTIVE
&& NILP (c
))
1855 int delay_level
, buffer_size
;
1857 /* Slow down auto saves logarithmically in size of current buffer,
1858 and garbage collect while we're at it. */
1859 if (! MINI_WINDOW_P (XWINDOW (selected_window
)))
1860 last_non_minibuf_size
= Z
- BEG
;
1861 buffer_size
= (last_non_minibuf_size
>> 8) + 1;
1863 while (buffer_size
> 64)
1864 delay_level
++, buffer_size
-= buffer_size
>> 2;
1865 if (delay_level
< 4) delay_level
= 4;
1866 /* delay_level is 4 for files under around 50k, 7 at 100k,
1867 9 at 200k, 11 at 300k, and 12 at 500k. It is 15 at 1 meg. */
1869 /* Auto save if enough time goes by without input. */
1870 if (commandflag
!= 0
1871 && num_nonmacro_input_chars
> last_auto_save
1872 && INTEGERP (Vauto_save_timeout
)
1873 && XINT (Vauto_save_timeout
) > 0)
1876 int delay
= delay_level
* XFASTINT (Vauto_save_timeout
) / 4;
1878 save_getcjmp (save_jump
);
1879 restore_getcjmp (local_getcjmp
);
1880 tem0
= sit_for (delay
, 0, 1, 1);
1881 restore_getcjmp (save_jump
);
1885 Fdo_auto_save (Qnil
, Qnil
);
1887 /* If we have auto-saved and there is still no input
1888 available, garbage collect if there has been enough
1889 consing going on to make it worthwhile. */
1890 if (!detect_input_pending ()
1891 && consing_since_gc
> gc_cons_threshold
/ 2)
1892 Fgarbage_collect ();
1899 /* Read something from current KBOARD's side queue, if possible. */
1903 if (current_kboard
->kbd_queue_has_data
)
1905 if (!CONSP (current_kboard
->kbd_queue
))
1907 c
= XCONS (current_kboard
->kbd_queue
)->car
;
1908 current_kboard
->kbd_queue
1909 = XCONS (current_kboard
->kbd_queue
)->cdr
;
1910 if (NILP (current_kboard
->kbd_queue
))
1911 current_kboard
->kbd_queue_has_data
= 0;
1912 input_pending
= readable_events ();
1914 if (EVENT_HAS_PARAMETERS (c
)
1915 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c
)), Qswitch_frame
))
1916 internal_last_event_frame
= XCONS (XCONS (c
)->cdr
)->car
;
1917 Vlast_event_frame
= internal_last_event_frame
;
1923 /* If current_kboard's side queue is empty check the other kboards.
1924 If one of them has data that we have not yet seen here,
1925 switch to it and process the data waiting for it.
1927 Note: if the events queued up for another kboard
1928 have already been seen here, and therefore are not a complete command,
1929 the kbd_queue_has_data field is 0, so we skip that kboard here.
1930 That's to avoid an infinite loop switching between kboards here. */
1931 if (NILP (c
) && !single_kboard
)
1934 for (kb
= all_kboards
; kb
; kb
= kb
->next_kboard
)
1935 if (kb
->kbd_queue_has_data
)
1937 current_kboard
= kb
;
1938 longjmp (wrong_kboard_jmpbuf
, 1);
1947 /* Finally, we read from the main queue,
1948 and if that gives us something we can't use yet, we put it on the
1949 appropriate side queue and try again. */
1955 /* Actually read a character, waiting if necessary. */
1956 save_getcjmp (save_jump
);
1957 restore_getcjmp (local_getcjmp
);
1958 c
= kbd_buffer_get_event (&kb
, used_mouse_menu
);
1959 restore_getcjmp (save_jump
);
1962 if (! NILP (c
) && (kb
!= current_kboard
))
1964 Lisp_Object
*tailp
= &kb
->kbd_queue
;
1965 while (CONSP (*tailp
))
1966 tailp
= &XCONS (*tailp
)->cdr
;
1969 *tailp
= Fcons (c
, Qnil
);
1970 kb
->kbd_queue_has_data
= 1;
1974 current_kboard
= kb
;
1975 longjmp (wrong_kboard_jmpbuf
, 1);
1980 /* Terminate Emacs in batch mode if at eof. */
1981 if (noninteractive
&& INTEGERP (c
) && XINT (c
) < 0)
1982 Fkill_emacs (make_number (1));
1986 /* Add in any extra modifiers, where appropriate. */
1987 if ((extra_keyboard_modifiers
& CHAR_CTL
)
1988 || ((extra_keyboard_modifiers
& 0177) < ' '
1989 && (extra_keyboard_modifiers
& 0177) != 0))
1990 XSETINT (c
, make_ctrl_char (XINT (c
)));
1992 /* Transfer any other modifier bits directly from
1993 extra_keyboard_modifiers to c. Ignore the actual character code
1994 in the low 16 bits of extra_keyboard_modifiers. */
1995 XSETINT (c
, XINT (c
) | (extra_keyboard_modifiers
& ~0xff7f & ~CHAR_CTL
));
2004 if (commandflag
>= 0
2005 && !input_pending
&& !detect_input_pending ())
2013 /* Buffer switch events are only for internal wakeups
2014 so don't show them to the user. */
2018 if (key_already_recorded
)
2021 /* Process special events within read_char
2022 and loop around to read another event. */
2025 tem
= get_keyelt (access_keymap (get_keymap_1 (Vspecial_event_map
, 0, 0),
2031 last_input_char
= c
;
2032 Fcommand_execute (tem
, Qnil
);
2036 /* Wipe the echo area. */
2037 echo_area_glyphs
= 0;
2039 /* Handle things that only apply to characters. */
2042 /* If kbd_buffer_get_event gave us an EOF, return that. */
2046 if (STRINGP (Vkeyboard_translate_table
)
2047 && XSTRING (Vkeyboard_translate_table
)->size
> XFASTINT (c
))
2048 XSETINT (c
, XSTRING (Vkeyboard_translate_table
)->data
[XFASTINT (c
)]);
2051 /* If this event is a mouse click in the menu bar,
2052 return just menu-bar for now. Modify the mouse click event
2053 so we won't do this twice, then queue it up. */
2054 if (EVENT_HAS_PARAMETERS (c
)
2055 && CONSP (XCONS (c
)->cdr
)
2056 && CONSP (EVENT_START (c
))
2057 && CONSP (XCONS (EVENT_START (c
))->cdr
))
2061 posn
= POSN_BUFFER_POSN (EVENT_START (c
));
2062 /* Handle menu-bar events:
2063 insert the dummy prefix event `menu-bar'. */
2064 if (EQ (posn
, Qmenu_bar
))
2066 /* Change menu-bar to (menu-bar) as the event "position". */
2067 POSN_BUFFER_POSN (EVENT_START (c
)) = Fcons (posn
, Qnil
);
2070 Vunread_command_events
= Fcons (c
, Vunread_command_events
);
2076 if (! NILP (also_record
))
2077 record_char (also_record
);
2081 before_command_key_count
= this_command_key_count
;
2082 before_command_echo_length
= echo_length ();
2084 /* Don't echo mouse motion events. */
2086 && ! (EVENT_HAS_PARAMETERS (c
)
2087 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (c
)), Qmouse_movement
)))
2090 if (! NILP (also_record
))
2091 echo_char (also_record
);
2094 /* Record this character as part of the current key. */
2095 add_command_key (c
);
2096 if (! NILP (also_record
))
2097 add_command_key (also_record
);
2099 /* Re-reading in the middle of a command */
2101 last_input_char
= c
;
2104 /* Process the help character specially if enabled */
2105 if (!NILP (Vhelp_form
) && help_char_p (c
))
2108 count
= specpdl_ptr
- specpdl
;
2110 record_unwind_protect (Fset_window_configuration
,
2111 Fcurrent_window_configuration (Qnil
));
2113 tem0
= Feval (Vhelp_form
);
2115 internal_with_output_to_temp_buffer ("*Help*", print_help
, tem0
);
2119 c
= read_char (0, 0, 0, Qnil
, 0);
2120 while (BUFFERP (c
));
2121 /* Remove the help from the frame */
2122 unbind_to (count
, Qnil
);
2125 if (EQ (c
, make_number (040)))
2129 c
= read_char (0, 0, 0, Qnil
, 0);
2130 while (BUFFERP (c
));
2137 /* Return 1 if should recognize C as "the help character". */
2145 if (EQ (c
, Vhelp_char
))
2147 for (tail
= Vhelp_event_list
; CONSP (tail
); tail
= XCONS (tail
)->cdr
)
2148 if (EQ (c
, XCONS (tail
)->car
))
2153 /* Record the input event C in various ways. */
2160 XVECTOR (recent_keys
)->contents
[recent_keys_index
] = c
;
2161 if (++recent_keys_index
>= NUM_RECENT_KEYS
)
2162 recent_keys_index
= 0;
2164 /* Write c to the dribble file. If c is a lispy event, write
2165 the event's symbol to the dribble file, in <brackets>. Bleaugh.
2166 If you, dear reader, have a better idea, you've got the source. :-) */
2171 if (XUINT (c
) < 0x100)
2172 putc (XINT (c
), dribble
);
2174 fprintf (dribble
, " 0x%x", (int) XUINT (c
));
2178 Lisp_Object dribblee
;
2180 /* If it's a structured event, take the event header. */
2181 dribblee
= EVENT_HEAD (c
);
2183 if (SYMBOLP (dribblee
))
2185 putc ('<', dribble
);
2186 fwrite (XSYMBOL (dribblee
)->name
->data
, sizeof (char),
2187 XSYMBOL (dribblee
)->name
->size
,
2189 putc ('>', dribble
);
2196 store_kbd_macro_char (c
);
2198 num_nonmacro_input_chars
++;
2205 struct buffer
*old
= current_buffer
;
2206 Fprinc (object
, Qnil
);
2207 set_buffer_internal (XBUFFER (Vstandard_output
));
2208 call0 (intern ("help-mode"));
2209 set_buffer_internal (old
);
2213 /* Copy out or in the info on where C-g should throw to.
2214 This is used when running Lisp code from within get_char,
2215 in case get_char is called recursively.
2216 See read_process_output. */
2221 bcopy (getcjmp
, temp
, sizeof getcjmp
);
2224 restore_getcjmp (temp
)
2227 bcopy (temp
, getcjmp
, sizeof getcjmp
);
2232 /* Restore mouse tracking enablement. See Ftrack_mouse for the only use
2233 of this function. */
2236 tracking_off (old_value
)
2237 Lisp_Object old_value
;
2239 do_mouse_tracking
= old_value
;
2240 if (NILP (old_value
))
2242 /* Redisplay may have been preempted because there was input
2243 available, and it assumes it will be called again after the
2244 input has been processed. If the only input available was
2245 the sort that we have just disabled, then we need to call
2247 if (!readable_events ())
2249 redisplay_preserve_echo_area ();
2250 get_input_pending (&input_pending
);
2255 DEFUN ("track-mouse", Ftrack_mouse
, Strack_mouse
, 0, UNEVALLED
, 0,
2256 "Evaluate BODY with mouse movement events enabled.\n\
2257 Within a `track-mouse' form, mouse motion generates input events that\n\
2258 you can read with `read-event'.\n\
2259 Normally, mouse motion is ignored.")
2263 int count
= specpdl_ptr
- specpdl
;
2266 record_unwind_protect (tracking_off
, do_mouse_tracking
);
2268 do_mouse_tracking
= Qt
;
2270 val
= Fprogn (args
);
2271 return unbind_to (count
, val
);
2274 /* If mouse has moved on some frame, return one of those frames.
2275 Return 0 otherwise. */
2280 Lisp_Object tail
, frame
;
2282 FOR_EACH_FRAME (tail
, frame
)
2284 if (XFRAME (frame
)->mouse_moved
)
2285 return XFRAME (frame
);
2291 #endif /* HAVE_MOUSE */
2293 /* Low level keyboard/mouse input.
2294 kbd_buffer_store_event places events in kbd_buffer, and
2295 kbd_buffer_get_event retrieves them.
2296 mouse_moved indicates when the mouse has moved again, and
2297 *mouse_position_hook provides the mouse position. */
2299 /* Return true iff there are any events in the queue that read-char
2300 would return. If this returns false, a read-char would block. */
2304 if (kbd_fetch_ptr
!= kbd_store_ptr
)
2307 if (!NILP (do_mouse_tracking
) && some_mouse_moved ())
2312 if (current_kboard
->kbd_queue_has_data
)
2318 for (kb
= all_kboards
; kb
; kb
= kb
->next_kboard
)
2319 if (kb
->kbd_queue_has_data
)
2325 /* Set this for debugging, to have a way to get out */
2330 event_to_kboard (event
)
2331 struct input_event
*event
;
2334 frame
= event
->frame_or_window
;
2336 frame
= XCONS (frame
)->car
;
2337 else if (WINDOWP (frame
))
2338 frame
= WINDOW_FRAME (XWINDOW (frame
));
2340 /* There are still some events that don't set this field.
2341 For now, just ignore the problem.
2342 Also ignore dead frames here. */
2343 if (!FRAMEP (frame
) || !FRAME_LIVE_P (XFRAME (frame
)))
2346 return FRAME_KBOARD (XFRAME (frame
));
2350 /* Store an event obtained at interrupt level into kbd_buffer, fifo */
2353 kbd_buffer_store_event (event
)
2354 register struct input_event
*event
;
2356 if (event
->kind
== no_event
)
2359 if (event
->kind
== ascii_keystroke
)
2361 register int c
= event
->code
& 0377;
2363 if (event
->modifiers
& ctrl_modifier
)
2364 c
= make_ctrl_char (c
);
2366 c
|= (event
->modifiers
2367 & (meta_modifier
| alt_modifier
2368 | hyper_modifier
| super_modifier
));
2372 extern SIGTYPE
interrupt_signal ();
2375 struct input_event
*sp
;
2378 && (kb
= FRAME_KBOARD (XFRAME (event
->frame_or_window
)),
2379 kb
!= current_kboard
))
2382 = Fcons (make_lispy_switch_frame (event
->frame_or_window
),
2383 Fcons (make_number (c
), Qnil
));
2384 kb
->kbd_queue_has_data
= 1;
2385 for (sp
= kbd_fetch_ptr
; sp
!= kbd_store_ptr
; sp
++)
2387 if (sp
== kbd_buffer
+ KBD_BUFFER_SIZE
)
2390 if (event_to_kboard (sp
) == kb
)
2392 sp
->kind
= no_event
;
2393 sp
->frame_or_window
= Qnil
;
2401 /* If this results in a quit_char being returned to Emacs as
2402 input, set Vlast_event_frame properly. If this doesn't
2403 get returned to Emacs as an event, the next event read
2404 will set Vlast_event_frame again, so this is safe to do. */
2408 focus
= FRAME_FOCUS_FRAME (XFRAME (event
->frame_or_window
));
2410 focus
= event
->frame_or_window
;
2411 internal_last_event_frame
= focus
;
2412 Vlast_event_frame
= focus
;
2416 last_event_timestamp
= event
->timestamp
;
2417 interrupt_signal ();
2421 if (c
&& c
== stop_character
)
2428 if (kbd_store_ptr
- kbd_buffer
== KBD_BUFFER_SIZE
)
2429 kbd_store_ptr
= kbd_buffer
;
2431 /* Don't let the very last slot in the buffer become full,
2432 since that would make the two pointers equal,
2433 and that is indistinguishable from an empty buffer.
2434 Discard the event if it would fill the last slot. */
2435 if (kbd_fetch_ptr
- 1 != kbd_store_ptr
)
2437 volatile struct input_event
*sp
= kbd_store_ptr
;
2438 sp
->kind
= event
->kind
;
2439 if (event
->kind
== selection_request_event
)
2441 /* We must not use the ordinary copying code for this case,
2442 since `part' is an enum and copying it might not copy enough
2444 bcopy (event
, (char *) sp
, sizeof (*event
));
2448 sp
->code
= event
->code
;
2449 sp
->part
= event
->part
;
2450 sp
->frame_or_window
= event
->frame_or_window
;
2451 sp
->modifiers
= event
->modifiers
;
2454 sp
->timestamp
= event
->timestamp
;
2456 (XVECTOR (kbd_buffer_frame_or_window
)->contents
[kbd_store_ptr
2458 = event
->frame_or_window
);
2464 /* Read one event from the event buffer, waiting if necessary.
2465 The value is a Lisp object representing the event.
2466 The value is nil for an event that should be ignored,
2467 or that was handled here.
2468 We always read and discard one event. */
2471 kbd_buffer_get_event (kbp
, used_mouse_menu
)
2473 int *used_mouse_menu
;
2482 *kbp
= current_kboard
;
2486 /* Wait until there is input available. */
2489 if (kbd_fetch_ptr
!= kbd_store_ptr
)
2492 if (!NILP (do_mouse_tracking
) && some_mouse_moved ())
2496 /* If the quit flag is set, then read_char will return
2497 quit_char, so that counts as "available input." */
2498 if (!NILP (Vquit_flag
))
2499 quit_throw_to_read_char ();
2501 /* One way or another, wait until input is available; then, if
2502 interrupt handlers have not read it, read it now. */
2505 wait_for_kbd_input ();
2507 /* Note SIGIO has been undef'd if FIONREAD is missing. */
2511 if (kbd_fetch_ptr
!= kbd_store_ptr
)
2514 if (!NILP (do_mouse_tracking
) && some_mouse_moved ())
2518 Lisp_Object minus_one
;
2520 XSETINT (minus_one
, -1);
2521 wait_reading_process_input (0, 0, minus_one
, 1);
2523 if (!interrupt_input
&& kbd_fetch_ptr
== kbd_store_ptr
)
2524 /* Pass 1 for EXPECT since we just waited to have input. */
2525 read_avail_input (1);
2527 #endif /* not VMS */
2530 /* At this point, we know that there is a readable event available
2531 somewhere. If the event queue is empty, then there must be a
2532 mouse movement enabled and available. */
2533 if (kbd_fetch_ptr
!= kbd_store_ptr
)
2535 struct input_event
*event
;
2537 event
= ((kbd_fetch_ptr
< kbd_buffer
+ KBD_BUFFER_SIZE
)
2541 last_event_timestamp
= event
->timestamp
;
2544 *kbp
= event_to_kboard (event
);
2546 *kbp
= current_kboard
; /* Better than returning null ptr? */
2548 *kbp
= &the_only_kboard
;
2553 /* These two kinds of events get special handling
2554 and don't actually appear to the command loop.
2555 We return nil for them. */
2556 if (event
->kind
== selection_request_event
)
2559 struct input_event copy
;
2561 /* Remove it from the buffer before processing it,
2562 since otherwise swallow_events will see it
2563 and process it again. */
2565 kbd_fetch_ptr
= event
+ 1;
2566 x_handle_selection_request (©
);
2568 /* We're getting selection request events, but we don't have
2574 else if (event
->kind
== selection_clear_event
)
2577 kbd_fetch_ptr
= event
+ 1;
2578 x_handle_selection_clear (event
);
2580 /* We're getting selection request events, but we don't have
2586 else if (event
->kind
== delete_window_event
)
2588 /* Make an event (delete-frame (FRAME)). */
2589 obj
= Fcons (event
->frame_or_window
, Qnil
);
2590 obj
= Fcons (Qdelete_frame
, Fcons (obj
, Qnil
));
2591 kbd_fetch_ptr
= event
+ 1;
2593 else if (event
->kind
== iconify_event
)
2595 /* Make an event (iconify-frame (FRAME)). */
2596 obj
= Fcons (event
->frame_or_window
, Qnil
);
2597 obj
= Fcons (Qiconify_frame
, Fcons (obj
, Qnil
));
2598 kbd_fetch_ptr
= event
+ 1;
2600 else if (event
->kind
== deiconify_event
)
2602 /* Make an event (make-frame-visible (FRAME)). */
2603 obj
= Fcons (event
->frame_or_window
, Qnil
);
2604 obj
= Fcons (Qmake_frame_visible
, Fcons (obj
, Qnil
));
2605 kbd_fetch_ptr
= event
+ 1;
2608 else if (event
->kind
== buffer_switch_event
)
2610 /* The value doesn't matter here; only the type is tested. */
2611 XSETBUFFER (obj
, current_buffer
);
2612 kbd_fetch_ptr
= event
+ 1;
2614 /* Just discard these, by returning nil.
2615 With MULTI_KBOARD, these events are used as placeholders
2616 when we need to randomly delete events from the queue.
2617 (They shouldn't otherwise be found in the buffer,
2618 but on some machines it appears they do show up
2619 even without MULTI_KBOARD.) */
2620 else if (event
->kind
== no_event
)
2621 kbd_fetch_ptr
= event
+ 1;
2623 /* If this event is on a different frame, return a switch-frame this
2624 time, and leave the event in the queue for next time. */
2631 frame
= event
->frame_or_window
;
2633 frame
= XCONS (frame
)->car
;
2634 else if (WINDOWP (frame
))
2635 frame
= WINDOW_FRAME (XWINDOW (frame
));
2637 focus
= FRAME_FOCUS_FRAME (XFRAME (frame
));
2641 if (! EQ (frame
, internal_last_event_frame
)
2642 && XFRAME (frame
) != selected_frame
)
2643 obj
= make_lispy_switch_frame (frame
);
2644 internal_last_event_frame
= frame
;
2645 #endif /* MULTI_FRAME */
2647 /* If we didn't decide to make a switch-frame event, go ahead
2648 and build a real event from the queue entry. */
2652 obj
= make_lispy_event (event
);
2653 #ifdef USE_X_TOOLKIT
2654 /* If this was a menu selection, then set the flag to inhibit
2655 writing to last_nonmenu_event. Don't do this if the event
2656 we're returning is (menu-bar), though; that indicates the
2657 beginning of the menu sequence, and we might as well leave
2658 that as the `event with parameters' for this selection. */
2659 if (event
->kind
== menu_bar_event
2660 && !(CONSP (obj
) && EQ (XCONS (obj
)->car
, Qmenu_bar
))
2662 *used_mouse_menu
= 1;
2665 /* Wipe out this event, to catch bugs. */
2666 event
->kind
= no_event
;
2667 XVECTOR (kbd_buffer_frame_or_window
)->contents
[event
- kbd_buffer
] = Qnil
;
2669 kbd_fetch_ptr
= event
+ 1;
2674 /* Try generating a mouse motion event. */
2675 else if (!NILP (do_mouse_tracking
) && some_mouse_moved ())
2677 FRAME_PTR f
= some_mouse_moved ();
2678 Lisp_Object bar_window
;
2679 enum scroll_bar_part part
;
2683 *kbp
= current_kboard
;
2684 /* Note that this uses F to determine which display to look at.
2685 If there is no valid info, it does not store anything
2686 so x remains nil. */
2688 (*mouse_position_hook
) (&f
, 0, &bar_window
, &part
, &x
, &y
, &time
);
2693 /* Decide if we should generate a switch-frame event. Don't
2694 generate switch-frame events for motion outside of all Emacs
2700 frame
= FRAME_FOCUS_FRAME (f
);
2702 XSETFRAME (frame
, f
);
2704 if (! EQ (frame
, internal_last_event_frame
)
2705 && XFRAME (frame
) != selected_frame
)
2706 obj
= make_lispy_switch_frame (frame
);
2707 internal_last_event_frame
= frame
;
2711 /* If we didn't decide to make a switch-frame event, go ahead and
2712 return a mouse-motion event. */
2713 if (!NILP (x
) && NILP (obj
))
2714 obj
= make_lispy_movement (f
, bar_window
, part
, x
, y
, time
);
2716 #endif /* HAVE_MOUSE */
2718 /* We were promised by the above while loop that there was
2719 something for us to read! */
2722 input_pending
= readable_events ();
2725 Vlast_event_frame
= internal_last_event_frame
;
2731 /* Process any events that are not user-visible,
2732 then return, without reading any user-visible events. */
2737 while (kbd_fetch_ptr
!= kbd_store_ptr
)
2739 struct input_event
*event
;
2741 event
= ((kbd_fetch_ptr
< kbd_buffer
+ KBD_BUFFER_SIZE
)
2745 last_event_timestamp
= event
->timestamp
;
2747 /* These two kinds of events get special handling
2748 and don't actually appear to the command loop. */
2749 if (event
->kind
== selection_request_event
)
2752 struct input_event copy
;
2754 kbd_fetch_ptr
= event
+ 1;
2755 x_handle_selection_request (©
);
2757 /* We're getting selection request events, but we don't have
2763 else if (event
->kind
== selection_clear_event
)
2766 x_handle_selection_clear (event
);
2767 kbd_fetch_ptr
= event
+ 1;
2769 /* We're getting selection request events, but we don't have
2778 get_input_pending (&input_pending
);
2781 /* Caches for modify_event_symbol. */
2782 static Lisp_Object accent_key_syms
;
2783 static Lisp_Object func_key_syms
;
2784 static Lisp_Object mouse_syms
;
2786 /* This is a list of keysym codes for special "accent" characters.
2787 It parallels lispy_accent_keys. */
2789 static int lispy_accent_codes
[] =
2791 #ifdef XK_dead_circumflex
2796 #ifdef XK_dead_grave
2801 #ifdef XK_dead_tilde
2806 #ifdef XK_dead_diaeresis
2811 #ifdef XK_dead_macron
2816 #ifdef XK_dead_degree
2821 #ifdef XK_dead_acute
2826 #ifdef XK_dead_cedilla
2831 #ifdef XK_dead_breve
2836 #ifdef XK_dead_ogonek
2841 #ifdef XK_dead_caron
2846 #ifdef XK_dead_doubleacute
2847 XK_dead_doubleacute
,
2851 #ifdef XK_dead_abovedot
2858 /* This is a list of Lisp names for special "accent" characters.
2859 It parallels lispy_accent_codes. */
2861 static char *lispy_accent_keys
[] =
2878 /* You'll notice that this table is arranged to be conveniently
2879 indexed by X Windows keysym values. */
2880 static char *lispy_function_keys
[] =
2882 /* X Keysym value */
2884 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff00 */
2892 0, 0, 0, /* 0xff10 */
2894 0, 0, 0, 0, 0, 0, 0,
2897 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff20...2f */
2898 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff30...3f */
2899 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff40...4f */
2901 "home", /* 0xff50 */ /* IsCursorKey */
2912 "select", /* 0xff60 */ /* IsMiscFunctionKey */
2923 "break", /* 0xff6b */
2925 0, 0, 0, 0, 0, 0, 0, 0, "backtab", 0,
2927 0, 0, 0, 0, 0, 0, 0, 0, "kp-numlock", /* 0xff7f */
2928 "kp-space", /* 0xff80 */ /* IsKeypadKey */
2929 0, 0, 0, 0, 0, 0, 0, 0,
2930 "kp-tab", /* 0xff89 */
2932 "kp-enter", /* 0xff8d */
2934 "kp-f1", /* 0xff91 */
2938 "kp-home", /* 0xff95 */
2943 "kp-prior", /* kp-page-up */
2944 "kp-next", /* kp-page-down */
2950 0, 0, 0, 0, 0, 0, 0, 0, 0,
2951 "kp-multiply", /* 0xffaa */
2956 "kp-divide", /* 0xffaf */
2957 "kp-0", /* 0xffb0 */
2958 "kp-1", "kp-2", "kp-3", "kp-4", "kp-5", "kp-6", "kp-7", "kp-8", "kp-9",
2961 "kp-equal", /* 0xffbd */
2962 "f1", /* 0xffbe */ /* IsFunctionKey */
2964 "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", /* 0xffc0 */
2965 "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
2966 "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
2967 "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
2968 "f35", 0, 0, 0, 0, 0, 0, 0, /* 0xffe0 */
2969 0, 0, 0, 0, 0, 0, 0, 0,
2970 0, 0, 0, 0, 0, 0, 0, 0, /* 0xfff0 */
2971 0, 0, 0, 0, 0, 0, 0, "delete"
2974 static char *lispy_mouse_names
[] =
2976 "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
2979 /* Scroll bar parts. */
2980 Lisp_Object Qabove_handle
, Qhandle
, Qbelow_handle
;
2982 /* An array of scroll bar parts, indexed by an enum scroll_bar_part value. */
2983 Lisp_Object
*scroll_bar_parts
[] = {
2984 &Qabove_handle
, &Qhandle
, &Qbelow_handle
2988 /* A vector, indexed by button number, giving the down-going location
2989 of currently depressed buttons, both scroll bar and non-scroll bar.
2991 The elements have the form
2992 (BUTTON-NUMBER MODIFIER-MASK . REST)
2993 where REST is the cdr of a position as it would be reported in the event.
2995 The make_lispy_event function stores positions here to tell the
2996 difference between click and drag events, and to store the starting
2997 location to be included in drag events. */
2999 static Lisp_Object button_down_location
;
3001 /* Information about the most recent up-going button event: Which
3002 button, what location, and what time. */
3004 static int last_mouse_button
;
3005 static int last_mouse_x
;
3006 static int last_mouse_y
;
3007 static unsigned long button_down_time
;
3009 /* The maximum time between clicks to make a double-click,
3010 or Qnil to disable double-click detection,
3011 or Qt for no time limit. */
3012 Lisp_Object Vdouble_click_time
;
3014 /* The number of clicks in this multiple-click. */
3016 int double_click_count
;
3018 /* Given a struct input_event, build the lisp event which represents
3019 it. If EVENT is 0, build a mouse movement event from the mouse
3020 movement buffer, which should have a movement event in it.
3022 Note that events must be passed to this function in the order they
3023 are received; this function stores the location of button presses
3024 in order to build drag events when the button is released. */
3027 make_lispy_event (event
)
3028 struct input_event
*event
;
3032 switch (SWITCH_ENUM_CAST (event
->kind
))
3034 /* A simple keystroke. */
3035 case ascii_keystroke
:
3037 Lisp_Object lispy_c
;
3038 int c
= event
->code
& 0377;
3039 /* Turn ASCII characters into control characters
3041 if (event
->modifiers
& ctrl_modifier
)
3042 c
= make_ctrl_char (c
);
3044 /* Add in the other modifier bits. We took care of ctrl_modifier
3045 just above, and the shift key was taken care of by the X code,
3046 and applied to control characters by make_ctrl_char. */
3047 c
|= (event
->modifiers
3048 & (meta_modifier
| alt_modifier
3049 | hyper_modifier
| super_modifier
));
3050 button_down_time
= 0;
3051 XSETFASTINT (lispy_c
, c
);
3055 /* A function key. The symbol may need to have modifier prefixes
3057 case non_ascii_keystroke
:
3058 button_down_time
= 0;
3060 for (i
= 0; i
< sizeof (lispy_accent_codes
) / sizeof (int); i
++)
3061 if (event
->code
== lispy_accent_codes
[i
])
3062 return modify_event_symbol (i
,
3064 Qfunction_key
, Qnil
,
3065 lispy_accent_keys
, &accent_key_syms
,
3066 (sizeof (lispy_accent_keys
)
3067 / sizeof (lispy_accent_keys
[0])));
3069 /* Handle system-specific keysyms. */
3070 if (event
->code
& (1 << 28))
3072 /* We need to use an alist rather than a vector as the cache
3073 since we can't make a vector long enuf. */
3074 if (NILP (current_kboard
->system_key_syms
))
3075 current_kboard
->system_key_syms
= Fcons (Qnil
, Qnil
);
3076 return modify_event_symbol (event
->code
,
3079 current_kboard
->Vsystem_key_alist
,
3080 0, ¤t_kboard
->system_key_syms
,
3084 return modify_event_symbol (event
->code
- 0xff00,
3086 Qfunction_key
, Qnil
,
3087 lispy_function_keys
, &func_key_syms
,
3088 (sizeof (lispy_function_keys
)
3089 / sizeof (lispy_function_keys
[0])));
3093 /* A mouse click. Figure out where it is, decide whether it's
3094 a press, click or drag, and build the appropriate structure. */
3096 case scroll_bar_click
:
3098 int button
= event
->code
;
3100 Lisp_Object position
;
3101 Lisp_Object
*start_pos_ptr
;
3102 Lisp_Object start_pos
;
3104 if (button
< 0 || button
>= NUM_MOUSE_BUTTONS
)
3107 /* Build the position as appropriate for this mouse click. */
3108 if (event
->kind
== mouse_click
)
3111 FRAME_PTR f
= XFRAME (event
->frame_or_window
);
3116 /* Ignore mouse events that were made on frame that
3117 have been deleted. */
3118 if (! FRAME_LIVE_P (f
))
3121 pixel_to_glyph_coords (f
, XINT (event
->x
), XINT (event
->y
),
3122 &column
, &row
, NULL
, 1);
3124 #ifndef USE_X_TOOLKIT
3125 /* In the non-toolkit version, clicks on the menu bar
3126 are ordinary button events in the event buffer.
3127 Distinguish them, and invoke the menu.
3129 (In the toolkit version, the toolkit handles the menu bar
3130 and Emacs doesn't know about it until after the user
3131 makes a selection.) */
3132 if (row
>= 0 && row
< FRAME_MENU_BAR_LINES (f
))
3134 Lisp_Object items
, item
;
3138 /* Activate the menu bar on the down event. If the
3139 up event comes in before the menu code can deal with it,
3141 if (! (event
->modifiers
& down_modifier
))
3145 items
= FRAME_MENU_BAR_ITEMS (f
);
3146 for (i
= 0; i
< XVECTOR (items
)->size
; i
+= 3)
3148 Lisp_Object pos
, string
;
3149 string
= XVECTOR (items
)->contents
[i
+ 1];
3150 pos
= XVECTOR (items
)->contents
[i
+ 2];
3153 if (column
>= XINT (pos
)
3154 && column
< XINT (pos
) + XSTRING (string
)->size
)
3156 item
= XVECTOR (items
)->contents
[i
];
3162 = Fcons (event
->frame_or_window
,
3164 Fcons (Fcons (event
->x
, event
->y
),
3165 Fcons (make_number (event
->timestamp
),
3168 return Fcons (item
, Fcons (position
, Qnil
));
3170 #endif /* not USE_X_TOOLKIT */
3172 window
= window_from_coordinates (f
, column
, row
, &part
);
3174 if (!WINDOWP (window
))
3176 window
= event
->frame_or_window
;
3181 int pixcolumn
, pixrow
;
3182 column
-= XINT (XWINDOW (window
)->left
);
3183 row
-= XINT (XWINDOW (window
)->top
);
3184 glyph_to_pixel_coords (f
, column
, row
, &pixcolumn
, &pixrow
);
3185 XSETINT (event
->x
, pixcolumn
);
3186 XSETINT (event
->y
, pixrow
);
3191 posn
= Qvertical_line
;
3194 buffer_posn_from_coords (XWINDOW (window
),
3201 Fcons (Fcons (event
->x
, event
->y
),
3202 Fcons (make_number (event
->timestamp
),
3208 Lisp_Object portion_whole
;
3211 window
= event
->frame_or_window
;
3212 portion_whole
= Fcons (event
->x
, event
->y
);
3213 part
= *scroll_bar_parts
[(int) event
->part
];
3217 Fcons (Qvertical_scroll_bar
,
3218 Fcons (portion_whole
,
3219 Fcons (make_number (event
->timestamp
),
3220 Fcons (part
, Qnil
)))));
3223 start_pos_ptr
= &XVECTOR (button_down_location
)->contents
[button
];
3225 start_pos
= *start_pos_ptr
;
3226 *start_pos_ptr
= Qnil
;
3228 is_double
= (button
== last_mouse_button
3229 && XINT (event
->x
) == last_mouse_x
3230 && XINT (event
->y
) == last_mouse_y
3231 && button_down_time
!= 0
3232 && (EQ (Vdouble_click_time
, Qt
)
3233 || (INTEGERP (Vdouble_click_time
)
3234 && ((int)(event
->timestamp
- button_down_time
)
3235 < XINT (Vdouble_click_time
)))));
3236 last_mouse_button
= button
;
3237 last_mouse_x
= XINT (event
->x
);
3238 last_mouse_y
= XINT (event
->y
);
3240 /* If this is a button press, squirrel away the location, so
3241 we can decide later whether it was a click or a drag. */
3242 if (event
->modifiers
& down_modifier
)
3246 double_click_count
++;
3247 event
->modifiers
|= ((double_click_count
> 2)
3252 double_click_count
= 1;
3253 button_down_time
= event
->timestamp
;
3254 *start_pos_ptr
= Fcopy_alist (position
);
3257 /* Now we're releasing a button - check the co-ordinates to
3258 see if this was a click or a drag. */
3259 else if (event
->modifiers
& up_modifier
)
3261 /* If we did not see a down before this up,
3262 ignore the up. Probably this happened because
3263 the down event chose a menu item.
3264 It would be an annoyance to treat the release
3265 of the button that chose the menu item
3266 as a separate event. */
3268 if (!CONSP (start_pos
))
3271 event
->modifiers
&= ~up_modifier
;
3272 #if 0 /* Formerly we treated an up with no down as a click event. */
3273 if (!CONSP (start_pos
))
3274 event
->modifiers
|= click_modifier
;
3278 /* The third element of every position should be the (x,y)
3282 down
= Fnth (make_number (2), start_pos
);
3283 if (EQ (event
->x
, XCONS (down
)->car
)
3284 && EQ (event
->y
, XCONS (down
)->cdr
))
3286 event
->modifiers
|= click_modifier
;
3290 button_down_time
= 0;
3291 event
->modifiers
|= drag_modifier
;
3293 /* Don't check is_double; treat this as multiple
3294 if the down-event was multiple. */
3295 if (double_click_count
> 1)
3296 event
->modifiers
|= ((double_click_count
> 2)
3302 /* Every mouse event should either have the down_modifier or
3303 the up_modifier set. */
3307 /* Get the symbol we should use for the mouse click. */
3310 head
= modify_event_symbol (button
,
3313 lispy_mouse_names
, &mouse_syms
,
3314 (sizeof (lispy_mouse_names
)
3315 / sizeof (lispy_mouse_names
[0])));
3316 if (event
->modifiers
& drag_modifier
)
3321 else if (event
->modifiers
& (double_modifier
| triple_modifier
))
3324 Fcons (make_number (double_click_count
),
3332 #endif /* HAVE_MOUSE */
3334 #ifdef USE_X_TOOLKIT
3335 case menu_bar_event
:
3336 /* The event value is in the cdr of the frame_or_window slot. */
3337 if (!CONSP (event
->frame_or_window
))
3339 return XCONS (event
->frame_or_window
)->cdr
;
3342 /* The 'kind' field of the event is something we don't recognize. */
3351 make_lispy_movement (frame
, bar_window
, part
, x
, y
, time
)
3353 Lisp_Object bar_window
;
3354 enum scroll_bar_part part
;
3359 /* Is it a scroll bar movement? */
3360 if (frame
&& ! NILP (bar_window
))
3362 Lisp_Object part_sym
;
3364 part_sym
= *scroll_bar_parts
[(int) part
];
3365 return Fcons (Qscroll_bar_movement
,
3366 (Fcons (Fcons (bar_window
,
3367 Fcons (Qvertical_scroll_bar
,
3368 Fcons (Fcons (x
, y
),
3369 Fcons (make_number (time
),
3375 /* Or is it an ordinary mouse movement? */
3377 #endif /* MULTI_FRAME */
3390 /* It's in a frame; which window on that frame? */
3391 pixel_to_glyph_coords (frame
, XINT (x
), XINT (y
), &column
, &row
,
3393 window
= window_from_coordinates (frame
, column
, row
, &area
);
3398 if (WINDOWP (window
))
3400 int pixcolumn
, pixrow
;
3401 column
-= XINT (XWINDOW (window
)->left
);
3402 row
-= XINT (XWINDOW (window
)->top
);
3403 glyph_to_pixel_coords (frame
, column
, row
, &pixcolumn
, &pixrow
);
3404 XSETINT (x
, pixcolumn
);
3405 XSETINT (y
, pixrow
);
3410 posn
= Qvertical_line
;
3413 buffer_posn_from_coords (XWINDOW (window
), column
, row
));
3416 else if (frame
!= 0)
3418 XSETFRAME (window
, frame
);
3430 return Fcons (Qmouse_movement
,
3431 Fcons (Fcons (window
,
3433 Fcons (Fcons (x
, y
),
3434 Fcons (make_number (time
),
3440 #endif /* HAVE_MOUSE */
3442 /* Construct a switch frame event. */
3444 make_lispy_switch_frame (frame
)
3447 return Fcons (Qswitch_frame
, Fcons (frame
, Qnil
));
3450 /* Manipulating modifiers. */
3452 /* Parse the name of SYMBOL, and return the set of modifiers it contains.
3454 If MODIFIER_END is non-zero, set *MODIFIER_END to the position in
3455 SYMBOL's name of the end of the modifiers; the string from this
3456 position is the unmodified symbol name.
3458 This doesn't use any caches. */
3461 parse_modifiers_uncached (symbol
, modifier_end
)
3465 struct Lisp_String
*name
;
3469 CHECK_SYMBOL (symbol
, 1);
3472 name
= XSYMBOL (symbol
)->name
;
3474 for (i
= 0; i
+2 <= name
->size
; )
3476 int this_mod_end
= 0;
3479 /* See if the name continues with a modifier word.
3480 Check that the word appears, but don't check what follows it.
3481 Set this_mod and this_mod_end to record what we find. */
3483 switch (name
->data
[i
])
3485 #define SINGLE_LETTER_MOD(BIT) \
3486 (this_mod_end = i + 1, this_mod = BIT)
3489 SINGLE_LETTER_MOD (alt_modifier
);
3493 SINGLE_LETTER_MOD (ctrl_modifier
);
3497 SINGLE_LETTER_MOD (hyper_modifier
);
3501 SINGLE_LETTER_MOD (meta_modifier
);
3505 SINGLE_LETTER_MOD (shift_modifier
);
3509 SINGLE_LETTER_MOD (super_modifier
);
3512 #undef SINGLE_LETTER_MOD
3515 /* If we found no modifier, stop looking for them. */
3516 if (this_mod_end
== 0)
3519 /* Check there is a dash after the modifier, so that it
3520 really is a modifier. */
3521 if (this_mod_end
>= name
->size
|| name
->data
[this_mod_end
] != '-')
3524 /* This modifier is real; look for another. */
3525 modifiers
|= this_mod
;
3526 i
= this_mod_end
+ 1;
3529 /* Should we include the `click' modifier? */
3530 if (! (modifiers
& (down_modifier
| drag_modifier
3531 | double_modifier
| triple_modifier
))
3532 && i
+ 7 == name
->size
3533 && strncmp (name
->data
+ i
, "mouse-", 6) == 0
3534 && ('0' <= name
->data
[i
+ 6] && name
->data
[i
+ 6] <= '9'))
3535 modifiers
|= click_modifier
;
3543 /* Return a symbol whose name is the modifier prefixes for MODIFIERS
3544 prepended to the string BASE[0..BASE_LEN-1].
3545 This doesn't use any caches. */
3547 apply_modifiers_uncached (modifiers
, base
, base_len
)
3552 /* Since BASE could contain nulls, we can't use intern here; we have
3553 to use Fintern, which expects a genuine Lisp_String, and keeps a
3556 (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-double-triple-"));
3562 /* Only the event queue may use the `up' modifier; it should always
3563 be turned into a click or drag event before presented to lisp code. */
3564 if (modifiers
& up_modifier
)
3567 if (modifiers
& alt_modifier
) { *p
++ = 'A'; *p
++ = '-'; }
3568 if (modifiers
& ctrl_modifier
) { *p
++ = 'C'; *p
++ = '-'; }
3569 if (modifiers
& hyper_modifier
) { *p
++ = 'H'; *p
++ = '-'; }
3570 if (modifiers
& meta_modifier
) { *p
++ = 'M'; *p
++ = '-'; }
3571 if (modifiers
& shift_modifier
) { *p
++ = 'S'; *p
++ = '-'; }
3572 if (modifiers
& super_modifier
) { *p
++ = 's'; *p
++ = '-'; }
3573 if (modifiers
& double_modifier
) { strcpy (p
, "double-"); p
+= 7; }
3574 if (modifiers
& triple_modifier
) { strcpy (p
, "triple-"); p
+= 7; }
3575 if (modifiers
& down_modifier
) { strcpy (p
, "down-"); p
+= 5; }
3576 if (modifiers
& drag_modifier
) { strcpy (p
, "drag-"); p
+= 5; }
3577 /* The click modifier is denoted by the absence of other modifiers. */
3581 mod_len
= p
- new_mods
;
3585 Lisp_Object new_name
;
3587 new_name
= make_uninit_string (mod_len
+ base_len
);
3588 bcopy (new_mods
, XSTRING (new_name
)->data
, mod_len
);
3589 bcopy (base
, XSTRING (new_name
)->data
+ mod_len
, base_len
);
3591 return Fintern (new_name
, Qnil
);
3596 static char *modifier_names
[] =
3598 "up", "down", "drag", "click", "double", "triple", 0, 0,
3599 0, 0, 0, 0, 0, 0, 0, 0,
3600 0, 0, "alt", "super", "hyper", "shift", "control", "meta"
3602 #define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
3604 static Lisp_Object modifier_symbols
;
3606 /* Return the list of modifier symbols corresponding to the mask MODIFIERS. */
3608 lispy_modifier_list (modifiers
)
3611 Lisp_Object modifier_list
;
3614 modifier_list
= Qnil
;
3615 for (i
= 0; (1<<i
) <= modifiers
&& i
< NUM_MOD_NAMES
; i
++)
3616 if (modifiers
& (1<<i
))
3617 modifier_list
= Fcons (XVECTOR (modifier_symbols
)->contents
[i
],
3620 return modifier_list
;
3624 /* Parse the modifiers on SYMBOL, and return a list like (UNMODIFIED MASK),
3625 where UNMODIFIED is the unmodified form of SYMBOL,
3626 MASK is the set of modifiers present in SYMBOL's name.
3627 This is similar to parse_modifiers_uncached, but uses the cache in
3628 SYMBOL's Qevent_symbol_element_mask property, and maintains the
3629 Qevent_symbol_elements property. */
3632 parse_modifiers (symbol
)
3635 Lisp_Object elements
;
3637 elements
= Fget (symbol
, Qevent_symbol_element_mask
);
3638 if (CONSP (elements
))
3643 int modifiers
= parse_modifiers_uncached (symbol
, &end
);
3644 Lisp_Object unmodified
;
3647 unmodified
= Fintern (make_string (XSYMBOL (symbol
)->name
->data
+ end
,
3648 XSYMBOL (symbol
)->name
->size
- end
),
3651 if (modifiers
& ~(((EMACS_INT
)1 << VALBITS
) - 1))
3653 XSETFASTINT (mask
, modifiers
);
3654 elements
= Fcons (unmodified
, Fcons (mask
, Qnil
));
3656 /* Cache the parsing results on SYMBOL. */
3657 Fput (symbol
, Qevent_symbol_element_mask
,
3659 Fput (symbol
, Qevent_symbol_elements
,
3660 Fcons (unmodified
, lispy_modifier_list (modifiers
)));
3662 /* Since we know that SYMBOL is modifiers applied to unmodified,
3663 it would be nice to put that in unmodified's cache.
3664 But we can't, since we're not sure that parse_modifiers is
3671 /* Apply the modifiers MODIFIERS to the symbol BASE.
3672 BASE must be unmodified.
3674 This is like apply_modifiers_uncached, but uses BASE's
3675 Qmodifier_cache property, if present. It also builds
3676 Qevent_symbol_elements properties, since it has that info anyway.
3678 apply_modifiers copies the value of BASE's Qevent_kind property to
3679 the modified symbol. */
3681 apply_modifiers (modifiers
, base
)
3685 Lisp_Object cache
, index
, entry
, new_symbol
;
3687 /* Mask out upper bits. We don't know where this value's been. */
3688 modifiers
&= ((EMACS_INT
)1 << VALBITS
) - 1;
3690 /* The click modifier never figures into cache indices. */
3691 cache
= Fget (base
, Qmodifier_cache
);
3692 XSETFASTINT (index
, (modifiers
& ~click_modifier
));
3693 entry
= assq_no_quit (index
, cache
);
3696 new_symbol
= XCONS (entry
)->cdr
;
3699 /* We have to create the symbol ourselves. */
3700 new_symbol
= apply_modifiers_uncached (modifiers
,
3701 XSYMBOL (base
)->name
->data
,
3702 XSYMBOL (base
)->name
->size
);
3704 /* Add the new symbol to the base's cache. */
3705 entry
= Fcons (index
, new_symbol
);
3706 Fput (base
, Qmodifier_cache
, Fcons (entry
, cache
));
3708 /* We have the parsing info now for free, so add it to the caches. */
3709 XSETFASTINT (index
, modifiers
);
3710 Fput (new_symbol
, Qevent_symbol_element_mask
,
3711 Fcons (base
, Fcons (index
, Qnil
)));
3712 Fput (new_symbol
, Qevent_symbol_elements
,
3713 Fcons (base
, lispy_modifier_list (modifiers
)));
3716 /* Make sure this symbol is of the same kind as BASE.
3718 You'd think we could just set this once and for all when we
3719 intern the symbol above, but reorder_modifiers may call us when
3720 BASE's property isn't set right; we can't assume that just
3721 because it has a Qmodifier_cache property it must have its
3722 Qevent_kind set right as well. */
3723 if (NILP (Fget (new_symbol
, Qevent_kind
)))
3727 kind
= Fget (base
, Qevent_kind
);
3729 Fput (new_symbol
, Qevent_kind
, kind
);
3736 /* Given a symbol whose name begins with modifiers ("C-", "M-", etc),
3737 return a symbol with the modifiers placed in the canonical order.
3738 Canonical order is alphabetical, except for down and drag, which
3739 always come last. The 'click' modifier is never written out.
3741 Fdefine_key calls this to make sure that (for example) C-M-foo
3742 and M-C-foo end up being equivalent in the keymap. */
3745 reorder_modifiers (symbol
)
3748 /* It's hopefully okay to write the code this way, since everything
3749 will soon be in caches, and no consing will be done at all. */
3752 parsed
= parse_modifiers (symbol
);
3753 return apply_modifiers ((int) XINT (XCONS (XCONS (parsed
)->cdr
)->car
),
3754 XCONS (parsed
)->car
);
3758 /* For handling events, we often want to produce a symbol whose name
3759 is a series of modifier key prefixes ("M-", "C-", etcetera) attached
3760 to some base, like the name of a function key or mouse button.
3761 modify_event_symbol produces symbols of this sort.
3763 NAME_TABLE should point to an array of strings, such that NAME_TABLE[i]
3764 is the name of the i'th symbol. TABLE_SIZE is the number of elements
3767 Alternatively, NAME_ALIST is an alist mapping codes into symbol names.
3768 NAME_ALIST is used if it is non-nil; otherwise NAME_TABLE is used.
3770 SYMBOL_TABLE should be a pointer to a Lisp_Object whose value will
3771 persist between calls to modify_event_symbol that it can use to
3772 store a cache of the symbols it's generated for this NAME_TABLE
3773 before. The object stored there may be a vector or an alist.
3775 SYMBOL_NUM is the number of the base name we want from NAME_TABLE.
3777 MODIFIERS is a set of modifier bits (as given in struct input_events)
3778 whose prefixes should be applied to the symbol name.
3780 SYMBOL_KIND is the value to be placed in the event_kind property of
3781 the returned symbol.
3783 The symbols we create are supposed to have an
3784 `event-symbol-elements' property, which lists the modifiers present
3785 in the symbol's name. */
3788 modify_event_symbol (symbol_num
, modifiers
, symbol_kind
, name_alist
,
3789 name_table
, symbol_table
, table_size
)
3792 Lisp_Object symbol_kind
;
3793 Lisp_Object name_alist
;
3795 Lisp_Object
*symbol_table
;
3796 unsigned int table_size
;
3799 Lisp_Object symbol_int
;
3801 /* Get rid of the "vendor-specific" bit here. */
3802 XSETINT (symbol_int
, symbol_num
& 0xffffff);
3804 /* Is this a request for a valid symbol? */
3805 if (symbol_num
< 0 || symbol_num
>= table_size
)
3808 if (CONSP (*symbol_table
))
3809 value
= Fcdr (assq_no_quit (symbol_int
, *symbol_table
));
3811 /* If *symbol_table doesn't seem to be initialized properly, fix that.
3812 *symbol_table should be a lisp vector TABLE_SIZE elements long,
3813 where the Nth element is the symbol for NAME_TABLE[N], or nil if
3814 we've never used that symbol before. */
3817 if (! VECTORP (*symbol_table
)
3818 || XVECTOR (*symbol_table
)->size
!= table_size
)
3822 XSETFASTINT (size
, table_size
);
3823 *symbol_table
= Fmake_vector (size
, Qnil
);
3826 value
= XVECTOR (*symbol_table
)->contents
[symbol_num
];
3829 /* Have we already used this symbol before? */
3832 /* No; let's create it. */
3833 if (!NILP (name_alist
))
3834 value
= Fcdr_safe (Fassq (symbol_int
, name_alist
));
3835 else if (name_table
[symbol_num
])
3836 value
= intern (name_table
[symbol_num
]);
3838 #ifdef HAVE_X_WINDOWS
3841 char *name
= x_get_keysym_name (symbol_num
);
3843 value
= intern (name
);
3850 sprintf (buf
, "key-%d", symbol_num
);
3851 value
= intern (buf
);
3854 if (CONSP (*symbol_table
))
3855 *symbol_table
= Fcons (value
, *symbol_table
);
3857 XVECTOR (*symbol_table
)->contents
[symbol_num
] = value
;
3859 /* Fill in the cache entries for this symbol; this also
3860 builds the Qevent_symbol_elements property, which the user
3862 apply_modifiers (modifiers
& click_modifier
, value
);
3863 Fput (value
, Qevent_kind
, symbol_kind
);
3866 /* Apply modifiers to that symbol. */
3867 return apply_modifiers (modifiers
, value
);
3870 /* Convert a list that represents an event type,
3871 such as (ctrl meta backspace), into the usual representation of that
3872 event type as a number or a symbol. */
3874 DEFUN ("event-convert-list", Fevent_convert_list
, Sevent_convert_list
, 1, 1, 0,
3875 "Convert the event description LIST to an event type.\n\
3876 LIST should contain one base event type (a character or symbol)\n\
3877 and zero or more modifier names (control, meta, hyper, super, shift, alt,\n\
3878 drag, down, double or triple).\n\
3879 The return value is an event type (a character or symbol) which\n\
3880 has the same base event type and all the specified modifiers.")
3890 while (CONSP (rest
))
3895 elt
= XCONS (rest
)->car
;
3897 /* Given a symbol, see if it is a modifier name. */
3899 this = parse_solitary_modifier (elt
);
3903 else if (!NILP (base
))
3904 error ("Two bases given in one event");
3908 rest
= XCONS (rest
)->cdr
;
3911 /* Let the symbol A refer to the character A. */
3912 if (SYMBOLP (base
) && XSYMBOL (base
)->name
->size
== 1)
3913 XSETINT (base
, XSYMBOL (base
)->name
->data
[0]);
3915 if (INTEGERP (base
))
3917 /* Turn (shift a) into A. */
3918 if ((modifiers
& shift_modifier
) != 0
3919 && (XINT (base
) >= 'a' && XINT (base
) <= 'z'))
3921 XSETINT (base
, XINT (base
) - ('a' - 'A'));
3922 modifiers
&= ~shift_modifier
;
3925 /* Turn (control a) into C-a. */
3926 if (modifiers
& ctrl_modifier
)
3927 return make_number ((modifiers
& ~ctrl_modifier
)
3928 | make_ctrl_char (XINT (base
)));
3930 return make_number (modifiers
| XINT (base
));
3932 else if (SYMBOLP (base
))
3933 return apply_modifiers (modifiers
, base
);
3935 error ("Invalid base event");
3938 /* Try to recognize SYMBOL as a modifier name.
3939 Return the modifier flag bit, or 0 if not recognized. */
3942 parse_solitary_modifier (symbol
)
3945 struct Lisp_String
*name
= XSYMBOL (symbol
)->name
;
3947 switch (name
->data
[0])
3949 #define SINGLE_LETTER_MOD(BIT) \
3950 if (name->size == 1) \
3953 #define MULTI_LETTER_MOD(BIT, NAME, LEN) \
3954 if (LEN == name->size \
3955 && ! strncmp (name->data, NAME, LEN)) \
3959 SINGLE_LETTER_MOD (alt_modifier
);
3963 MULTI_LETTER_MOD (alt_modifier
, "alt", 3);
3967 SINGLE_LETTER_MOD (ctrl_modifier
);
3971 MULTI_LETTER_MOD (ctrl_modifier
, "ctrl", 4);
3972 MULTI_LETTER_MOD (ctrl_modifier
, "control", 7);
3976 SINGLE_LETTER_MOD (hyper_modifier
);
3980 MULTI_LETTER_MOD (hyper_modifier
, "hyper", 5);
3984 SINGLE_LETTER_MOD (meta_modifier
);
3988 MULTI_LETTER_MOD (meta_modifier
, "meta", 4);
3992 SINGLE_LETTER_MOD (shift_modifier
);
3996 MULTI_LETTER_MOD (shift_modifier
, "shift", 5);
3997 MULTI_LETTER_MOD (super_modifier
, "super", 5);
3998 SINGLE_LETTER_MOD (super_modifier
);
4002 MULTI_LETTER_MOD (drag_modifier
, "drag", 4);
4003 MULTI_LETTER_MOD (down_modifier
, "down", 4);
4004 MULTI_LETTER_MOD (double_modifier
, "double", 6);
4008 MULTI_LETTER_MOD (triple_modifier
, "triple", 6);
4011 #undef SINGLE_LETTER_MOD
4012 #undef MULTI_LETTER_MOD
4018 /* Return 1 if EVENT is a list whose elements are all integers or symbols.
4019 Such a list is not valid as an event,
4020 but it can be a Lucid-style event type list. */
4023 lucid_event_type_list_p (object
)
4028 if (! CONSP (object
))
4031 for (tail
= object
; CONSP (tail
); tail
= XCONS (tail
)->cdr
)
4034 elt
= XCONS (tail
)->car
;
4035 if (! (INTEGERP (elt
) || SYMBOLP (elt
)))
4042 /* Store into *addr a value nonzero if terminal input chars are available.
4043 Serves the purpose of ioctl (0, FIONREAD, addr)
4044 but works even if FIONREAD does not exist.
4045 (In fact, this may actually read some input.) */
4048 get_input_pending (addr
)
4051 /* First of all, have we already counted some input? */
4052 *addr
= !NILP (Vquit_flag
) || readable_events ();
4054 /* If input is being read as it arrives, and we have none, there is none. */
4055 if (*addr
> 0 || (interrupt_input
&& ! interrupts_deferred
))
4058 /* Try to read some input and see how much we get. */
4060 *addr
= !NILP (Vquit_flag
) || readable_events ();
4063 /* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
4066 gobble_input (expected
)
4071 if (interrupt_input
)
4074 mask
= sigblockx (SIGIO
);
4075 read_avail_input (expected
);
4079 #ifdef POLL_FOR_INPUT
4080 if (read_socket_hook
&& !interrupt_input
&& poll_suppress_count
== 0)
4083 mask
= sigblockx (SIGALRM
);
4084 read_avail_input (expected
);
4090 read_avail_input (expected
);
4094 /* Put a buffer_switch_event in the buffer
4095 so that read_key_sequence will notice the new current buffer. */
4097 record_asynch_buffer_change ()
4099 struct input_event event
;
4102 event
.kind
= buffer_switch_event
;
4103 event
.frame_or_window
= Qnil
;
4106 /* We don't need a buffer-switch event unless Emacs is waiting for input.
4107 The purpose of the event is to make read_key_sequence look up the
4108 keymaps again. If we aren't in read_key_sequence, we don't need one,
4109 and the event could cause trouble by messing up (input-pending-p). */
4110 tem
= Fwaiting_for_user_input_p ();
4114 /* We never need these events if we have no asynchronous subprocesses. */
4118 /* Make sure no interrupt happens while storing the event. */
4120 if (interrupt_input
)
4123 mask
= sigblockx (SIGIO
);
4124 kbd_buffer_store_event (&event
);
4131 kbd_buffer_store_event (&event
);
4138 /* Read any terminal input already buffered up by the system
4139 into the kbd_buffer, but do not wait.
4141 EXPECTED should be nonzero if the caller knows there is some input.
4143 Except on VMS, all input is read by this function.
4144 If interrupt_input is nonzero, this function MUST be called
4145 only when SIGIO is blocked.
4147 Returns the number of keyboard chars read, or -1 meaning
4148 this is a bad time to try to read input. */
4151 read_avail_input (expected
)
4154 struct input_event buf
[KBD_BUFFER_SIZE
];
4158 if (read_socket_hook
)
4159 /* No need for FIONREAD or fcntl; just say don't wait. */
4160 nread
= (*read_socket_hook
) (input_fd
, buf
, KBD_BUFFER_SIZE
,
4161 expected
, expected
);
4164 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
4165 the kbd_buffer can really hold. That may prevent loss
4166 of characters on some systems when input is stuffed at us. */
4167 unsigned char cbuf
[KBD_BUFFER_SIZE
- 1];
4170 /* Determine how many characters we should *try* to read. */
4173 #else /* not WINDOWSNT */
4175 n_to_read
= dos_keysns ();
4178 #else /* not MSDOS */
4180 /* Find out how much input is available. */
4181 if (ioctl (input_fd
, FIONREAD
, &n_to_read
) < 0)
4182 /* Formerly simply reported no input, but that sometimes led to
4183 a failure of Emacs to terminate.
4184 SIGHUP seems appropriate if we can't reach the terminal. */
4185 /* ??? Is it really right to send the signal just to this process
4186 rather than to the whole process group?
4187 Perhaps on systems with FIONREAD Emacs is alone in its group. */
4188 kill (getpid (), SIGHUP
);
4191 if (n_to_read
> sizeof cbuf
)
4192 n_to_read
= sizeof cbuf
;
4193 #else /* no FIONREAD */
4194 #if defined(USG) || defined(DGUX)
4195 /* Read some input if available, but don't wait. */
4196 n_to_read
= sizeof cbuf
;
4197 fcntl (input_fd
, F_SETFL
, O_NDELAY
);
4202 #endif /* not MSDOS */
4203 #endif /* not WINDOWSNT */
4205 /* Now read; for one reason or another, this will not block.
4206 NREAD is set to the number of chars read. */
4210 cbuf
[0] = dos_keyread();
4213 nread
= read (input_fd
, cbuf
, n_to_read
);
4215 #if defined (AIX) && (! defined (aix386) && defined (_BSD))
4216 /* The kernel sometimes fails to deliver SIGHUP for ptys.
4217 This looks incorrect, but it isn't, because _BSD causes
4218 O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
4219 and that causes a value other than 0 when there is no input. */
4225 /* We used to retry the read if it was interrupted.
4226 But this does the wrong thing when O_NDELAY causes
4227 an EAGAIN error. Does anybody know of a situation
4228 where a retry is actually needed? */
4230 nread
< 0 && (errno
== EAGAIN
4244 #if defined (USG) || defined (DGUX)
4245 fcntl (input_fd
, F_SETFL
, 0);
4246 #endif /* USG or DGUX */
4247 #endif /* no FIONREAD */
4248 for (i
= 0; i
< nread
; i
++)
4250 buf
[i
].kind
= ascii_keystroke
;
4251 buf
[i
].modifiers
= 0;
4252 if (meta_key
== 1 && (cbuf
[i
] & 0x80))
4253 buf
[i
].modifiers
= meta_modifier
;
4257 buf
[i
].code
= cbuf
[i
];
4259 XSETFRAME (buf
[i
].frame_or_window
, selected_frame
);
4261 buf
[i
].frame_or_window
= Qnil
;
4266 /* Scan the chars for C-g and store them in kbd_buffer. */
4267 for (i
= 0; i
< nread
; i
++)
4269 kbd_buffer_store_event (&buf
[i
]);
4270 /* Don't look at input that follows a C-g too closely.
4271 This reduces lossage due to autorepeat on C-g. */
4272 if (buf
[i
].kind
== ascii_keystroke
4273 && buf
[i
].code
== quit_char
)
4279 #endif /* not VMS */
4281 #ifdef SIGIO /* for entire page */
4282 /* Note SIGIO has been undef'd if FIONREAD is missing. */
4285 input_available_signal (signo
)
4288 /* Must preserve main program's value of errno. */
4289 int old_errno
= errno
;
4291 extern int select_alarmed
;
4295 /* USG systems forget handlers when they are used;
4296 must reestablish each time */
4297 signal (signo
, input_available_signal
);
4304 if (input_available_clear_time
)
4305 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
4310 nread
= read_avail_input (1);
4311 /* -1 means it's not ok to read the input now.
4312 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
4313 0 means there was no keyboard input available. */
4318 select_alarmed
= 1; /* Force the select emulator back to life */
4329 /* Send ourselves a SIGIO.
4331 This function exists so that the UNBLOCK_INPUT macro in
4332 blockinput.h can have some way to take care of input we put off
4333 dealing with, without assuming that every file which uses
4334 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
4336 reinvoke_input_signal ()
4345 /* Return the prompt-string of a sparse keymap.
4346 This is the first element which is a string.
4347 Return nil if there is none. */
4355 register Lisp_Object tem
;
4364 static void menu_bar_item ();
4365 static void menu_bar_one_keymap ();
4367 /* These variables hold the vector under construction within
4368 menu_bar_items and its subroutines, and the current index
4369 for storing into that vector. */
4370 static Lisp_Object menu_bar_items_vector
;
4371 static int menu_bar_items_index
;
4373 /* Return a vector of menu items for a menu bar, appropriate
4374 to the current buffer. Each item has three elements in the vector:
4377 OLD is an old vector we can optionally reuse, or nil. */
4380 menu_bar_items (old
)
4383 /* The number of keymaps we're scanning right now, and the number of
4384 keymaps we have allocated space for. */
4387 /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
4388 in the current keymaps, or nil where it is not a prefix. */
4391 Lisp_Object def
, tem
, tail
;
4400 struct gcpro gcpro1
;
4402 /* In order to build the menus, we need to call the keymap
4403 accessors. They all call QUIT. But this function is called
4404 during redisplay, during which a quit is fatal. So inhibit
4405 quitting while building the menus.
4406 We do this instead of specbind because (1) errors will clear it anyway
4407 and (2) this avoids risk of specpdl overflow. */
4408 oquit
= Vinhibit_quit
;
4412 menu_bar_items_vector
= old
;
4414 menu_bar_items_vector
= Fmake_vector (make_number (24), Qnil
);
4415 menu_bar_items_index
= 0;
4417 GCPRO1 (menu_bar_items_vector
);
4419 /* Build our list of keymaps.
4420 If we recognize a function key and replace its escape sequence in
4421 keybuf with its symbol, or if the sequence starts with a mouse
4422 click and we need to switch buffers, we jump back here to rebuild
4423 the initial keymaps from the current buffer. */
4427 /* Should overriding-terminal-local-map and overriding-local-map apply? */
4428 if (!NILP (Voverriding_local_map_menu_flag
))
4430 /* Yes, use them (if non-nil) as well as the global map. */
4431 maps
= (Lisp_Object
*) alloca (3 * sizeof (maps
[0]));
4433 if (!NILP (current_kboard
->Voverriding_terminal_local_map
))
4434 maps
[nmaps
++] = current_kboard
->Voverriding_terminal_local_map
;
4435 if (!NILP (Voverriding_local_map
))
4436 maps
[nmaps
++] = Voverriding_local_map
;
4440 /* No, so use major and minor mode keymaps. */
4441 nmaps
= current_minor_maps (NULL
, &tmaps
);
4442 maps
= (Lisp_Object
*) alloca ((nmaps
+ 2) * sizeof (maps
[0]));
4443 bcopy (tmaps
, maps
, nmaps
* sizeof (maps
[0]));
4444 #ifdef USE_TEXT_PROPERTIES
4445 maps
[nmaps
++] = get_local_map (PT
, current_buffer
);
4447 maps
[nmaps
++] = current_buffer
->keymap
;
4450 maps
[nmaps
++] = current_global_map
;
4453 /* Look up in each map the dummy prefix key `menu-bar'. */
4457 for (mapno
= nmaps
- 1; mapno
>= 0; mapno
--)
4459 if (! NILP (maps
[mapno
]))
4460 def
= get_keyelt (access_keymap (maps
[mapno
], Qmenu_bar
, 1, 0));
4464 tem
= Fkeymapp (def
);
4466 menu_bar_one_keymap (def
);
4469 /* Move to the end those items that should be at the end. */
4471 for (tail
= Vmenu_bar_final_items
; CONSP (tail
); tail
= XCONS (tail
)->cdr
)
4474 int end
= menu_bar_items_index
;
4476 for (i
= 0; i
< end
; i
+= 3)
4477 if (EQ (XCONS (tail
)->car
, XVECTOR (menu_bar_items_vector
)->contents
[i
]))
4479 Lisp_Object tem0
, tem1
, tem2
;
4480 /* Move the item at index I to the end,
4481 shifting all the others forward. */
4482 tem0
= XVECTOR (menu_bar_items_vector
)->contents
[i
+ 0];
4483 tem1
= XVECTOR (menu_bar_items_vector
)->contents
[i
+ 1];
4484 tem2
= XVECTOR (menu_bar_items_vector
)->contents
[i
+ 2];
4486 bcopy (&XVECTOR (menu_bar_items_vector
)->contents
[i
+ 3],
4487 &XVECTOR (menu_bar_items_vector
)->contents
[i
],
4488 (end
- i
- 3) * sizeof (Lisp_Object
));
4489 XVECTOR (menu_bar_items_vector
)->contents
[end
- 3] = tem0
;
4490 XVECTOR (menu_bar_items_vector
)->contents
[end
- 2] = tem1
;
4491 XVECTOR (menu_bar_items_vector
)->contents
[end
- 1] = tem2
;
4496 /* Add nil, nil, nil at the end. */
4497 i
= menu_bar_items_index
;
4498 if (i
+ 3 > XVECTOR (menu_bar_items_vector
)->size
)
4501 int newsize
= 2 * i
;
4502 tem
= Fmake_vector (make_number (2 * i
), Qnil
);
4503 bcopy (XVECTOR (menu_bar_items_vector
)->contents
,
4504 XVECTOR (tem
)->contents
, i
* sizeof (Lisp_Object
));
4505 menu_bar_items_vector
= tem
;
4507 /* Add this item. */
4508 XVECTOR (menu_bar_items_vector
)->contents
[i
++] = Qnil
;
4509 XVECTOR (menu_bar_items_vector
)->contents
[i
++] = Qnil
;
4510 XVECTOR (menu_bar_items_vector
)->contents
[i
++] = Qnil
;
4511 menu_bar_items_index
= i
;
4513 Vinhibit_quit
= oquit
;
4515 return menu_bar_items_vector
;
4518 /* Scan one map KEYMAP, accumulating any menu items it defines
4519 in menu_bar_items_vector. */
4522 menu_bar_one_keymap (keymap
)
4525 Lisp_Object tail
, item
, key
, binding
, item_string
, table
;
4527 /* Loop over all keymap entries that have menu strings. */
4528 for (tail
= keymap
; CONSP (tail
); tail
= XCONS (tail
)->cdr
)
4530 item
= XCONS (tail
)->car
;
4533 key
= XCONS (item
)->car
;
4534 binding
= XCONS (item
)->cdr
;
4535 if (CONSP (binding
))
4537 item_string
= XCONS (binding
)->car
;
4538 if (STRINGP (item_string
))
4539 menu_bar_item (key
, item_string
, Fcdr (binding
));
4541 else if (EQ (binding
, Qundefined
))
4542 menu_bar_item (key
, Qnil
, binding
);
4544 else if (VECTORP (item
))
4546 /* Loop over the char values represented in the vector. */
4547 int len
= XVECTOR (item
)->size
;
4549 for (c
= 0; c
< len
; c
++)
4551 Lisp_Object character
;
4552 XSETFASTINT (character
, c
);
4553 binding
= XVECTOR (item
)->contents
[c
];
4554 if (CONSP (binding
))
4556 item_string
= XCONS (binding
)->car
;
4557 if (STRINGP (item_string
))
4558 menu_bar_item (key
, item_string
, Fcdr (binding
));
4560 else if (EQ (binding
, Qundefined
))
4561 menu_bar_item (key
, Qnil
, binding
);
4567 /* This is used as the handler when calling internal_condition_case_1. */
4570 menu_bar_item_1 (arg
)
4576 /* Add one item to menu_bar_items_vector, for KEY, ITEM_STRING and DEF.
4577 If there's already an item for KEY, add this DEF to it. */
4580 menu_bar_item (key
, item_string
, def
)
4581 Lisp_Object key
, item_string
, def
;
4584 Lisp_Object enabled
;
4587 if (EQ (def
, Qundefined
))
4589 /* If a map has an explicit `undefined' as definition,
4590 discard any previously made menu bar item. */
4592 for (i
= 0; i
< menu_bar_items_index
; i
+= 3)
4593 if (EQ (key
, XVECTOR (menu_bar_items_vector
)->contents
[i
]))
4595 if (menu_bar_items_index
> i
+ 3)
4596 bcopy (&XVECTOR (menu_bar_items_vector
)->contents
[i
+ 3],
4597 &XVECTOR (menu_bar_items_vector
)->contents
[i
],
4598 (menu_bar_items_index
- i
- 3) * sizeof (Lisp_Object
));
4599 menu_bar_items_index
-= 3;
4603 /* If there's no definition for this key yet,
4604 just ignore `undefined'. */
4608 /* See if this entry is enabled. */
4613 /* No property, or nil, means enable.
4614 Otherwise, enable if value is not nil. */
4615 tem
= Fget (def
, Qmenu_enable
);
4617 /* (condition-case nil (eval tem)
4619 enabled
= internal_condition_case_1 (Feval
, tem
, Qerror
,
4623 /* Ignore this item if it's not enabled. */
4627 /* Find any existing item for this KEY. */
4628 for (i
= 0; i
< menu_bar_items_index
; i
+= 3)
4629 if (EQ (key
, XVECTOR (menu_bar_items_vector
)->contents
[i
]))
4632 /* If we did not find this KEY, add it at the end. */
4633 if (i
== menu_bar_items_index
)
4635 /* If vector is too small, get a bigger one. */
4636 if (i
+ 3 > XVECTOR (menu_bar_items_vector
)->size
)
4639 int newsize
= 2 * i
;
4640 tem
= Fmake_vector (make_number (2 * i
), Qnil
);
4641 bcopy (XVECTOR (menu_bar_items_vector
)->contents
,
4642 XVECTOR (tem
)->contents
, i
* sizeof (Lisp_Object
));
4643 menu_bar_items_vector
= tem
;
4645 /* Add this item. */
4646 XVECTOR (menu_bar_items_vector
)->contents
[i
++] = key
;
4647 XVECTOR (menu_bar_items_vector
)->contents
[i
++] = item_string
;
4648 XVECTOR (menu_bar_items_vector
)->contents
[i
++] = Fcons (def
, Qnil
);
4649 menu_bar_items_index
= i
;
4651 /* We did find an item for this KEY. Add DEF to its list of maps. */
4655 old
= XVECTOR (menu_bar_items_vector
)->contents
[i
+ 2];
4656 XVECTOR (menu_bar_items_vector
)->contents
[i
+ 2] = Fcons (def
, old
);
4660 /* Read a character using menus based on maps in the array MAPS.
4661 NMAPS is the length of MAPS. Return nil if there are no menus in the maps.
4662 Return t if we displayed a menu but the user rejected it.
4664 PREV_EVENT is the previous input event, or nil if we are reading
4665 the first event of a key sequence.
4667 If USED_MOUSE_MENU is non-null, then we set *USED_MOUSE_MENU to 1
4668 if we used a mouse menu to read the input, or zero otherwise. If
4669 USED_MOUSE_MENU is null, we don't dereference it.
4671 The prompting is done based on the prompt-string of the map
4672 and the strings associated with various map elements.
4674 This can be done with X menus or with menus put in the minibuf.
4675 These are done in different ways, depending on how the input will be read.
4676 Menus using X are done after auto-saving in read-char, getting the input
4677 event from Fx_popup_menu; menus using the minibuf use read_char recursively
4678 and do auto-saving in the inner call of read_char. */
4681 read_char_x_menu_prompt (nmaps
, maps
, prev_event
, used_mouse_menu
)
4684 Lisp_Object prev_event
;
4685 int *used_mouse_menu
;
4688 register Lisp_Object name
;
4689 Lisp_Object rest
, vector
;
4691 if (used_mouse_menu
)
4692 *used_mouse_menu
= 0;
4694 /* Use local over global Menu maps */
4696 if (! menu_prompting
)
4699 /* Optionally disregard all but the global map. */
4700 if (inhibit_local_menu_bar_menus
)
4702 maps
+= (nmaps
- 1);
4706 /* Get the menu name from the first map that has one (a prompt string). */
4707 for (mapno
= 0; mapno
< nmaps
; mapno
++)
4709 name
= map_prompt (maps
[mapno
]);
4714 /* If we don't have any menus, just read a character normally. */
4718 #if (defined (HAVE_X_WINDOWS) && defined (HAVE_X_MENU)) || defined (MSDOS)
4719 /* If we got to this point via a mouse click,
4720 use a real menu for mouse selection. */
4721 if (EVENT_HAS_PARAMETERS (prev_event
)
4722 && !EQ (XCONS (prev_event
)->car
, Qmenu_bar
))
4724 /* Display the menu and get the selection. */
4725 Lisp_Object
*realmaps
4726 = (Lisp_Object
*) alloca (nmaps
* sizeof (Lisp_Object
));
4730 /* Use the maps that are not nil. */
4731 for (mapno
= 0; mapno
< nmaps
; mapno
++)
4732 if (!NILP (maps
[mapno
]))
4733 realmaps
[nmaps1
++] = maps
[mapno
];
4735 value
= Fx_popup_menu (prev_event
, Flist (nmaps1
, realmaps
));
4738 /* If we got more than one event, put all but the first
4739 onto this list to be read later.
4740 Return just the first event now. */
4741 Vunread_command_events
4742 = nconc2 (XCONS (value
)->cdr
, Vunread_command_events
);
4743 value
= XCONS (value
)->car
;
4745 else if (NILP (value
))
4747 if (used_mouse_menu
)
4748 *used_mouse_menu
= 1;
4751 #endif /* (HAVE_X_WINDOWS && HAVE_X_MENU) || MSDOS */
4756 read_char_minibuf_menu_prompt (commandflag
, nmaps
, maps
)
4762 register Lisp_Object name
;
4764 int width
= FRAME_WIDTH (selected_frame
) - 4;
4765 char *menu
= (char *) alloca (width
+ 4);
4768 Lisp_Object rest
, vector
;
4770 if (! menu_prompting
)
4773 /* Get the menu name from the first map that has one (a prompt string). */
4774 for (mapno
= 0; mapno
< nmaps
; mapno
++)
4776 name
= map_prompt (maps
[mapno
]);
4781 /* If we don't have any menus, just read a character normally. */
4785 /* Prompt string always starts with map's prompt, and a space. */
4786 strcpy (menu
, XSTRING (name
)->data
);
4787 nlength
= XSTRING (name
)->size
;
4788 menu
[nlength
++] = ':';
4789 menu
[nlength
++] = ' ';
4792 /* Start prompting at start of first map. */
4796 /* Present the documented bindings, a line at a time. */
4803 Lisp_Object orig_defn_macro
;
4805 /* Loop over elements of map. */
4810 /* If reached end of map, start at beginning of next map. */
4814 /* At end of last map, wrap around to first map if just starting,
4815 or end this line if already have something on it. */
4819 if (notfirst
|| nobindings
) break;
4824 /* Look at the next element of the map. */
4826 elt
= XVECTOR (vector
)->contents
[idx
];
4828 elt
= Fcar_safe (rest
);
4830 if (idx
< 0 && VECTORP (elt
))
4832 /* If we found a dense table in the keymap,
4833 advanced past it, but start scanning its contents. */
4834 rest
= Fcdr_safe (rest
);
4840 /* An ordinary element. */
4845 s
= Fcar_safe (Fcdr_safe (elt
)); /* alist */
4846 event
= Fcar_safe (elt
);
4850 s
= Fcar_safe (elt
); /* vector */
4851 XSETINT (event
, idx
);
4854 /* Ignore the element if it has no prompt string. */
4855 if (STRINGP (s
) && INTEGERP (event
))
4857 /* 1 if the char to type matches the string. */
4859 Lisp_Object upcased_event
, downcased_event
;
4862 upcased_event
= Fupcase (event
);
4863 downcased_event
= Fdowncase (event
);
4864 char_matches
= (XINT (upcased_event
) == XSTRING (s
)->data
[0]
4865 || XINT (downcased_event
) == XSTRING (s
)->data
[0]);
4867 desc
= Fsingle_key_description (event
);
4869 /* If we have room for the prompt string, add it to this line.
4870 If this is the first on the line, always add it. */
4871 if ((XSTRING (s
)->size
+ i
+ 2
4872 + (char_matches
? 0 : XSTRING (desc
)->size
+ 3))
4878 /* Punctuate between strings. */
4881 strcpy (menu
+ i
, ", ");
4887 /* If the char to type doesn't match the string's
4888 first char, explicitly show what char to type. */
4891 /* Add as much of string as fits. */
4892 thiswidth
= XSTRING (desc
)->size
;
4893 if (thiswidth
+ i
> width
)
4894 thiswidth
= width
- i
;
4895 bcopy (XSTRING (desc
)->data
, menu
+ i
, thiswidth
);
4897 strcpy (menu
+ i
, " = ");
4901 /* Add as much of string as fits. */
4902 thiswidth
= XSTRING (s
)->size
;
4903 if (thiswidth
+ i
> width
)
4904 thiswidth
= width
- i
;
4905 bcopy (XSTRING (s
)->data
, menu
+ i
, thiswidth
);
4911 /* If this element does not fit, end the line now,
4912 and save the element for the next line. */
4913 strcpy (menu
+ i
, "...");
4918 /* Move past this element. */
4919 if (idx
>= 0 && idx
+ 1 >= XVECTOR (vector
)->size
)
4920 /* Handle reaching end of dense table. */
4925 rest
= Fcdr_safe (rest
);
4929 /* Prompt with that and read response. */
4932 /* Make believe its not a keyboard macro in case the help char
4933 is pressed. Help characters are not recorded because menu prompting
4934 is not used on replay.
4936 orig_defn_macro
= current_kboard
->defining_kbd_macro
;
4937 current_kboard
->defining_kbd_macro
= Qnil
;
4939 obj
= read_char (commandflag
, 0, 0, Qnil
, 0);
4940 while (BUFFERP (obj
));
4941 current_kboard
->defining_kbd_macro
= orig_defn_macro
;
4943 if (!INTEGERP (obj
))
4948 if (! EQ (obj
, menu_prompt_more_char
)
4949 && (!INTEGERP (menu_prompt_more_char
)
4950 || ! EQ (obj
, make_number (Ctl (XINT (menu_prompt_more_char
))))))
4952 if (!NILP (current_kboard
->defining_kbd_macro
))
4953 store_kbd_macro_char (obj
);
4956 /* Help char - go round again */
4960 /* Reading key sequences. */
4962 /* Follow KEY in the maps in CURRENT[0..NMAPS-1], placing its bindings
4963 in DEFS[0..NMAPS-1]. Set NEXT[i] to DEFS[i] if DEFS[i] is a
4964 keymap, or nil otherwise. Return the index of the first keymap in
4965 which KEY has any binding, or NMAPS if no map has a binding.
4967 If KEY is a meta ASCII character, treat it like meta-prefix-char
4968 followed by the corresponding non-meta character. Keymaps in
4969 CURRENT with non-prefix bindings for meta-prefix-char become nil in
4972 If KEY has no bindings in any of the CURRENT maps, NEXT is left
4975 NEXT may == CURRENT. */
4978 follow_key (key
, nmaps
, current
, defs
, next
)
4980 Lisp_Object
*current
, *defs
, *next
;
4983 int i
, first_binding
;
4985 /* If KEY is a meta ASCII character, treat it like meta-prefix-char
4986 followed by the corresponding non-meta character. */
4987 if (INTEGERP (key
) && (XINT (key
) & CHAR_META
))
4989 for (i
= 0; i
< nmaps
; i
++)
4990 if (! NILP (current
[i
]))
4993 get_keyelt (access_keymap (current
[i
], meta_prefix_char
, 1, 0));
4995 /* Note that since we pass the resulting bindings through
4996 get_keymap_1, non-prefix bindings for meta-prefix-char
4998 next
[i
] = get_keymap_1 (next
[i
], 0, 1);
5004 XSETINT (key
, XFASTINT (key
) & ~CHAR_META
);
5007 first_binding
= nmaps
;
5008 for (i
= nmaps
- 1; i
>= 0; i
--)
5010 if (! NILP (current
[i
]))
5012 defs
[i
] = get_keyelt (access_keymap (current
[i
], key
, 1, 0));
5013 if (! NILP (defs
[i
]))
5020 /* Given the set of bindings we've found, produce the next set of maps. */
5021 if (first_binding
< nmaps
)
5022 for (i
= 0; i
< nmaps
; i
++)
5023 next
[i
] = NILP (defs
[i
]) ? Qnil
: get_keymap_1 (defs
[i
], 0, 1);
5025 return first_binding
;
5028 /* Read a sequence of keys that ends with a non prefix character,
5029 storing it in KEYBUF, a buffer of size BUFSIZE.
5031 Return the length of the key sequence stored.
5032 Return -1 if the user rejected a command menu.
5034 Echo starting immediately unless `prompt' is 0.
5036 Where a key sequence ends depends on the currently active keymaps.
5037 These include any minor mode keymaps active in the current buffer,
5038 the current buffer's local map, and the global map.
5040 If a key sequence has no other bindings, we check Vfunction_key_map
5041 to see if some trailing subsequence might be the beginning of a
5042 function key's sequence. If so, we try to read the whole function
5043 key, and substitute its symbolic name into the key sequence.
5045 We ignore unbound `down-' mouse clicks. We turn unbound `drag-' and
5046 `double-' events into similar click events, if that would make them
5047 bound. We try to turn `triple-' events first into `double-' events,
5050 If we get a mouse click in a mode line, vertical divider, or other
5051 non-text area, we treat the click as if it were prefixed by the
5052 symbol denoting that area - `mode-line', `vertical-line', or
5055 If the sequence starts with a mouse click, we read the key sequence
5056 with respect to the buffer clicked on, not the current buffer.
5058 If the user switches frames in the midst of a key sequence, we put
5059 off the switch-frame event until later; the next call to
5060 read_char will return it. */
5063 read_key_sequence (keybuf
, bufsize
, prompt
, dont_downcase_last
,
5064 can_return_switch_frame
)
5065 Lisp_Object
*keybuf
;
5068 int dont_downcase_last
;
5069 int can_return_switch_frame
;
5071 int count
= specpdl_ptr
- specpdl
;
5073 /* How many keys there are in the current key sequence. */
5076 /* The length of the echo buffer when we started reading, and
5077 the length of this_command_keys when we started reading. */
5081 /* The number of keymaps we're scanning right now, and the number of
5082 keymaps we have allocated space for. */
5084 int nmaps_allocated
= 0;
5086 /* defs[0..nmaps-1] are the definitions of KEYBUF[0..t-1] in
5087 the current keymaps. */
5090 /* submaps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
5091 in the current keymaps, or nil where it is not a prefix. */
5092 Lisp_Object
*submaps
;
5094 /* The local map to start out with at start of key sequence. */
5095 Lisp_Object orig_local_map
;
5097 /* 1 if we have already considered switching to the local-map property
5098 of the place where a mouse click occurred. */
5099 int localized_local_map
= 0;
5101 /* The index in defs[] of the first keymap that has a binding for
5102 this key sequence. In other words, the lowest i such that
5103 defs[i] is non-nil. */
5106 /* If t < mock_input, then KEYBUF[t] should be read as the next
5109 We use this to recover after recognizing a function key. Once we
5110 realize that a suffix of the current key sequence is actually a
5111 function key's escape sequence, we replace the suffix with the
5112 function key's binding from Vfunction_key_map. Now keybuf
5113 contains a new and different key sequence, so the echo area,
5114 this_command_keys, and the submaps and defs arrays are wrong. In
5115 this situation, we set mock_input to t, set t to 0, and jump to
5116 restart_sequence; the loop will read keys from keybuf up until
5117 mock_input, thus rebuilding the state; and then it will resume
5118 reading characters from the keyboard. */
5121 /* If the sequence is unbound in submaps[], then
5122 keybuf[fkey_start..fkey_end-1] is a prefix in Vfunction_key_map,
5123 and fkey_map is its binding.
5125 These might be > t, indicating that all function key scanning
5126 should hold off until t reaches them. We do this when we've just
5127 recognized a function key, to avoid searching for the function
5128 key's again in Vfunction_key_map. */
5129 int fkey_start
= 0, fkey_end
= 0;
5130 Lisp_Object fkey_map
;
5132 /* Likewise, for key_translation_map. */
5133 int keytran_start
= 0, keytran_end
= 0;
5134 Lisp_Object keytran_map
;
5136 /* If we receive a ``switch-frame'' event in the middle of a key sequence,
5137 we put it off for later. While we're reading, we keep the event here. */
5138 Lisp_Object delayed_switch_frame
;
5140 /* See the comment below... */
5141 #if defined (GOBBLE_FIRST_EVENT)
5142 Lisp_Object first_event
;
5145 Lisp_Object original_uppercase
;
5146 int original_uppercase_position
= -1;
5148 /* Gets around Microsoft compiler limitations. */
5151 struct buffer
*starting_buffer
;
5153 /* Nonzero if we seem to have got the beginning of a binding
5154 in function_key_map. */
5155 int function_key_possible
= 0;
5156 int key_translation_possible
= 0;
5160 last_nonmenu_event
= Qnil
;
5162 delayed_switch_frame
= Qnil
;
5163 fkey_map
= Vfunction_key_map
;
5164 keytran_map
= Vkey_translation_map
;
5166 /* If there is no function-key-map, turn off function key scanning. */
5167 if (NILP (Fkeymapp (Vfunction_key_map
)))
5168 fkey_start
= fkey_end
= bufsize
+ 1;
5170 /* If there is no key-translation-map, turn off scanning. */
5171 if (NILP (Fkeymapp (Vkey_translation_map
)))
5172 keytran_start
= keytran_end
= bufsize
+ 1;
5177 echo_prompt (XSTRING (prompt
)->data
);
5178 else if (cursor_in_echo_area
&& echo_keystrokes
)
5179 /* This doesn't put in a dash if the echo buffer is empty, so
5180 you don't always see a dash hanging out in the minibuffer. */
5184 /* Record the initial state of the echo area and this_command_keys;
5185 we will need to restore them if we replay a key sequence. */
5187 echo_start
= echo_length ();
5188 keys_start
= this_command_key_count
;
5190 #if defined (GOBBLE_FIRST_EVENT)
5191 /* This doesn't quite work, because some of the things that read_char
5192 does cannot safely be bypassed. It seems too risky to try to make
5195 /* Read the first char of the sequence specially, before setting
5196 up any keymaps, in case a filter runs and switches buffers on us. */
5197 first_event
= read_char (NILP (prompt
), 0, submaps
, last_nonmenu_event
,
5199 #endif /* GOBBLE_FIRST_EVENT */
5201 orig_local_map
= get_local_map (PT
, current_buffer
);
5203 /* We jump here when the key sequence has been thoroughly changed, and
5204 we need to rescan it starting from the beginning. When we jump here,
5205 keybuf[0..mock_input] holds the sequence we should reread. */
5208 starting_buffer
= current_buffer
;
5209 function_key_possible
= 0;
5210 key_translation_possible
= 0;
5212 /* Build our list of keymaps.
5213 If we recognize a function key and replace its escape sequence in
5214 keybuf with its symbol, or if the sequence starts with a mouse
5215 click and we need to switch buffers, we jump back here to rebuild
5216 the initial keymaps from the current buffer. */
5220 if (!NILP (current_kboard
->Voverriding_terminal_local_map
)
5221 || !NILP (Voverriding_local_map
))
5223 if (3 > nmaps_allocated
)
5225 submaps
= (Lisp_Object
*) alloca (3 * sizeof (submaps
[0]));
5226 defs
= (Lisp_Object
*) alloca (3 * sizeof (defs
[0]));
5227 nmaps_allocated
= 3;
5230 if (!NILP (current_kboard
->Voverriding_terminal_local_map
))
5231 submaps
[nmaps
++] = current_kboard
->Voverriding_terminal_local_map
;
5232 if (!NILP (Voverriding_local_map
))
5233 submaps
[nmaps
++] = Voverriding_local_map
;
5237 nmaps
= current_minor_maps (0, &maps
);
5238 if (nmaps
+ 2 > nmaps_allocated
)
5240 submaps
= (Lisp_Object
*) alloca ((nmaps
+2) * sizeof (submaps
[0]));
5241 defs
= (Lisp_Object
*) alloca ((nmaps
+2) * sizeof (defs
[0]));
5242 nmaps_allocated
= nmaps
+ 2;
5244 bcopy (maps
, submaps
, nmaps
* sizeof (submaps
[0]));
5245 #ifdef USE_TEXT_PROPERTIES
5246 submaps
[nmaps
++] = orig_local_map
;
5248 submaps
[nmaps
++] = current_buffer
->keymap
;
5251 submaps
[nmaps
++] = current_global_map
;
5254 /* Find an accurate initial value for first_binding. */
5255 for (first_binding
= 0; first_binding
< nmaps
; first_binding
++)
5256 if (! NILP (submaps
[first_binding
]))
5259 /* Start from the beginning in keybuf. */
5262 /* These are no-ops the first time through, but if we restart, they
5263 revert the echo area and this_command_keys to their original state. */
5264 this_command_key_count
= keys_start
;
5265 if (INTERACTIVE
&& t
< mock_input
)
5266 echo_truncate (echo_start
);
5268 /* If the best binding for the current key sequence is a keymap, or
5269 we may be looking at a function key's escape sequence, keep on
5271 while ((first_binding
< nmaps
&& ! NILP (submaps
[first_binding
]))
5272 || (first_binding
>= nmaps
5274 /* mock input is never part of a function key's sequence. */
5275 && mock_input
<= fkey_start
)
5276 || (first_binding
>= nmaps
5277 && keytran_start
< t
&& key_translation_possible
)
5278 /* Don't return in the middle of a possible function key sequence,
5279 if the only bindings we found were via case conversion.
5280 Thus, if ESC O a has a function-key-map translation
5281 and ESC o has a binding, don't return after ESC O,
5282 so that we can translate ESC O plus the next character. */
5286 int used_mouse_menu
= 0;
5288 /* Where the last real key started. If we need to throw away a
5289 key that has expanded into more than one element of keybuf
5290 (say, a mouse click on the mode line which is being treated
5291 as [mode-line (mouse-...)], then we backtrack to this point
5293 int last_real_key_start
;
5295 /* These variables are analogous to echo_start and keys_start;
5296 while those allow us to restart the entire key sequence,
5297 echo_local_start and keys_local_start allow us to throw away
5299 int echo_local_start
, keys_local_start
, local_first_binding
;
5302 error ("key sequence too long");
5305 echo_local_start
= echo_length ();
5306 keys_local_start
= this_command_key_count
;
5307 local_first_binding
= first_binding
;
5310 /* These are no-ops, unless we throw away a keystroke below and
5311 jumped back up to replay_key; in that case, these restore the
5312 variables to their original state, allowing us to replay the
5314 if (INTERACTIVE
&& t
< mock_input
)
5315 echo_truncate (echo_local_start
);
5316 this_command_key_count
= keys_local_start
;
5317 first_binding
= local_first_binding
;
5319 /* By default, assume each event is "real". */
5320 last_real_key_start
= t
;
5322 /* Does mock_input indicate that we are re-reading a key sequence? */
5326 add_command_key (key
);
5327 if (echo_keystrokes
)
5331 /* If not, we should actually read a character. */
5334 struct buffer
*buf
= current_buffer
;
5338 KBOARD
*interrupted_kboard
= current_kboard
;
5339 struct frame
*interrupted_frame
= selected_frame
;
5340 if (setjmp (wrong_kboard_jmpbuf
))
5342 if (!NILP (delayed_switch_frame
))
5344 interrupted_kboard
->kbd_queue
5345 = Fcons (delayed_switch_frame
,
5346 interrupted_kboard
->kbd_queue
);
5347 delayed_switch_frame
= Qnil
;
5350 interrupted_kboard
->kbd_queue
5351 = Fcons (keybuf
[--t
], interrupted_kboard
->kbd_queue
);
5353 /* If the side queue is non-empty, ensure it begins with a
5354 switch-frame, so we'll replay it in the right context. */
5355 if (CONSP (interrupted_kboard
->kbd_queue
)
5356 && (key
= XCONS (interrupted_kboard
->kbd_queue
)->car
,
5357 !(EVENT_HAS_PARAMETERS (key
)
5358 && EQ (EVENT_HEAD_KIND (EVENT_HEAD (key
)),
5362 XSETFRAME (frame
, interrupted_frame
);
5363 interrupted_kboard
->kbd_queue
5364 = Fcons (make_lispy_switch_frame (frame
),
5365 interrupted_kboard
->kbd_queue
);
5368 orig_local_map
= get_local_map (PT
, current_buffer
);
5369 goto replay_sequence
;
5372 key
= read_char (NILP (prompt
), nmaps
, submaps
, last_nonmenu_event
,
5376 /* read_char returns t when it shows a menu and the user rejects it.
5381 /* read_char returns -1 at the end of a macro.
5382 Emacs 18 handles this by returning immediately with a
5383 zero, so that's what we'll do. */
5384 if (INTEGERP (key
) && XINT (key
) == -1)
5387 /* The Microsoft C compiler can't handle the goto that
5393 /* If the current buffer has been changed from under us, the
5394 keymap may have changed, so replay the sequence. */
5398 orig_local_map
= get_local_map (PT
, current_buffer
);
5399 goto replay_sequence
;
5402 /* If we have a quit that was typed in another frame, and
5403 quit_throw_to_read_char switched buffers,
5404 replay to get the right keymap. */
5405 if (XINT (key
) == quit_char
&& current_buffer
!= starting_buffer
)
5410 orig_local_map
= get_local_map (PT
, current_buffer
);
5411 goto replay_sequence
;
5417 /* Clicks in non-text areas get prefixed by the symbol
5418 in their CHAR-ADDRESS field. For example, a click on
5419 the mode line is prefixed by the symbol `mode-line'.
5421 Furthermore, key sequences beginning with mouse clicks
5422 are read using the keymaps of the buffer clicked on, not
5423 the current buffer. So we may have to switch the buffer
5426 When we turn one event into two events, we must make sure
5427 that neither of the two looks like the original--so that,
5428 if we replay the events, they won't be expanded again.
5429 If not for this, such reexpansion could happen either here
5430 or when user programs play with this-command-keys. */
5431 if (EVENT_HAS_PARAMETERS (key
))
5435 kind
= EVENT_HEAD_KIND (EVENT_HEAD (key
));
5436 if (EQ (kind
, Qmouse_click
))
5438 Lisp_Object window
, posn
;
5440 window
= POSN_WINDOW (EVENT_START (key
));
5441 posn
= POSN_BUFFER_POSN (EVENT_START (key
));
5444 /* We're looking at the second event of a
5445 sequence which we expanded before. Set
5446 last_real_key_start appropriately. */
5448 last_real_key_start
= t
- 1;
5451 /* Key sequences beginning with mouse clicks are
5452 read using the keymaps in the buffer clicked on,
5453 not the current buffer. If we're at the
5454 beginning of a key sequence, switch buffers. */
5455 if (last_real_key_start
== 0
5457 && BUFFERP (XWINDOW (window
)->buffer
)
5458 && XBUFFER (XWINDOW (window
)->buffer
) != current_buffer
)
5463 /* Arrange to go back to the original buffer once we're
5464 done reading the key sequence. Note that we can't
5465 use save_excursion_{save,restore} here, because they
5466 save point as well as the current buffer; we don't
5467 want to save point, because redisplay may change it,
5468 to accommodate a Fset_window_start or something. We
5469 don't want to do this at the top of the function,
5470 because we may get input from a subprocess which
5471 wants to change the selected window and stuff (say,
5473 record_unwind_protect (Fset_buffer
, Fcurrent_buffer ());
5475 set_buffer_internal (XBUFFER (XWINDOW (window
)->buffer
));
5476 orig_local_map
= get_local_map (PT
, current_buffer
);
5477 goto replay_sequence
;
5479 /* For a mouse click, get the local text-property keymap
5480 of the place clicked on, rather than point. */
5481 if (last_real_key_start
== 0 && CONSP (XCONS (key
)->cdr
)
5482 && ! localized_local_map
)
5484 Lisp_Object map_here
, start
, pos
;
5486 localized_local_map
= 1;
5487 start
= EVENT_START (key
);
5488 if (CONSP (start
) && CONSP (XCONS (start
)->cdr
))
5490 pos
= POSN_BUFFER_POSN (start
);
5493 map_here
= get_local_map (XINT (pos
), current_buffer
);
5494 if (!EQ (map_here
, orig_local_map
))
5496 orig_local_map
= map_here
;
5500 goto replay_sequence
;
5506 /* Expand mode-line and scroll-bar events into two events:
5507 use posn as a fake prefix key. */
5510 if (t
+ 1 >= bufsize
)
5511 error ("key sequence too long");
5516 /* Zap the position in key, so we know that we've
5517 expanded it, and don't try to do so again. */
5518 POSN_BUFFER_POSN (EVENT_START (key
))
5519 = Fcons (posn
, Qnil
);
5523 else if (EQ (kind
, Qswitch_frame
))
5525 /* If we're at the beginning of a key sequence, and the caller
5526 says it's okay, go ahead and return this event. If we're
5527 in the midst of a key sequence, delay it until the end. */
5528 if (t
> 0 || !can_return_switch_frame
)
5530 delayed_switch_frame
= key
;
5534 else if (CONSP (XCONS (key
)->cdr
)
5535 && CONSP (EVENT_START (key
))
5536 && CONSP (XCONS (EVENT_START (key
))->cdr
))
5540 posn
= POSN_BUFFER_POSN (EVENT_START (key
));
5541 /* Handle menu-bar events:
5542 insert the dummy prefix event `menu-bar'. */
5543 if (EQ (posn
, Qmenu_bar
))
5545 if (t
+ 1 >= bufsize
)
5546 error ("key sequence too long");
5550 /* Zap the position in key, so we know that we've
5551 expanded it, and don't try to do so again. */
5552 POSN_BUFFER_POSN (EVENT_START (key
))
5553 = Fcons (posn
, Qnil
);
5556 goto replay_sequence
;
5558 else if (CONSP (posn
))
5560 /* We're looking at the second event of a
5561 sequence which we expanded before. Set
5562 last_real_key_start appropriately. */
5563 if (last_real_key_start
== t
&& t
> 0)
5564 last_real_key_start
= t
- 1;
5569 /* We have finally decided that KEY is something we might want
5571 first_binding
= (follow_key (key
,
5572 nmaps
- first_binding
,
5573 submaps
+ first_binding
,
5574 defs
+ first_binding
,
5575 submaps
+ first_binding
)
5578 /* If KEY wasn't bound, we'll try some fallbacks. */
5579 if (first_binding
>= nmaps
)
5583 head
= EVENT_HEAD (key
);
5584 if (help_char_p (head
) && t
> 0)
5586 read_key_sequence_cmd
= Vprefix_help_command
;
5588 last_nonmenu_event
= key
;
5589 /* The Microsoft C compiler can't handle the goto that
5597 Lisp_Object breakdown
;
5600 breakdown
= parse_modifiers (head
);
5601 modifiers
= XINT (XCONS (XCONS (breakdown
)->cdr
)->car
);
5602 /* Attempt to reduce an unbound mouse event to a simpler
5603 event that is bound:
5604 Drags reduce to clicks.
5605 Double-clicks reduce to clicks.
5606 Triple-clicks reduce to double-clicks, then to clicks.
5607 Down-clicks are eliminated.
5608 Double-downs reduce to downs, then are eliminated.
5609 Triple-downs reduce to double-downs, then to downs,
5610 then are eliminated. */
5611 if (modifiers
& (down_modifier
| drag_modifier
5612 | double_modifier
| triple_modifier
))
5614 while (modifiers
& (down_modifier
| drag_modifier
5615 | double_modifier
| triple_modifier
))
5617 Lisp_Object new_head
, new_click
;
5618 if (modifiers
& triple_modifier
)
5619 modifiers
^= (double_modifier
| triple_modifier
);
5620 else if (modifiers
& double_modifier
)
5621 modifiers
&= ~double_modifier
;
5622 else if (modifiers
& drag_modifier
)
5623 modifiers
&= ~drag_modifier
;
5626 /* Dispose of this `down' event by simply jumping
5627 back to replay_key, to get another event.
5629 Note that if this event came from mock input,
5630 then just jumping back to replay_key will just
5631 hand it to us again. So we have to wipe out any
5634 We could delete keybuf[t] and shift everything
5635 after that to the left by one spot, but we'd also
5636 have to fix up any variable that points into
5637 keybuf, and shifting isn't really necessary
5640 Adding prefixes for non-textual mouse clicks
5641 creates two characters of mock input, and both
5642 must be thrown away. If we're only looking at
5643 the prefix now, we can just jump back to
5644 replay_key. On the other hand, if we've already
5645 processed the prefix, and now the actual click
5646 itself is giving us trouble, then we've lost the
5647 state of the keymaps we want to backtrack to, and
5648 we need to replay the whole sequence to rebuild
5651 Beyond that, only function key expansion could
5652 create more than two keys, but that should never
5653 generate mouse events, so it's okay to zero
5654 mock_input in that case too.
5656 Isn't this just the most wonderful code ever? */
5657 if (t
== last_real_key_start
)
5664 mock_input
= last_real_key_start
;
5665 goto replay_sequence
;
5670 = apply_modifiers (modifiers
, XCONS (breakdown
)->car
);
5672 = Fcons (new_head
, Fcons (EVENT_START (key
), Qnil
));
5674 /* Look for a binding for this new key. follow_key
5675 promises that it didn't munge submaps the
5676 last time we called it, since key was unbound. */
5678 = (follow_key (new_click
,
5679 nmaps
- local_first_binding
,
5680 submaps
+ local_first_binding
,
5681 defs
+ local_first_binding
,
5682 submaps
+ local_first_binding
)
5683 + local_first_binding
);
5685 /* If that click is bound, go for it. */
5686 if (first_binding
< nmaps
)
5691 /* Otherwise, we'll leave key set to the drag event. */
5698 /* Normally, last_nonmenu_event gets the previous key we read.
5699 But when a mouse popup menu is being used,
5700 we don't update last_nonmenu_event; it continues to hold the mouse
5701 event that preceded the first level of menu. */
5702 if (!used_mouse_menu
)
5703 last_nonmenu_event
= key
;
5705 /* If the sequence is unbound, see if we can hang a function key
5706 off the end of it. We only want to scan real keyboard input
5707 for function key sequences, so if mock_input says that we're
5708 re-reading old events, don't examine it. */
5709 if (first_binding
>= nmaps
5712 Lisp_Object fkey_next
;
5714 /* Continue scan from fkey_end until we find a bound suffix.
5715 If we fail, increment fkey_start
5716 and start fkey_end from there. */
5717 while (fkey_end
< t
)
5721 key
= keybuf
[fkey_end
++];
5722 /* Look up meta-characters by prefixing them
5723 with meta_prefix_char. I hate this. */
5724 if (INTEGERP (key
) && XINT (key
) & meta_modifier
)
5729 (access_keymap (fkey_map
, meta_prefix_char
, 1, 0)),
5731 XSETFASTINT (key
, XFASTINT (key
) & ~meta_modifier
);
5734 fkey_next
= fkey_map
;
5737 = get_keyelt (access_keymap (fkey_next
, key
, 1, 0));
5739 #if 0 /* I didn't turn this on, because it might cause trouble
5740 for the mapping of return into C-m and tab into C-i. */
5741 /* Optionally don't map function keys into other things.
5742 This enables the user to redefine kp- keys easily. */
5743 if (SYMBOLP (key
) && !NILP (Vinhibit_function_key_mapping
))
5747 /* If the function key map gives a function, not an
5748 array, then call the function with no args and use
5749 its value instead. */
5750 if (SYMBOLP (fkey_next
) && ! NILP (Ffboundp (fkey_next
))
5753 struct gcpro gcpro1
, gcpro2
, gcpro3
;
5757 GCPRO3 (fkey_map
, keytran_map
, delayed_switch_frame
);
5758 fkey_next
= call1 (fkey_next
, prompt
);
5760 /* If the function returned something invalid,
5761 barf--don't ignore it.
5762 (To ignore it safely, we would need to gcpro a bunch of
5763 other variables.) */
5764 if (! (VECTORP (fkey_next
) || STRINGP (fkey_next
)))
5765 error ("Function in function-key-map returns invalid key sequence");
5768 function_key_possible
= ! NILP (fkey_next
);
5770 /* If keybuf[fkey_start..fkey_end] is bound in the
5771 function key map and it's a suffix of the current
5772 sequence (i.e. fkey_end == t), replace it with
5773 the binding and restart with fkey_start at the end. */
5774 if ((VECTORP (fkey_next
) || STRINGP (fkey_next
))
5777 int len
= XFASTINT (Flength (fkey_next
));
5779 t
= fkey_start
+ len
;
5781 error ("key sequence too long");
5783 if (VECTORP (fkey_next
))
5784 bcopy (XVECTOR (fkey_next
)->contents
,
5785 keybuf
+ fkey_start
,
5786 (t
- fkey_start
) * sizeof (keybuf
[0]));
5787 else if (STRINGP (fkey_next
))
5791 for (i
= 0; i
< len
; i
++)
5792 XSETFASTINT (keybuf
[fkey_start
+ i
],
5793 XSTRING (fkey_next
)->data
[i
]);
5797 fkey_start
= fkey_end
= t
;
5798 fkey_map
= Vfunction_key_map
;
5800 /* Do pass the results through key-translation-map. */
5801 keytran_start
= keytran_end
= 0;
5802 keytran_map
= Vkey_translation_map
;
5804 goto replay_sequence
;
5807 fkey_map
= get_keymap_1 (fkey_next
, 0, 1);
5809 /* If we no longer have a bound suffix, try a new positions for
5811 if (NILP (fkey_map
))
5813 fkey_end
= ++fkey_start
;
5814 fkey_map
= Vfunction_key_map
;
5815 function_key_possible
= 0;
5820 /* Look for this sequence in key-translation-map. */
5822 Lisp_Object keytran_next
;
5824 /* Scan from keytran_end until we find a bound suffix. */
5825 while (keytran_end
< t
)
5829 key
= keybuf
[keytran_end
++];
5830 /* Look up meta-characters by prefixing them
5831 with meta_prefix_char. I hate this. */
5832 if (INTEGERP (key
) && XINT (key
) & meta_modifier
)
5837 (access_keymap (keytran_map
, meta_prefix_char
, 1, 0)),
5839 XSETFASTINT (key
, XFASTINT (key
) & ~meta_modifier
);
5842 keytran_next
= keytran_map
;
5845 = get_keyelt (access_keymap (keytran_next
, key
, 1, 0));
5847 /* If the key translation map gives a function, not an
5848 array, then call the function with no args and use
5849 its value instead. */
5850 if (SYMBOLP (keytran_next
) && ! NILP (Ffboundp (keytran_next
))
5851 && keytran_end
== t
)
5853 struct gcpro gcpro1
, gcpro2
, gcpro3
;
5857 GCPRO3 (fkey_map
, keytran_map
, delayed_switch_frame
);
5858 keytran_next
= call1 (keytran_next
, prompt
);
5860 /* If the function returned something invalid,
5861 barf--don't ignore it.
5862 (To ignore it safely, we would need to gcpro a bunch of
5863 other variables.) */
5864 if (! (VECTORP (keytran_next
) || STRINGP (keytran_next
)))
5865 error ("Function in key-translation-map returns invalid key sequence");
5868 key_translation_possible
= ! NILP (keytran_next
);
5870 /* If keybuf[keytran_start..keytran_end] is bound in the
5871 key translation map and it's a suffix of the current
5872 sequence (i.e. keytran_end == t), replace it with
5873 the binding and restart with keytran_start at the end. */
5874 if ((VECTORP (keytran_next
) || STRINGP (keytran_next
))
5875 && keytran_end
== t
)
5877 int len
= XFASTINT (Flength (keytran_next
));
5879 t
= keytran_start
+ len
;
5881 error ("key sequence too long");
5883 if (VECTORP (keytran_next
))
5884 bcopy (XVECTOR (keytran_next
)->contents
,
5885 keybuf
+ keytran_start
,
5886 (t
- keytran_start
) * sizeof (keybuf
[0]));
5887 else if (STRINGP (keytran_next
))
5891 for (i
= 0; i
< len
; i
++)
5892 XSETFASTINT (keybuf
[keytran_start
+ i
],
5893 XSTRING (keytran_next
)->data
[i
]);
5897 keytran_start
= keytran_end
= t
;
5898 keytran_map
= Vkey_translation_map
;
5900 /* Don't pass the results of key-translation-map
5901 through function-key-map. */
5902 fkey_start
= fkey_end
= t
;
5903 fkey_map
= Vkey_translation_map
;
5905 goto replay_sequence
;
5908 keytran_map
= get_keymap_1 (keytran_next
, 0, 1);
5910 /* If we no longer have a bound suffix, try a new positions for
5912 if (NILP (keytran_map
))
5914 keytran_end
= ++keytran_start
;
5915 keytran_map
= Vkey_translation_map
;
5916 key_translation_possible
= 0;
5921 /* If KEY is not defined in any of the keymaps,
5922 and cannot be part of a function key or translation,
5923 and is an upper case letter
5924 use the corresponding lower-case letter instead. */
5925 if (first_binding
== nmaps
&& ! function_key_possible
5926 && ! key_translation_possible
5928 && ((((XINT (key
) & 0x3ffff)
5929 < XSTRING (current_buffer
->downcase_table
)->size
)
5930 && UPPERCASEP (XINT (key
) & 0x3ffff))
5931 || (XINT (key
) & shift_modifier
)))
5933 original_uppercase
= key
;
5934 original_uppercase_position
= t
- 1;
5936 if (XINT (key
) & shift_modifier
)
5937 XSETINT (key
, XINT (key
) & ~shift_modifier
);
5939 XSETINT (key
, (DOWNCASE (XINT (key
) & 0x3ffff)
5940 | (XINT (key
) & ~0x3ffff)));
5942 keybuf
[t
- 1] = key
;
5944 goto replay_sequence
;
5946 /* If KEY is not defined in any of the keymaps,
5947 and cannot be part of a function key or translation,
5948 and is a shifted function key,
5949 use the corresponding unshifted function key instead. */
5950 if (first_binding
== nmaps
&& ! function_key_possible
5951 && ! key_translation_possible
5954 Lisp_Object breakdown
;
5957 original_uppercase
= key
;
5958 original_uppercase_position
= t
- 1;
5960 breakdown
= parse_modifiers (key
);
5961 modifiers
= XINT (XCONS (XCONS (breakdown
)->cdr
)->car
);
5962 if (modifiers
& shift_modifier
)
5964 modifiers
&= ~shift_modifier
;
5965 key
= apply_modifiers (modifiers
,
5966 XCONS (breakdown
)->car
);
5968 keybuf
[t
- 1] = key
;
5970 goto replay_sequence
;
5976 read_key_sequence_cmd
= (first_binding
< nmaps
5977 ? defs
[first_binding
]
5980 unread_switch_frame
= delayed_switch_frame
;
5981 unbind_to (count
, Qnil
);
5983 if (dont_downcase_last
&& t
- 1 == original_uppercase_position
)
5984 keybuf
[t
- 1] = original_uppercase
;
5986 /* Occasionally we fabricate events, perhaps by expanding something
5987 according to function-key-map, or by adding a prefix symbol to a
5988 mouse click in the scroll bar or modeline. In this cases, return
5989 the entire generated key sequence, even if we hit an unbound
5990 prefix or a definition before the end. This means that you will
5991 be able to push back the event properly, and also means that
5992 read-key-sequence will always return a logical unit.
5995 for (; t
< mock_input
; t
++)
5997 if (echo_keystrokes
)
5998 echo_char (keybuf
[t
]);
5999 add_command_key (keybuf
[t
]);
6005 #if 0 /* This doc string is too long for some compilers.
6006 This commented-out definition serves for DOC. */
6007 DEFUN ("read-key-sequence", Fread_key_sequence
, Sread_key_sequence
, 1, 4, 0,
6008 "Read a sequence of keystrokes and return as a string or vector.\n\
6009 The sequence is sufficient to specify a non-prefix command in the\n\
6010 current local and global maps.\n\
6012 First arg PROMPT is a prompt string. If nil, do not prompt specially.\n\
6013 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
6014 as a continuation of the previous key.\n\
6016 The third (optional) arg DONT-DOWNCASE-LAST, if non-nil, means do not\n\
6017 convert the last event to lower case. (Normally any upper case event\n\
6018 is converted to lower case if the original event is undefined and the lower\n\
6019 case equivalent is defined.) A non-nil value is appropriate for reading\n\
6020 a key sequence to be defined.\n\
6022 A C-g typed while in this function is treated like any other character,\n\
6023 and `quit-flag' is not set.\n\
6025 If the key sequence starts with a mouse click, then the sequence is read\n\
6026 using the keymaps of the buffer of the window clicked in, not the buffer\n\
6027 of the selected window as normal.\n\
6029 `read-key-sequence' drops unbound button-down events, since you normally\n\
6030 only care about the click or drag events which follow them. If a drag\n\
6031 or multi-click event is unbound, but the corresponding click event would\n\
6032 be bound, `read-key-sequence' turns the event into a click event at the\n\
6033 drag's starting position. This means that you don't have to distinguish\n\
6034 between click and drag, double, or triple events unless you want to.\n\
6036 `read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
6037 lines separating windows, and scroll bars with imaginary keys\n\
6038 `mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
6040 Optional fourth argument CAN-RETURN-SWITCH-FRAME non-nil means that this\n\
6041 function will process a switch-frame event if the user switches frames\n\
6042 before typing anything. If the user switches frames in the middle of a\n\
6043 key sequence, or at the start of the sequence but CAN-RETURN-SWITCH-FRAME\n\
6044 is nil, then the event will be put off until after the current key sequence.\n\
6046 `read-key-sequence' checks `function-key-map' for function key\n\
6047 sequences, where they wouldn't conflict with ordinary bindings. See\n\
6048 `function-key-map' for more details.")
6049 (prompt
, continue_echo
, dont_downcase_last
, can_return_switch_frame
)
6052 DEFUN ("read-key-sequence", Fread_key_sequence
, Sread_key_sequence
, 1, 4, 0,
6054 (prompt
, continue_echo
, dont_downcase_last
, can_return_switch_frame
)
6055 Lisp_Object prompt
, continue_echo
, dont_downcase_last
;
6056 Lisp_Object can_return_switch_frame
;
6058 Lisp_Object keybuf
[30];
6060 struct gcpro gcpro1
, gcpro2
;
6063 CHECK_STRING (prompt
, 0);
6066 bzero (keybuf
, sizeof keybuf
);
6068 gcpro1
.nvars
= (sizeof keybuf
/sizeof (keybuf
[0]));
6070 if (NILP (continue_echo
))
6071 this_command_key_count
= 0;
6073 i
= read_key_sequence (keybuf
, (sizeof keybuf
/sizeof (keybuf
[0])),
6074 prompt
, ! NILP (dont_downcase_last
),
6075 ! NILP (can_return_switch_frame
));
6083 return make_event_array (i
, keybuf
);
6086 DEFUN ("command-execute", Fcommand_execute
, Scommand_execute
, 1, 2, 0,
6087 "Execute CMD as an editor command.\n\
6088 CMD must be a symbol that satisfies the `commandp' predicate.\n\
6089 Optional second arg RECORD-FLAG non-nil\n\
6090 means unconditionally put this command in `command-history'.\n\
6091 Otherwise, that is done only if an arg is read using the minibuffer.")
6093 Lisp_Object cmd
, record
;
6095 register Lisp_Object final
;
6096 register Lisp_Object tem
;
6097 Lisp_Object prefixarg
;
6098 struct backtrace backtrace
;
6099 extern int debug_on_next_call
;
6101 prefixarg
= current_kboard
->Vprefix_arg
;
6102 current_kboard
->Vprefix_arg
= Qnil
;
6103 Vcurrent_prefix_arg
= prefixarg
;
6104 debug_on_next_call
= 0;
6108 tem
= Fget (cmd
, Qdisabled
);
6109 if (!NILP (tem
) && !NILP (Vrun_hooks
))
6110 return call1 (Vrun_hooks
, Qdisabled_command_hook
);
6115 final
= Findirect_function (cmd
);
6117 if (CONSP (final
) && (tem
= Fcar (final
), EQ (tem
, Qautoload
)))
6118 do_autoload (final
, cmd
);
6123 if (STRINGP (final
) || VECTORP (final
))
6125 /* If requested, place the macro in the command history. For
6126 other sorts of commands, call-interactively takes care of
6130 = Fcons (Fcons (Qexecute_kbd_macro
,
6131 Fcons (final
, Fcons (prefixarg
, Qnil
))),
6134 return Fexecute_kbd_macro (final
, prefixarg
);
6136 if (CONSP (final
) || SUBRP (final
) || COMPILEDP (final
))
6138 backtrace
.next
= backtrace_list
;
6139 backtrace_list
= &backtrace
;
6140 backtrace
.function
= &Qcall_interactively
;
6141 backtrace
.args
= &cmd
;
6142 backtrace
.nargs
= 1;
6143 backtrace
.evalargs
= 0;
6145 tem
= Fcall_interactively (cmd
, record
);
6147 backtrace_list
= backtrace
.next
;
6153 DEFUN ("execute-extended-command", Fexecute_extended_command
, Sexecute_extended_command
,
6155 "Read function name, then read its arguments and call it.")
6157 Lisp_Object prefixarg
;
6159 Lisp_Object function
;
6161 Lisp_Object saved_keys
;
6162 struct gcpro gcpro1
;
6164 saved_keys
= Fvector (this_command_key_count
,
6165 XVECTOR (this_command_keys
)->contents
);
6167 GCPRO1 (saved_keys
);
6169 if (EQ (prefixarg
, Qminus
))
6171 else if (CONSP (prefixarg
) && XINT (XCONS (prefixarg
)->car
) == 4)
6172 strcpy (buf
, "C-u ");
6173 else if (CONSP (prefixarg
) && INTEGERP (XCONS (prefixarg
)->car
))
6175 if (sizeof (int) == sizeof (EMACS_INT
))
6176 sprintf (buf
, "%d ", XINT (XCONS (prefixarg
)->car
));
6177 else if (sizeof (long) == sizeof (EMACS_INT
))
6178 sprintf (buf
, "%ld ", XINT (XCONS (prefixarg
)->car
));
6182 else if (INTEGERP (prefixarg
))
6184 if (sizeof (int) == sizeof (EMACS_INT
))
6185 sprintf (buf
, "%d ", XINT (prefixarg
));
6186 else if (sizeof (long) == sizeof (EMACS_INT
))
6187 sprintf (buf
, "%ld ", XINT (prefixarg
));
6192 /* This isn't strictly correct if execute-extended-command
6193 is bound to anything else. Perhaps it should use
6194 this_command_keys? */
6195 strcat (buf
, "M-x ");
6197 /* Prompt with buf, and then read a string, completing from and
6198 restricting to the set of all defined commands. Don't provide
6199 any initial input. Save the command read on the extended-command
6201 function
= Fcompleting_read (build_string (buf
),
6202 Vobarray
, Qcommandp
,
6203 Qt
, Qnil
, Qextended_command_history
);
6205 /* Set this_command_keys to the concatenation of saved_keys and
6206 function, followed by a RET. */
6208 struct Lisp_String
*str
;
6213 this_command_key_count
= 0;
6215 keys
= XVECTOR (saved_keys
)->contents
;
6216 for (i
= 0; i
< XVECTOR (saved_keys
)->size
; i
++)
6217 add_command_key (keys
[i
]);
6219 str
= XSTRING (function
);
6220 for (i
= 0; i
< str
->size
; i
++)
6222 XSETFASTINT (tem
, str
->data
[i
]);
6223 add_command_key (tem
);
6226 XSETFASTINT (tem
, '\015');
6227 add_command_key (tem
);
6232 function
= Fintern (function
, Qnil
);
6233 current_kboard
->Vprefix_arg
= prefixarg
;
6234 this_command
= function
;
6236 return Fcommand_execute (function
, Qt
);
6240 detect_input_pending ()
6243 get_input_pending (&input_pending
);
6245 return input_pending
;
6248 /* This is called in some cases before a possible quit.
6249 It cases the next call to detect_input_pending to recompute input_pending.
6250 So calling this function unnecessarily can't do any harm. */
6251 clear_input_pending ()
6256 DEFUN ("input-pending-p", Finput_pending_p
, Sinput_pending_p
, 0, 0, 0,
6257 "T if command input is currently available with no waiting.\n\
6258 Actually, the value is nil only if we can be sure that no input is available.")
6261 if (!NILP (Vunread_command_events
) || unread_command_char
!= -1)
6264 return detect_input_pending () ? Qt
: Qnil
;
6267 DEFUN ("recent-keys", Frecent_keys
, Srecent_keys
, 0, 0, 0,
6268 "Return vector of last 100 events, not counting those from keyboard macros.")
6271 Lisp_Object
*keys
= XVECTOR (recent_keys
)->contents
;
6274 if (total_keys
< NUM_RECENT_KEYS
)
6275 return Fvector (total_keys
, keys
);
6278 val
= Fvector (NUM_RECENT_KEYS
, keys
);
6279 bcopy (keys
+ recent_keys_index
,
6280 XVECTOR (val
)->contents
,
6281 (NUM_RECENT_KEYS
- recent_keys_index
) * sizeof (Lisp_Object
));
6283 XVECTOR (val
)->contents
+ NUM_RECENT_KEYS
- recent_keys_index
,
6284 recent_keys_index
* sizeof (Lisp_Object
));
6289 DEFUN ("this-command-keys", Fthis_command_keys
, Sthis_command_keys
, 0, 0, 0,
6290 "Return the key sequence that invoked this command.\n\
6291 The value is a string or a vector.")
6294 return make_event_array (this_command_key_count
,
6295 XVECTOR (this_command_keys
)->contents
);
6298 DEFUN ("reset-this-command-lengths", Freset_this_command_lengths
,
6299 Sreset_this_command_lengths
, 0, 0, 0,
6300 "Used for complicated reasons in `universal-argument-other-key'.\n\
6302 `universal-argument-other-key' rereads the event just typed.\n\
6303 It then gets translated through `function-key-map'.\n\
6304 The translated event gets included in the echo area and in\n\
6305 the value of `this-command-keys' in addition to the raw original event.\n\
6306 That is not right.\n\
6308 Calling this function directs the translated event to replace\n\
6309 the original event, so that only one version of the event actually\n\
6310 appears in the echo area and in the value of `this-command-keys.'.")
6313 before_command_restore_flag
= 1;
6314 before_command_key_count_1
= before_command_key_count
;
6315 before_command_echo_length_1
= before_command_echo_length
;
6318 DEFUN ("recursion-depth", Frecursion_depth
, Srecursion_depth
, 0, 0, 0,
6319 "Return the current depth in recursive edits.")
6323 XSETFASTINT (temp
, command_loop_level
+ minibuf_level
);
6327 DEFUN ("open-dribble-file", Fopen_dribble_file
, Sopen_dribble_file
, 1, 1,
6328 "FOpen dribble file: ",
6329 "Start writing all keyboard characters to a dribble file called FILE.\n\
6330 If FILE is nil, close any open dribble file.")
6344 file
= Fexpand_file_name (file
, Qnil
);
6345 dribble
= fopen (XSTRING (file
)->data
, "w");
6350 DEFUN ("discard-input", Fdiscard_input
, Sdiscard_input
, 0, 0, 0,
6351 "Discard the contents of the terminal input buffer.\n\
6352 Also cancel any kbd macro being defined.")
6355 current_kboard
->defining_kbd_macro
= Qnil
;
6356 update_mode_lines
++;
6358 Vunread_command_events
= Qnil
;
6359 unread_command_char
= -1;
6361 discard_tty_input ();
6363 /* Without the cast, GCC complains that this assignment loses the
6364 volatile qualifier of kbd_store_ptr. Is there anything wrong
6366 kbd_fetch_ptr
= (struct input_event
*) kbd_store_ptr
;
6367 Ffillarray (kbd_buffer_frame_or_window
, Qnil
);
6373 DEFUN ("suspend-emacs", Fsuspend_emacs
, Ssuspend_emacs
, 0, 1, "",
6374 "Stop Emacs and return to superior process. You can resume later.\n\
6375 If `cannot-suspend' is non-nil, or if the system doesn't support job\n\
6376 control, run a subshell instead.\n\n\
6377 If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
6378 to be read as terminal input by Emacs's parent, after suspension.\n\
6380 Before suspending, run the normal hook `suspend-hook'.\n\
6381 After resumption run the normal hook `suspend-resume-hook'.\n\
6383 Some operating systems cannot stop the Emacs process and resume it later.\n\
6384 On such systems, Emacs starts a subshell instead of suspending.")
6386 Lisp_Object stuffstring
;
6389 int count
= specpdl_ptr
- specpdl
;
6390 int old_height
, old_width
;
6392 struct gcpro gcpro1
, gcpro2
;
6393 extern init_sys_modes ();
6395 if (!NILP (stuffstring
))
6396 CHECK_STRING (stuffstring
, 0);
6398 /* Run the functions in suspend-hook. */
6399 if (!NILP (Vrun_hooks
))
6400 call1 (Vrun_hooks
, intern ("suspend-hook"));
6402 GCPRO1 (stuffstring
);
6403 get_frame_size (&old_width
, &old_height
);
6405 /* sys_suspend can get an error if it tries to fork a subshell
6406 and the system resources aren't available for that. */
6407 record_unwind_protect (init_sys_modes
, 0);
6408 stuff_buffered_input (stuffstring
);
6413 unbind_to (count
, Qnil
);
6415 /* Check if terminal/window size has changed.
6416 Note that this is not useful when we are running directly
6417 with a window system; but suspend should be disabled in that case. */
6418 get_frame_size (&width
, &height
);
6419 if (width
!= old_width
|| height
!= old_height
)
6420 change_frame_size (selected_frame
, height
, width
, 0, 0);
6422 /* Run suspend-resume-hook. */
6423 if (!NILP (Vrun_hooks
))
6424 call1 (Vrun_hooks
, intern ("suspend-resume-hook"));
6430 /* If STUFFSTRING is a string, stuff its contents as pending terminal input.
6431 Then in any case stuff anything Emacs has read ahead and not used. */
6433 stuff_buffered_input (stuffstring
)
6434 Lisp_Object stuffstring
;
6436 /* stuff_char works only in BSD, versions 4.2 and up. */
6439 register unsigned char *p
;
6441 if (STRINGP (stuffstring
))
6445 p
= XSTRING (stuffstring
)->data
;
6446 count
= XSTRING (stuffstring
)->size
;
6451 /* Anything we have read ahead, put back for the shell to read. */
6452 /* ?? What should this do when we have multiple keyboards??
6453 Should we ignore anything that was typed in at the "wrong" kboard? */
6454 for (; kbd_fetch_ptr
!= kbd_store_ptr
; kbd_fetch_ptr
++)
6456 if (kbd_fetch_ptr
== kbd_buffer
+ KBD_BUFFER_SIZE
)
6457 kbd_fetch_ptr
= kbd_buffer
;
6458 if (kbd_fetch_ptr
->kind
== ascii_keystroke
)
6459 stuff_char (kbd_fetch_ptr
->code
);
6460 kbd_fetch_ptr
->kind
= no_event
;
6461 (XVECTOR (kbd_buffer_frame_or_window
)->contents
[kbd_fetch_ptr
6467 #endif /* BSD and not BSD4_1 */
6470 set_waiting_for_input (time_to_clear
)
6471 EMACS_TIME
*time_to_clear
;
6473 input_available_clear_time
= time_to_clear
;
6475 /* Tell interrupt_signal to throw back to read_char, */
6476 waiting_for_input
= 1;
6478 /* If interrupt_signal was called before and buffered a C-g,
6479 make it run again now, to avoid timing error. */
6480 if (!NILP (Vquit_flag
))
6481 quit_throw_to_read_char ();
6484 clear_waiting_for_input ()
6486 /* Tell interrupt_signal not to throw back to read_char, */
6487 waiting_for_input
= 0;
6488 input_available_clear_time
= 0;
6491 /* This routine is called at interrupt level in response to C-G.
6492 If interrupt_input, this is the handler for SIGINT.
6493 Otherwise, it is called from kbd_buffer_store_event,
6494 in handling SIGIO or SIGTINT.
6496 If `waiting_for_input' is non zero, then unless `echoing' is nonzero,
6497 immediately throw back to read_char.
6499 Otherwise it sets the Lisp variable quit-flag not-nil.
6500 This causes eval to throw, when it gets a chance.
6501 If quit-flag is already non-nil, it stops the job right away. */
6504 interrupt_signal (signalnum
) /* If we don't have an argument, */
6505 int signalnum
; /* some compilers complain in signal calls. */
6508 /* Must preserve main program's value of errno. */
6509 int old_errno
= errno
;
6512 if (!read_socket_hook
&& NILP (Vwindow_system
))
6514 /* USG systems forget handlers when they are used;
6515 must reestablish each time */
6516 signal (SIGINT
, interrupt_signal
);
6517 signal (SIGQUIT
, interrupt_signal
);
6523 if (!NILP (Vquit_flag
) && FRAME_TERMCAP_P (selected_frame
))
6528 #ifdef SIGTSTP /* Support possible in later USG versions */
6530 * On systems which can suspend the current process and return to the original
6531 * shell, this command causes the user to end up back at the shell.
6532 * The "Auto-save" and "Abort" questions are not asked until
6533 * the user elects to return to emacs, at which point he can save the current
6534 * job and either dump core or continue.
6539 if (sys_suspend () == -1)
6541 printf ("Not running as a subprocess;\n");
6542 printf ("you can continue or abort.\n");
6545 /* Perhaps should really fork an inferior shell?
6546 But that would not provide any way to get back
6547 to the original shell, ever. */
6548 printf ("No support for stopping a process on this operating system;\n");
6549 printf ("you can continue or abort.\n");
6550 #endif /* not VMS */
6551 #endif /* not SIGTSTP */
6553 /* We must remain inside the screen area when the internal terminal
6554 is used. Note that [Enter] is not echoed by dos. */
6557 printf ("Auto-save? (y or n) ");
6559 if (((c
= getchar ()) & ~040) == 'Y')
6561 Fdo_auto_save (Qt
, Qnil
);
6563 printf ("\r\nAuto-save done");
6564 #else /* not MSDOS */
6565 printf ("Auto-save done\n");
6566 #endif /* not MSDOS */
6568 while (c
!= '\n') c
= getchar ();
6570 printf ("\r\nAbort? (y or n) ");
6571 #else /* not MSDOS */
6573 printf ("Abort (and enter debugger)? (y or n) ");
6575 printf ("Abort (and dump core)? (y or n) ");
6576 #endif /* not VMS */
6577 #endif /* not MSDOS */
6579 if (((c
= getchar ()) & ~040) == 'Y')
6581 while (c
!= '\n') c
= getchar ();
6583 printf ("\r\nContinuing...\r\n");
6584 #else /* not MSDOS */
6585 printf ("Continuing...\n");
6586 #endif /* not MSDOS */
6592 /* If executing a function that wants to be interrupted out of
6593 and the user has not deferred quitting by binding `inhibit-quit'
6594 then quit right away. */
6595 if (immediate_quit
&& NILP (Vinhibit_quit
))
6599 Fsignal (Qquit
, Qnil
);
6602 /* Else request quit when it's safe */
6606 if (waiting_for_input
&& !echoing
)
6607 quit_throw_to_read_char ();
6612 /* Handle a C-g by making read_char return C-g. */
6614 quit_throw_to_read_char ()
6616 quit_error_check ();
6618 /* Prevent another signal from doing this before we finish. */
6619 clear_waiting_for_input ();
6622 Vunread_command_events
= Qnil
;
6623 unread_command_char
= -1;
6625 #if 0 /* Currently, sit_for is called from read_char without turning
6626 off polling. And that can call set_waiting_for_input.
6627 It seems to be harmless. */
6628 #ifdef POLL_FOR_INPUT
6629 /* May be > 1 if in recursive minibuffer. */
6630 if (poll_suppress_count
== 0)
6635 if (FRAMEP (internal_last_event_frame
)
6636 && XFRAME (internal_last_event_frame
) != selected_frame
)
6637 do_switch_frame (make_lispy_switch_frame (internal_last_event_frame
),
6641 _longjmp (getcjmp
, 1);
6644 DEFUN ("set-input-mode", Fset_input_mode
, Sset_input_mode
, 3, 4, 0,
6645 "Set mode of reading keyboard input.\n\
6646 First arg INTERRUPT non-nil means use input interrupts;\n\
6647 nil means use CBREAK mode.\n\
6648 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
6649 (no effect except in CBREAK mode).\n\
6650 Third arg META t means accept 8-bit input (for a Meta key).\n\
6651 META nil means ignore the top bit, on the assumption it is parity.\n\
6652 Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
6653 Optional fourth arg QUIT if non-nil specifies character to use for quitting.\n\
6654 See also `current-input-mode'.")
6655 (interrupt
, flow
, meta
, quit
)
6656 Lisp_Object interrupt
, flow
, meta
, quit
;
6659 && (!INTEGERP (quit
) || XINT (quit
) < 0 || XINT (quit
) > 0400))
6660 error ("set-input-mode: QUIT must be an ASCII character");
6662 #ifdef POLL_FOR_INPUT
6668 /* Note SIGIO has been undef'd if FIONREAD is missing. */
6669 if (read_socket_hook
)
6671 /* When using X, don't give the user a real choice,
6672 because we haven't implemented the mechanisms to support it. */
6673 #ifdef NO_SOCK_SIGIO
6674 interrupt_input
= 0;
6675 #else /* not NO_SOCK_SIGIO */
6676 interrupt_input
= 1;
6677 #endif /* NO_SOCK_SIGIO */
6680 interrupt_input
= !NILP (interrupt
);
6681 #else /* not SIGIO */
6682 interrupt_input
= 0;
6683 #endif /* not SIGIO */
6685 /* Our VMS input only works by interrupts, as of now. */
6687 interrupt_input
= 1;
6690 flow_control
= !NILP (flow
);
6693 else if (EQ (meta
, Qt
))
6698 /* Don't let this value be out of range. */
6699 quit_char
= XINT (quit
) & (meta_key
? 0377 : 0177);
6703 #ifdef POLL_FOR_INPUT
6704 poll_suppress_count
= 1;
6710 DEFUN ("current-input-mode", Fcurrent_input_mode
, Scurrent_input_mode
, 0, 0, 0,
6711 "Return information about the way Emacs currently reads keyboard input.\n\
6712 The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
6713 INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
6714 nil, Emacs is using CBREAK mode.\n\
6715 FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
6716 terminal; this does not apply if Emacs uses interrupt-driven input.\n\
6717 META is t if accepting 8-bit input with 8th bit as Meta flag.\n\
6718 META nil means ignoring the top bit, on the assumption it is parity.\n\
6719 META is neither t nor nil if accepting 8-bit input and using\n\
6720 all 8 bits as the character code.\n\
6721 QUIT is the character Emacs currently uses to quit.\n\
6722 The elements of this list correspond to the arguments of\n\
6728 val
[0] = interrupt_input
? Qt
: Qnil
;
6729 val
[1] = flow_control
? Qt
: Qnil
;
6730 val
[2] = meta_key
== 2 ? make_number (0) : meta_key
== 1 ? Qt
: Qnil
;
6731 XSETFASTINT (val
[3], quit_char
);
6733 return Flist (sizeof (val
) / sizeof (val
[0]), val
);
6738 * Set up a new kboard object with reasonable initial values.
6744 kb
->Voverriding_terminal_local_map
= Qnil
;
6745 kb
->Vlast_command
= Qnil
;
6746 kb
->Vprefix_arg
= Qnil
;
6747 kb
->kbd_queue
= Qnil
;
6748 kb
->kbd_queue_has_data
= 0;
6749 kb
->immediate_echo
= 0;
6750 kb
->echoptr
= kb
->echobuf
;
6751 kb
->echo_after_prompt
= -1;
6752 kb
->kbd_macro_buffer
= 0;
6753 kb
->kbd_macro_bufsize
= 0;
6754 kb
->defining_kbd_macro
= Qnil
;
6755 kb
->Vlast_kbd_macro
= Qnil
;
6756 kb
->reference_count
= 0;
6757 kb
->Vsystem_key_alist
= Qnil
;
6758 kb
->system_key_syms
= Qnil
;
6759 kb
->Vdefault_minibuffer_frame
= Qnil
;
6763 * Destroy the contents of a kboard object, but not the object itself.
6764 * We use this just before deleteing it, or if we're going to initialize
6771 if (kb
->kbd_macro_buffer
)
6772 xfree (kb
->kbd_macro_buffer
);
6781 for (kbp
= &all_kboards
; *kbp
!= kb
; kbp
= &(*kbp
)->next_kboard
)
6784 *kbp
= kb
->next_kboard
;
6792 /* This is correct before outermost invocation of the editor loop */
6793 command_loop_level
= -1;
6795 quit_char
= Ctl ('g');
6796 Vunread_command_events
= Qnil
;
6797 unread_command_char
= -1;
6799 recent_keys_index
= 0;
6800 kbd_fetch_ptr
= kbd_buffer
;
6801 kbd_store_ptr
= kbd_buffer
;
6802 kbd_buffer_frame_or_window
6803 = Fmake_vector (make_number (KBD_BUFFER_SIZE
), Qnil
);
6805 do_mouse_tracking
= Qnil
;
6810 /* This means that command_loop_1 won't try to select anything the first
6812 internal_last_event_frame
= Qnil
;
6813 Vlast_event_frame
= internal_last_event_frame
;
6817 current_kboard
= initial_kboard
;
6819 wipe_kboard (current_kboard
);
6820 init_kboard (current_kboard
);
6823 Ffillarray (kbd_buffer_frame_or_window
, Qnil
);
6825 kbd_buffer_frame_or_window
6826 = Fmake_vector (make_number (KBD_BUFFER_SIZE
), Qnil
);
6827 if (!noninteractive
&& !read_socket_hook
&& NILP (Vwindow_system
))
6829 signal (SIGINT
, interrupt_signal
);
6830 #if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
6831 /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
6832 SIGQUIT and we can't tell which one it will give us. */
6833 signal (SIGQUIT
, interrupt_signal
);
6834 #endif /* HAVE_TERMIO */
6836 /* Note SIGIO has been undef'd if FIONREAD is missing. */
6838 if (!noninteractive
)
6839 signal (SIGIO
, input_available_signal
);
6842 /* Use interrupt input by default, if it works and noninterrupt input
6843 has deficiencies. */
6845 #ifdef INTERRUPT_INPUT
6846 interrupt_input
= 1;
6848 interrupt_input
= 0;
6851 /* Our VMS input only works by interrupts, as of now. */
6853 interrupt_input
= 1;
6859 if (keyboard_init_hook
)
6860 (*keyboard_init_hook
) ();
6862 #ifdef POLL_FOR_INPUT
6863 poll_suppress_count
= 1;
6868 /* This type's only use is in syms_of_keyboard, to initialize the
6869 event header symbols and put properties on them. */
6876 struct event_head head_table
[] = {
6877 &Qmouse_movement
, "mouse-movement", &Qmouse_movement
,
6878 &Qscroll_bar_movement
, "scroll-bar-movement", &Qmouse_movement
,
6879 &Qswitch_frame
, "switch-frame", &Qswitch_frame
,
6880 &Qdelete_frame
, "delete-frame", &Qdelete_frame
,
6881 &Qiconify_frame
, "iconify-frame", &Qiconify_frame
,
6882 &Qmake_frame_visible
, "make-frame-visible", &Qmake_frame_visible
,
6887 Qdisabled_command_hook
= intern ("disabled-command-hook");
6888 staticpro (&Qdisabled_command_hook
);
6890 Qself_insert_command
= intern ("self-insert-command");
6891 staticpro (&Qself_insert_command
);
6893 Qforward_char
= intern ("forward-char");
6894 staticpro (&Qforward_char
);
6896 Qbackward_char
= intern ("backward-char");
6897 staticpro (&Qbackward_char
);
6899 Qdisabled
= intern ("disabled");
6900 staticpro (&Qdisabled
);
6902 Qundefined
= intern ("undefined");
6903 staticpro (&Qundefined
);
6905 Qpre_command_hook
= intern ("pre-command-hook");
6906 staticpro (&Qpre_command_hook
);
6908 Qpost_command_hook
= intern ("post-command-hook");
6909 staticpro (&Qpost_command_hook
);
6911 Qdeferred_action_function
= intern ("deferred-action-function");
6912 staticpro (&Qdeferred_action_function
);
6914 Qcommand_hook_internal
= intern ("command-hook-internal");
6915 staticpro (&Qcommand_hook_internal
);
6917 Qfunction_key
= intern ("function-key");
6918 staticpro (&Qfunction_key
);
6919 Qmouse_click
= intern ("mouse-click");
6920 staticpro (&Qmouse_click
);
6922 Qmenu_enable
= intern ("menu-enable");
6923 staticpro (&Qmenu_enable
);
6925 Qmode_line
= intern ("mode-line");
6926 staticpro (&Qmode_line
);
6927 Qvertical_line
= intern ("vertical-line");
6928 staticpro (&Qvertical_line
);
6929 Qvertical_scroll_bar
= intern ("vertical-scroll-bar");
6930 staticpro (&Qvertical_scroll_bar
);
6931 Qmenu_bar
= intern ("menu-bar");
6932 staticpro (&Qmenu_bar
);
6934 Qabove_handle
= intern ("above-handle");
6935 staticpro (&Qabove_handle
);
6936 Qhandle
= intern ("handle");
6937 staticpro (&Qhandle
);
6938 Qbelow_handle
= intern ("below-handle");
6939 staticpro (&Qbelow_handle
);
6941 Qevent_kind
= intern ("event-kind");
6942 staticpro (&Qevent_kind
);
6943 Qevent_symbol_elements
= intern ("event-symbol-elements");
6944 staticpro (&Qevent_symbol_elements
);
6945 Qevent_symbol_element_mask
= intern ("event-symbol-element-mask");
6946 staticpro (&Qevent_symbol_element_mask
);
6947 Qmodifier_cache
= intern ("modifier-cache");
6948 staticpro (&Qmodifier_cache
);
6950 Qrecompute_lucid_menubar
= intern ("recompute-lucid-menubar");
6951 staticpro (&Qrecompute_lucid_menubar
);
6952 Qactivate_menubar_hook
= intern ("activate-menubar-hook");
6953 staticpro (&Qactivate_menubar_hook
);
6955 Qpolling_period
= intern ("polling-period");
6956 staticpro (&Qpolling_period
);
6959 struct event_head
*p
;
6961 for (p
= head_table
;
6962 p
< head_table
+ (sizeof (head_table
) / sizeof (head_table
[0]));
6965 *p
->var
= intern (p
->name
);
6967 Fput (*p
->var
, Qevent_kind
, *p
->kind
);
6968 Fput (*p
->var
, Qevent_symbol_elements
, Fcons (*p
->var
, Qnil
));
6972 button_down_location
= Fmake_vector (make_number (NUM_MOUSE_BUTTONS
), Qnil
);
6973 staticpro (&button_down_location
);
6977 int len
= sizeof (modifier_names
) / sizeof (modifier_names
[0]);
6979 modifier_symbols
= Fmake_vector (make_number (len
), Qnil
);
6980 for (i
= 0; i
< len
; i
++)
6981 if (modifier_names
[i
])
6982 XVECTOR (modifier_symbols
)->contents
[i
] = intern (modifier_names
[i
]);
6983 staticpro (&modifier_symbols
);
6986 recent_keys
= Fmake_vector (make_number (NUM_RECENT_KEYS
), Qnil
);
6987 staticpro (&recent_keys
);
6989 this_command_keys
= Fmake_vector (make_number (40), Qnil
);
6990 staticpro (&this_command_keys
);
6992 Qextended_command_history
= intern ("extended-command-history");
6993 Fset (Qextended_command_history
, Qnil
);
6994 staticpro (&Qextended_command_history
);
6996 kbd_buffer_frame_or_window
6997 = Fmake_vector (make_number (KBD_BUFFER_SIZE
), Qnil
);
6998 staticpro (&kbd_buffer_frame_or_window
);
7000 accent_key_syms
= Qnil
;
7001 staticpro (&accent_key_syms
);
7003 func_key_syms
= Qnil
;
7004 staticpro (&func_key_syms
);
7007 staticpro (&mouse_syms
);
7009 unread_switch_frame
= Qnil
;
7010 staticpro (&unread_switch_frame
);
7012 defsubr (&Sevent_convert_list
);
7013 defsubr (&Sread_key_sequence
);
7014 defsubr (&Srecursive_edit
);
7016 defsubr (&Strack_mouse
);
7018 defsubr (&Sinput_pending_p
);
7019 defsubr (&Scommand_execute
);
7020 defsubr (&Srecent_keys
);
7021 defsubr (&Sthis_command_keys
);
7022 defsubr (&Sreset_this_command_lengths
);
7023 defsubr (&Ssuspend_emacs
);
7024 defsubr (&Sabort_recursive_edit
);
7025 defsubr (&Sexit_recursive_edit
);
7026 defsubr (&Srecursion_depth
);
7027 defsubr (&Stop_level
);
7028 defsubr (&Sdiscard_input
);
7029 defsubr (&Sopen_dribble_file
);
7030 defsubr (&Sset_input_mode
);
7031 defsubr (&Scurrent_input_mode
);
7032 defsubr (&Sexecute_extended_command
);
7034 DEFVAR_LISP ("last-command-char", &last_command_char
,
7035 "Last input event that was part of a command.");
7037 DEFVAR_LISP_NOPRO ("last-command-event", &last_command_char
,
7038 "Last input event that was part of a command.");
7040 DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event
,
7041 "Last input event in a command, except for mouse menu events.\n\
7042 Mouse menus give back keys that don't look like mouse events;\n\
7043 this variable holds the actual mouse event that led to the menu,\n\
7044 so that you can determine whether the command was run by mouse or not.");
7046 DEFVAR_LISP ("last-input-char", &last_input_char
,
7047 "Last input event.");
7049 DEFVAR_LISP_NOPRO ("last-input-event", &last_input_char
,
7050 "Last input event.");
7052 DEFVAR_LISP ("unread-command-events", &Vunread_command_events
,
7053 "List of objects to be read as next command input events.");
7055 DEFVAR_INT ("unread-command-char", &unread_command_char
,
7056 "If not -1, an object to be read as next command input event.");
7058 DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char
,
7059 "Meta-prefix character code. Meta-foo as command input\n\
7060 turns into this character followed by foo.");
7061 XSETINT (meta_prefix_char
, 033);
7063 DEFVAR_KBOARD ("last-command", Vlast_command
,
7064 "The last command executed. Normally a symbol with a function definition,\n\
7065 but can be whatever was found in the keymap, or whatever the variable\n\
7066 `this-command' was set to by that command.\n\
7068 The value `mode-exit' is special; it means that the previous command\n\
7069 read an event that told it to exit, and it did so and unread that event.\n\
7070 In other words, the present command is the event that made the previous\n\
7073 The value `kill-region' is special; it means that the previous command\n\
7074 was a kill command.");
7076 DEFVAR_LISP ("this-command", &this_command
,
7077 "The command now being executed.\n\
7078 The command can set this variable; whatever is put here\n\
7079 will be in `last-command' during the following command.");
7080 this_command
= Qnil
;
7082 DEFVAR_INT ("auto-save-interval", &auto_save_interval
,
7083 "*Number of keyboard input characters between auto-saves.\n\
7084 Zero means disable autosaving due to number of characters typed.");
7085 auto_save_interval
= 300;
7087 DEFVAR_LISP ("auto-save-timeout", &Vauto_save_timeout
,
7088 "*Number of seconds idle time before auto-save.\n\
7089 Zero or nil means disable auto-saving due to idleness.\n\
7090 After auto-saving due to this many seconds of idle time,\n\
7091 Emacs also does a garbage collection if that seems to be warranted.");
7092 XSETFASTINT (Vauto_save_timeout
, 30);
7094 DEFVAR_INT ("echo-keystrokes", &echo_keystrokes
,
7095 "*Nonzero means echo unfinished commands after this many seconds of pause.");
7096 echo_keystrokes
= 1;
7098 DEFVAR_INT ("polling-period", &polling_period
,
7099 "*Interval between polling for input during Lisp execution.\n\
7100 The reason for polling is to make C-g work to stop a running program.\n\
7101 Polling is needed only when using X windows and SIGIO does not work.\n\
7102 Polling is automatically disabled in all other cases.");
7105 DEFVAR_LISP ("double-click-time", &Vdouble_click_time
,
7106 "*Maximum time between mouse clicks to make a double-click.\n\
7107 Measured in milliseconds. nil means disable double-click recognition;\n\
7108 t means double-clicks have no time limit and are detected\n\
7109 by position only.");
7110 Vdouble_click_time
= make_number (500);
7112 DEFVAR_BOOL ("inhibit-local-menu-bar-menus", &inhibit_local_menu_bar_menus
,
7113 "*Non-nil means inhibit local map menu bar menus.");
7114 inhibit_local_menu_bar_menus
= 0;
7116 DEFVAR_INT ("num-input-keys", &num_input_keys
,
7117 "Number of complete keys read from the keyboard so far.");
7120 DEFVAR_LISP ("last-event-frame", &Vlast_event_frame
,
7121 "The frame in which the most recently read event occurred.\n\
7122 If the last event came from a keyboard macro, this is set to `macro'.");
7123 Vlast_event_frame
= Qnil
;
7125 DEFVAR_LISP ("help-char", &Vhelp_char
,
7126 "Character to recognize as meaning Help.\n\
7127 When it is read, do `(eval help-form)', and display result if it's a string.\n\
7128 If the value of `help-form' is nil, this char can be read normally.");
7129 XSETINT (Vhelp_char
, Ctl ('H'));
7131 DEFVAR_LISP ("help-event-list", &Vhelp_event_list
,
7132 "List of input events to recognize as meaning Help.\n\
7133 These work just like the value of `help-char' (see that).");
7134 Vhelp_event_list
= Qnil
;
7136 DEFVAR_LISP ("help-form", &Vhelp_form
,
7137 "Form to execute when character `help-char' is read.\n\
7138 If the form returns a string, that string is displayed.\n\
7139 If `help-form' is nil, the help char is not recognized.");
7142 DEFVAR_LISP ("prefix-help-command", &Vprefix_help_command
,
7143 "Command to run when `help-char' character follows a prefix key.\n\
7144 This command is used only when there is no actual binding\n\
7145 for that character after that prefix key.");
7146 Vprefix_help_command
= Qnil
;
7148 DEFVAR_LISP ("top-level", &Vtop_level
,
7149 "Form to evaluate when Emacs starts up.\n\
7150 Useful to set before you dump a modified Emacs.");
7153 DEFVAR_LISP ("keyboard-translate-table", &Vkeyboard_translate_table
,
7154 "String used as translate table for keyboard input, or nil.\n\
7155 Each character is looked up in this string and the contents used instead.\n\
7156 If string is of length N, character codes N and up are untranslated.");
7157 Vkeyboard_translate_table
= Qnil
;
7159 DEFVAR_LISP ("key-translation-map", &Vkey_translation_map
,
7160 "Keymap of key translations that can override keymaps.\n\
7161 This keymap works like `function-key-map', but comes after that,\n\
7162 and applies even for keys that have ordinary bindings.");
7163 Vkey_translation_map
= Qnil
;
7165 DEFVAR_BOOL ("cannot-suspend", &cannot_suspend
,
7166 "Non-nil means to always spawn a subshell instead of suspending,\n\
7167 even if the operating system has support for stopping a process.");
7170 DEFVAR_BOOL ("menu-prompting", &menu_prompting
,
7171 "Non-nil means prompt with menus when appropriate.\n\
7172 This is done when reading from a keymap that has a prompt string,\n\
7173 for elements that have prompt strings.\n\
7174 The menu is displayed on the screen\n\
7175 if X menus were enabled at configuration\n\
7176 time and the previous event was a mouse click prefix key.\n\
7177 Otherwise, menu prompting uses the echo area.");
7180 DEFVAR_LISP ("menu-prompt-more-char", &menu_prompt_more_char
,
7181 "Character to see next line of menu prompt.\n\
7182 Type this character while in a menu prompt to rotate around the lines of it.");
7183 XSETINT (menu_prompt_more_char
, ' ');
7185 DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers
,
7186 "A mask of additional modifier keys to use with every keyboard character.\n\
7187 Emacs applies the modifiers of the character stored here to each keyboard\n\
7188 character it reads. For example, after evaluating the expression\n\
7189 (setq extra-keyboard-modifiers ?\\C-x)\n\
7190 all input characters will have the control modifier applied to them.\n\
7192 Note that the character ?\\C-@, equivalent to the integer zero, does\n\
7193 not count as a control character; rather, it counts as a character\n\
7194 with no modifiers; thus, setting `extra-keyboard-modifiers' to zero\n\
7195 cancels any modification.");
7196 extra_keyboard_modifiers
= 0;
7198 DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark
,
7199 "If an editing command sets this to t, deactivate the mark afterward.\n\
7200 The command loop sets this to nil before each command,\n\
7201 and tests the value when the command returns.\n\
7202 Buffer modification stores t in this variable.");
7203 Vdeactivate_mark
= Qnil
;
7205 DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal
,
7206 "Temporary storage of pre-command-hook or post-command-hook.");
7207 Vcommand_hook_internal
= Qnil
;
7209 DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook
,
7210 "Normal hook run before each command is executed.\n\
7211 While the hook is run, its value is temporarily set to nil\n\
7212 to avoid an unbreakable infinite loop if a hook function gets an error.\n\
7213 As a result, a hook function cannot straightforwardly alter the value of\n\
7214 `pre-command-hook'. See the Emacs Lisp manual for a way of\n\
7215 implementing hook functions that alter the set of hook functions.");
7216 Vpre_command_hook
= Qnil
;
7218 DEFVAR_LISP ("post-command-hook", &Vpost_command_hook
,
7219 "Normal hook run after each command is executed.\n\
7220 While the hook is run, its value is temporarily set to nil\n\
7221 to avoid an unbreakable infinite loop if a hook function gets an error.\n\
7222 As a result, a hook function cannot straightforwardly alter the value of\n\
7223 `post-command-hook'. See the Emacs Lisp manual for a way of\n\
7224 implementing hook functions that alter the set of hook functions.");
7225 Vpost_command_hook
= Qnil
;
7227 DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag
,
7228 "t means menu bar, specified Lucid style, needs to be recomputed.");
7229 Vlucid_menu_bar_dirty_flag
= Qnil
;
7231 DEFVAR_LISP ("menu-bar-final-items", &Vmenu_bar_final_items
,
7232 "List of menu bar items to move to the end of the menu bar.\n\
7233 The elements of the list are event types that may have menu bar bindings.");
7234 Vmenu_bar_final_items
= Qnil
;
7236 DEFVAR_KBOARD ("overriding-terminal-local-map",
7237 Voverriding_terminal_local_map
,
7238 "Keymap that overrides all other local keymaps.\n\
7239 If this variable is non-nil, it is used as a keymap instead of the\n\
7240 buffer's local map, and the minor mode keymaps and text property keymaps.");
7242 DEFVAR_LISP ("overriding-local-map", &Voverriding_local_map
,
7243 "Keymap that overrides all other local keymaps.\n\
7244 If this variable is non-nil, it is used as a keymap instead of the\n\
7245 buffer's local map, and the minor mode keymaps and text property keymaps.");
7246 Voverriding_local_map
= Qnil
;
7248 DEFVAR_LISP ("overriding-local-map-menu-flag", &Voverriding_local_map_menu_flag
,
7249 "Non-nil means `overriding-local-map' applies to the menu bar.\n\
7250 Otherwise, the menu bar continues to reflect the buffer's local map\n\
7251 and the minor mode maps regardless of `overriding-local-map'.");
7252 Voverriding_local_map_menu_flag
= Qnil
;
7254 DEFVAR_LISP ("special-event-map", &Vspecial_event_map
,
7255 "Keymap defining bindings for special events to execute at low level.");
7256 Vspecial_event_map
= Fcons (intern ("keymap"), Qnil
);
7258 DEFVAR_LISP ("track-mouse", &do_mouse_tracking
,
7259 "*Non-nil means generate motion events for mouse motion.");
7261 DEFVAR_KBOARD ("system-key-alist", Vsystem_key_alist
,
7262 "Alist of system-specific X windows key symbols.\n\
7263 Each element should have the form (N . SYMBOL) where N is the\n\
7264 numeric keysym code (sans the \"system-specific\" bit 1<<28)\n\
7265 and SYMBOL is its name.");
7267 DEFVAR_LISP ("deferred-action-list", &Vdeferred_action_list
,
7268 "List of deferred actions to be performed at a later time.\n\
7269 The precise format isn't relevant here; we just check whether it is nil.");
7270 Vdeferred_action_list
= Qnil
;
7272 DEFVAR_LISP ("deferred-action-function", &Vdeferred_action_function
,
7273 "Function to call to handle deferred actions, after each command.\n\
7274 This function is called with no arguments after each command\n\
7275 whenever `deferred-action-list' is non-nil.");
7276 Vdeferred_action_function
= Qnil
;
7281 initial_define_key (global_map
, Ctl ('Z'), "suspend-emacs");
7282 initial_define_key (control_x_map
, Ctl ('Z'), "suspend-emacs");
7283 initial_define_key (meta_map
, Ctl ('C'), "exit-recursive-edit");
7284 initial_define_key (global_map
, Ctl (']'), "abort-recursive-edit");
7285 initial_define_key (meta_map
, 'x', "execute-extended-command");
7287 initial_define_lispy_key (Vspecial_event_map
, "delete-frame",
7288 "handle-delete-frame");
7289 initial_define_lispy_key (Vspecial_event_map
, "iconify-frame",
7291 initial_define_lispy_key (Vspecial_event_map
, "make-frame-visible",