]> code.delx.au - gnu-emacs/blobdiff - src/keyboard.c
* keymap.c (Fwhere_is_internal): If FIRSTONLY is non-nil, avoid
[gnu-emacs] / src / keyboard.c
index 3c139c68644ad7287af8e774ddba86da70073ba0..3e8a522a0737a895c9dbe6ad2853edee5b7bc184 100644 (file)
@@ -1,5 +1,5 @@
 /* Keyboard and mouse input; editor command loop.
-   Copyright (C) 1985, 1986, 1987, 1988, 1989, 1992, 1993 Free Software Foundation, Inc.
+   Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -35,6 +35,8 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include "disptab.h"
 #include "dispextern.h"
 #include "keyboard.h"
+#include "intervals.h"
+#include "blockinput.h"
 #include <setjmp.h>
 #include <errno.h>
 
@@ -48,6 +50,16 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 extern int errno;
 
+/* Variables for blockinput.h: */
+
+/* Non-zero if interrupt input is blocked right now.  */
+int interrupt_input_blocked;
+
+/* Nonzero means an input interrupt has arrived
+   during the current critical section.  */
+int interrupt_input_pending;
+
+
 #ifdef HAVE_X_WINDOWS
 extern Lisp_Object Vmouse_grabbed;
 
@@ -152,18 +164,24 @@ Lisp_Object last_nonmenu_event;
 /* Last input character read for any purpose.  */
 Lisp_Object last_input_char;
 
-/* If not Qnil, an object to be read as the next command input.  */
-Lisp_Object unread_command_event;
+/* If not Qnil, a list of objects to be read as subsequent command input.  */
+Lisp_Object unread_command_events;
+
+/* If not -1, an event to be read as subsequent command input.  */
+int unread_command_char;
 
 /* If not Qnil, this is a switch-frame event which we decided to put
    off until the end of a key sequence.  This should be read as the
-   next command input, after any unread_command_event.
+   next command input, after any unread_command_events.
 
    read_key_sequence uses this to delay switch-frame events until the
    end of the key sequence; Fread_char uses it to put off switch-frame
    events until a non-ASCII event is acceptable as input.  */
 Lisp_Object unread_switch_frame;
 
+/* A mask of extra modifier bits to put into every keyboard char.  */
+int extra_keyboard_modifiers;
+
 /* Char to use as prefix when a meta character is typed in.
    This is bound on entry to minibuffer in case ESC is changed there.  */
 
@@ -202,7 +220,15 @@ Lisp_Object this_command;
 
 #ifdef MULTI_FRAME
 /* The frame in which the last input event occurred, or Qmacro if the
-   last event came from a macro.  */
+   last event came from a macro.  We use this to determine when to
+   generate switch-frame events.  This may be cleared by functions
+   like Fselect_frame, to make sure that a switch-frame event is
+   generated by the next character.  */
+Lisp_Object internal_last_event_frame;
+
+/* A user-visible version of the above, intended to allow users to
+   figure out where the last event came from, if the event doesn't
+   carry that information itself (i.e. if it was a character).  */
 Lisp_Object Vlast_event_frame;
 #endif
 
@@ -227,13 +253,27 @@ Lisp_Object Vkeyboard_translate_table;
 /* Keymap mapping ASCII function key sequences onto their preferred forms.  */
 extern Lisp_Object Vfunction_key_map;
 
+/* Non-nil means deactivate the mark at end of this command.  */
+Lisp_Object Vdeactivate_mark;
+
+/* Menu bar specified in Lucid Emacs fashion.  */
+
+Lisp_Object Vlucid_menu_bar_dirty_flag;
+Lisp_Object Qrecompute_lucid_menubar, Qactivate_menubar_hook;
+
+/* Hooks to run before and after each command.  */
+Lisp_Object Qpre_command_hook, Qpost_command_hook;
+Lisp_Object Vpre_command_hook, Vpost_command_hook;
+
 /* File in which we write all commands we read.  */
 FILE *dribble;
 
 /* Nonzero if input is available.  */
 int input_pending;
 
-/* Nonzero if should obey 0200 bit in input chars as "Meta".  */
+/* 1 if should obey 0200 bit in input chars as "Meta", 2 if should
+   keep 0200 bit in input chars.  0 to ignore the 0200 bit.  */
+
 int meta_key;
 
 extern char *pending_malloc_warning;
@@ -245,8 +285,8 @@ static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
 
    The interrupt-level event handlers will never enqueue an event on a
    frame which is not in Vframe_list, and once an event is dequeued,
-   Vlast_event_frame or the event itself points to the frame.  So
-   that's all fine.
+   internal_last_event_frame or the event itself points to the frame.
+   So that's all fine.
 
    But while the event is sitting in the queue, it's completely
    unprotected.  Suppose the user types one command which will run for
@@ -257,7 +297,7 @@ static struct input_event kbd_buffer[KBD_BUFFER_SIZE];
    have an event referring to a freed frame, which will crash Emacs
    when it is dequeued.
 
-   Similar things happen when an event on a scrollbar is enqueued; the
+   Similar things happen when an event on a scroll bar is enqueued; the
    window may be deleted while the event is in the queue.
 
    So, we use this vector to protect the frame_or_window field in the
@@ -292,10 +332,10 @@ static struct input_event *kbd_store_ptr;
    dequeuing functions?  Such a flag could be screwed up by interrupts
    at inopportune times.  */
 
-/* If this flag is non-zero, we will check mouse_moved to see when the
+/* If this flag is non-zero, we check mouse_moved to see when the
    mouse moves, and motion events will appear in the input stream.  If
-   it is zero, mouse motion will be ignored.  */
-int do_mouse_tracking;
+   it is zero, mouse motion is ignored.  */
+static int do_mouse_tracking;
 
 /* The window system handling code should set this if the mouse has
    moved since the last call to the mouse_position_hook.  Calling that
@@ -315,8 +355,7 @@ int mouse_moved;
 
 /* Symbols to head events.  */
 Lisp_Object Qmouse_movement;
-Lisp_Object Qscrollbar_movement;
-
+Lisp_Object Qscroll_bar_movement;
 Lisp_Object Qswitch_frame;
 
 /* Symbols to denote kinds of events.  */
@@ -328,6 +367,8 @@ Lisp_Object Qmouse_click;
 Lisp_Object Qevent_kind;
 Lisp_Object Qevent_symbol_elements;
 
+Lisp_Object Qmenu_enable;
+
 /* An event header symbol HEAD may have a property named
    Qevent_symbol_element_mask, which is of the form (BASE MODIFIERS);
    BASE is the base, unmodified version of HEAD, and MODIFIERS is the
@@ -341,10 +382,13 @@ Lisp_Object Qevent_symbol_element_mask;
    apply_modifiers.  */
 Lisp_Object Qmodifier_cache;
 
-/* Symbols to use for non-text mouse positions.  */
+/* Symbols to use for parts of windows.  */
 Lisp_Object Qmode_line;
 Lisp_Object Qvertical_line;
-Lisp_Object Qvertical_scrollbar;
+Lisp_Object Qvertical_scroll_bar;
+Lisp_Object Qmenu_bar;
+
+extern Lisp_Object Qmenu_enable;
 
 Lisp_Object recursive_edit_unwind (), command_loop ();
 Lisp_Object Fthis_command_keys ();
@@ -449,7 +493,7 @@ echo_char (c)
          if (ptr - echobuf > sizeof echobuf - 6)
            return;
 
-         ptr = push_key_description (c, ptr);
+         ptr = push_key_description (XINT (c), ptr);
        }
       else if (XTYPE (c) == Lisp_Symbol)
        {
@@ -480,6 +524,9 @@ echo_dash ()
 {
   if (!immediate_echo && echoptr == echobuf)
     return;
+  /* Do nothing if not echoing at all.  */
+  if (echoptr == 0)
+    return;
 
   /* Put a dash at the end of the buffer temporarily,
      but make it go away when the next character is added.  */
@@ -578,8 +625,7 @@ recursive_edit_1 ()
   if (EQ (val, Qt))
     Fsignal (Qquit, Qnil);
 
-  unbind_to (count);
-  return Qnil;
+  return unbind_to (count, Qnil);
 }
 
 /* When an auto-save happens, record the "time", and don't do again soon.  */
@@ -811,15 +857,18 @@ static int read_key_sequence ();
 Lisp_Object
 command_loop_1 ()
 {
-  Lisp_Object cmd;
+  Lisp_Object cmd, tem;
   int lose;
   int nonundocount;
   Lisp_Object keybuf[30];
   int i;
   int no_redisplay;
   int no_direct;
+  int prev_modiff;
+  struct buffer *prev_buffer;
 
   Vprefix_arg = Qnil;
+  Vdeactivate_mark = Qnil;
   waiting_for_input = 0;
   cancel_echoing ();
 
@@ -850,6 +899,8 @@ command_loop_1 ()
 
       no_direct = 0;
 
+      Vdeactivate_mark = Qnil;
+
       /* If minibuffer on and echo area in use,
         wait 2 sec and redraw minibufer.  */
 
@@ -860,14 +911,14 @@ command_loop_1 ()
          int count = specpdl_ptr - specpdl;
          specbind (Qinhibit_quit, Qt);
          Fsit_for (make_number (2), Qnil, Qnil);
-         unbind_to (count);
+         unbind_to (count, Qnil);
 
          echo_area_glyphs = 0;
          no_direct = 1;
          if (!NILP (Vquit_flag))
            {
              Vquit_flag = Qnil;
-             unread_command_event = make_number (quit_char);
+             unread_command_events = Fcons (make_number (quit_char), Qnil);
            }
        }
 
@@ -882,11 +933,42 @@ command_loop_1 ()
         switch-frame events will take care of this, but if some lisp
         code swallows a switch-frame event, we'll fix things up here.
         Is this a good idea?  */
-      if (XTYPE (Vlast_event_frame) == Lisp_Frame
-         && XFRAME (Vlast_event_frame) != selected_frame)
-       Fselect_frame (Vlast_event_frame, Qnil);
+      if (XTYPE (internal_last_event_frame) == Lisp_Frame
+         && XFRAME (internal_last_event_frame) != selected_frame)
+       Fselect_frame (internal_last_event_frame, Qnil);
 #endif
 #endif
+      /* If it has changed current-menubar from previous value,
+        really recompute the menubar from the value.  */
+      if (! NILP (Vlucid_menu_bar_dirty_flag))
+       call0 (Qrecompute_lucid_menubar);
+
+#ifdef MULTI_FRAME
+      for (tem = Vframe_list; CONSP (tem); tem = XCONS (tem)->cdr)
+       {
+         struct frame *f = XFRAME (XCONS (tem)->car);
+         struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
+
+         /* If the user has switched buffers or windows, we need to
+            recompute to reflect the new bindings.  But we'll
+            recompute when update_mode_lines is set too; that means
+            that people can use force-mode-line-update to request
+            that the menu bar be recomputed.  The adverse effect on
+            the rest of the redisplay algorithm is about the same as
+            windows_or_buffers_changed anyway.  */
+         if (windows_or_buffers_changed
+             || update_mode_lines
+             || (XFASTINT (w->last_modified) < MODIFF
+                 && (XFASTINT (w->last_modified)
+                     <= XBUFFER (w->buffer)->save_modified)))
+           {
+             struct buffer *prev = current_buffer;
+             current_buffer = XBUFFER (w->buffer);
+             FRAME_MENU_BAR_ITEMS (f) = menu_bar_items ();
+             current_buffer = prev;
+           }
+       }
+#endif /* MULTI_FRAME */
 
       /* Read next key sequence; i gets its length.  */
       i = read_key_sequence (keybuf, (sizeof keybuf / sizeof (keybuf[0])), 0);
@@ -901,6 +983,11 @@ command_loop_1 ()
 
       last_command_char = keybuf[i - 1];
 
+      /* If the previous command tried to force a specific window-start,
+        forget about that, in case this command moves point far away
+        from that position.  */
+      XWINDOW (selected_window)->force_start = Qnil;
+
       cmd = read_key_sequence_cmd;
       if (!NILP (Vexecuting_macro))
        {
@@ -916,8 +1003,15 @@ command_loop_1 ()
         cases identified below that set no_redisplay to 1.  */
       no_redisplay = 0;
 
+      prev_buffer = current_buffer;
+      prev_modiff = MODIFF;
+
       /* Execute the command.  */
 
+      this_command = cmd;
+      if (!NILP (Vpre_command_hook))
+       call1 (Vrun_hooks, Qpre_command_hook);
+
       if (NILP (cmd))
        {
          /* nil means key is undefined.  */
@@ -925,10 +1019,10 @@ command_loop_1 ()
          defining_kbd_macro = 0;
          update_mode_lines = 1;
          Vprefix_arg = Qnil;
+
        }
       else
        {
-         this_command = cmd;
          if (NILP (Vprefix_arg) && ! no_direct)
            {
              /* Recognize some common commands in common situations and
@@ -939,10 +1033,10 @@ command_loop_1 ()
                    = window_display_table (XWINDOW (selected_window));
                  lose = FETCH_CHAR (point);
                  SET_PT (point + 1);
-                 if (((dp == 0 && lose >= 040 && lose < 0177)
-                      ||
-                      (dp && (XTYPE (dp->contents[lose]) != Lisp_String
-                              || XSTRING (dp->contents[lose])->size == sizeof (GLYPH))))
+                 if ((dp
+                      ? (XTYPE (DISP_CHAR_VECTOR (dp, lose)) != Lisp_Vector
+                         && XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1)
+                      : (lose >= 0x20 && lose < 0x7f))
                      && (XFASTINT (XWINDOW (selected_window)->last_modified)
                          >= MODIFF)
                      && (XFASTINT (XWINDOW (selected_window)->last_point)
@@ -960,10 +1054,10 @@ command_loop_1 ()
                    = window_display_table (XWINDOW (selected_window));
                  SET_PT (point - 1);
                  lose = FETCH_CHAR (point);
-                 if (((dp == 0 && lose >= 040 && lose < 0177)
-                      ||
-                      (dp && (XTYPE (dp->contents[lose]) != Lisp_String
-                              || XSTRING (dp->contents[lose])->size == sizeof (GLYPH))))
+                 if ((dp
+                      ? (XTYPE (DISP_CHAR_VECTOR (dp, lose)) != Lisp_Vector
+                         && XVECTOR (DISP_CHAR_VECTOR (dp, lose))->size == 1)
+                      : (lose >= 0x20 && lose < 0x7f))
                      && (XFASTINT (XWINDOW (selected_window)->last_modified)
                          >= MODIFF)
                      && (XFASTINT (XWINDOW (selected_window)->last_point)
@@ -1000,7 +1094,7 @@ command_loop_1 ()
                    || !EQ (current_buffer->selective_display, Qnil)
                    || detect_input_pending ()
                    || !NILP (Vexecuting_macro);
-                 if (internal_self_insert (c, 0))
+                 if (internal_self_insert (XINT (c), 0))
                    {
                      lose = 1;
                      nonundocount = 0;
@@ -1010,13 +1104,24 @@ command_loop_1 ()
                    {
                      struct Lisp_Vector *dp
                        = window_display_table (XWINDOW (selected_window));
+                     int lose = XINT (c);
 
-                     if (dp == 0 || XTYPE (dp->contents[c]) != Lisp_String)
-                       no_redisplay = direct_output_for_insert (c);
-                     else if (XSTRING (dp->contents[c])->size
-                              == sizeof (GLYPH))
-                       no_redisplay =
-                         direct_output_for_insert (*(GLYPH *)XSTRING (dp->contents[c])->data);
+                     if (dp)
+                       {
+                         Lisp_Object obj = DISP_CHAR_VECTOR (dp, lose);
+
+                         if (XTYPE (obj) == Lisp_Vector
+                             && XVECTOR (obj)->size == 1
+                             && (XTYPE (obj = XVECTOR (obj)->contents[0])
+                                 == Lisp_Int))
+                           no_redisplay =
+                             direct_output_for_insert (XINT (obj));
+                       }
+                     else
+                       {
+                         if (lose >= 0x20 && lose <= 0x7e)
+                           no_redisplay = direct_output_for_insert (lose);
+                       }
                    }
                  goto directly_done;
                }
@@ -1032,6 +1137,9 @@ command_loop_1 ()
        }
     directly_done: ;
 
+      if (!NILP (Vpost_command_hook))
+       call1 (Vrun_hooks, Qpost_command_hook);
+
       /* If there is a prefix argument,
         1) We don't want last_command to be ``universal-argument''
         (that would be dumb), so don't set last_command,
@@ -1047,6 +1155,17 @@ command_loop_1 ()
          cancel_echoing ();
          this_command_key_count = 0;
        }
+
+      if (!NILP (current_buffer->mark_active))
+       {
+         if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
+           {
+             current_buffer->mark_active = Qnil;
+             call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
+           }
+         else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
+           call1 (Vrun_hooks, intern ("activate-mark-hook"));
+       }
     }
 }
 \f
@@ -1065,12 +1184,9 @@ int polling_for_input;
 SIGTYPE
 input_poll_signal ()
 {
-#ifdef HAVE_X_WINDOWS
-  extern int x_input_blocked;
-  if (x_input_blocked == 0)
-#endif
-    if (!waiting_for_input)
-      read_avail_input (0);
+  if (interrupt_input_blocked == 0
+      && !waiting_for_input)
+    read_avail_input (0);
   signal (SIGALRM, input_poll_signal);
   alarm (polling_period);
 }
@@ -1145,10 +1261,21 @@ read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
   int count;
   jmp_buf save_jump;
 
-  if (!NILP (unread_command_event))
+  if (CONSP (unread_command_events))
     {
-      c = unread_command_event;
-      unread_command_event = Qnil;
+      c = XCONS (unread_command_events)->car;
+      unread_command_events = XCONS (unread_command_events)->cdr;
+
+      if (this_command_key_count == 0)
+       goto reread_first;
+      else
+       goto reread;
+    }
+
+  if (unread_command_char != -1)
+    {
+      XSET (c, Lisp_Int, unread_command_char);
+      unread_command_char = -1;
 
       if (this_command_key_count == 0)
        goto reread_first;
@@ -1164,20 +1291,29 @@ read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
         remain unchanged.
 
          Since this event came from a macro, it would be misleading to
-        leave Vlast_event_frame set to whereever the last real event
-        came from.  Normally, command_loop_1 selects
-        Vlast_event_frame after each command is read, but events read
-        from a macro should never cause a new frame to be selected.  */
-      Vlast_event_frame = Qmacro;
+        leave internal_last_event_frame set to whereever the last
+        real event came from.  Normally, a switch-frame event selects
+        internal_last_event_frame after each command is read, but
+        events read from a macro should never cause a new frame to be
+        selected. */
+      Vlast_event_frame = internal_last_event_frame = Qmacro;
 #endif
 
-      if (executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
+      /* Exit the macro if we are at the end.
+        Also, some things replace the macro with t
+        to force an early exit.  */
+      if (EQ (Vexecuting_macro, Qt)
+         || executing_macro_index >= XFASTINT (Flength (Vexecuting_macro)))
        {
          XSET (c, Lisp_Int, -1);
          return c;
        }
       
       c = Faref (Vexecuting_macro, make_number (executing_macro_index));
+      if (XTYPE (Vexecuting_macro) == Lisp_String
+         && (XINT (c) & 0x80))
+       XFASTINT (c) = CHAR_META | (XINT (c) & ~0x80);
+
       executing_macro_index++;
 
       goto from_macro;
@@ -1205,8 +1341,13 @@ read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
     {
       XSET (c, Lisp_Int, quit_char);
 #ifdef MULTI_FRAME
-      XSET (Vlast_event_frame, Lisp_Frame, selected_frame);
+      XSET (internal_last_event_frame, Lisp_Frame, selected_frame);
+      Vlast_event_frame = internal_last_event_frame;
 #endif
+      /* If we report the quit char as an event,
+        don't do so more than once.  */
+      if (!NILP (Vinhibit_quit))
+       Vquit_flag = Qnil;
 
       goto non_reread;
     }
@@ -1229,7 +1370,7 @@ read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
       /* After a mouse event, start echoing right away.
         This is because we are probably about to display a menu,
         and we don't want to delay before doing so.  */
-      if (XTYPE (prev_event) == Lisp_Cons)
+      if (EVENT_HAS_PARAMETERS (prev_event))
        echo ();
       else
        {
@@ -1313,6 +1454,39 @@ read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
   if (noninteractive && XTYPE (c) == Lisp_Int && XINT (c) < 0)
     Fkill_emacs (make_number (1));
 
+  if (XTYPE (c) == Lisp_Int)
+    {
+      /* Add in any extra modifiers, where appropriate.  */
+      if ((extra_keyboard_modifiers & CHAR_CTL)
+         || ((extra_keyboard_modifiers & 0177) < ' '
+             && (extra_keyboard_modifiers & 0177) != 0))
+       {
+         /* If it's already a control character, don't mess with it.  */
+         if ((c & 0177) == 0)
+           ;
+
+         /* Making ? a control character should result in DEL.  */
+         else if ((c & 0177) == '?')
+           c |= 0177;
+
+         /* ASCII control chars are made from letters (both cases),
+            as well as the non-letters within 0100...0137.  */
+         else if ((c & 0137) >= 0101 && (c & 0137) <= 0132)
+           c = (c & (037 | ~0177));
+         else if ((c & 0177) >= 0100 && (c & 0177) <= 0137)
+           c = (c & (037 | ~0177));
+
+         /* Anything else must get its high control bit set.  */
+         else
+           c = c | ctrl_modifier;
+       }
+
+      /* Transfer any other modifier bits directly from
+        extra_keyboard_modifiers to c.  Ignore the actual character code
+        in the low 16 bits of extra_keyboard_modifiers.  */
+      c |= (extra_keyboard_modifiers & ~0xff7f & ~CHAR_CTL);
+    }
+
  non_reread:
 
   restore_getcjmp (save_jump);
@@ -1325,12 +1499,9 @@ read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
   if (XTYPE (c) == Lisp_Int)
     {
       /* If kbd_buffer_get_event gave us an EOF, return that.  */
-      if (XINT (c) < 0)
+      if (XINT (c) == -1)
        return c;
 
-      /* Strip the high bits, and maybe the meta bit too.  */
-      XSETINT (c, XINT (c) & (meta_key ? 0377 : 0177));
-
       if (XTYPE (Vkeyboard_translate_table) == Lisp_String
          && XSTRING (Vkeyboard_translate_table)->size > XFASTINT (c))
        XSETINT (c, XSTRING (Vkeyboard_translate_table)->data[XFASTINT (c)]);
@@ -1519,14 +1690,13 @@ kbd_buffer_store_event (event)
     {
       register int c = XFASTINT (event->code) & 0377;
 
-      if (c == quit_char
-         || ((c == (0200 | quit_char)) && !meta_key))
+      if (c == quit_char)
        {
          extern SIGTYPE interrupt_signal ();
 
 #ifdef MULTI_FRAME
          /* If this results in a quit_char being returned to Emacs as
-            input, set last-event-frame properly.  If this doesn't
+            input, set Vlast_event_frame properly.  If this doesn't
             get returned to Emacs as an event, the next event read
             will set Vlast_event_frame again, so this is safe to do.  */
          {
@@ -1534,9 +1704,10 @@ kbd_buffer_store_event (event)
              FRAME_FOCUS_FRAME (XFRAME (event->frame_or_window));
 
            if (NILP (focus))
-             Vlast_event_frame = event->frame_or_window;
+             internal_last_event_frame = event->frame_or_window;
            else
-             Vlast_event_frame = focus;
+             internal_last_event_frame = focus;
+           Vlast_event_frame = internal_last_event_frame;
          }
 #endif
 
@@ -1597,6 +1768,7 @@ kbd_buffer_get_event ()
       return obj;
     }
 
+ retry:
   /* Wait until there is input available.  */
   for (;;)
     {
@@ -1648,6 +1820,34 @@ kbd_buffer_get_event ()
 
       obj = Qnil;
 
+      /* These two kinds of events get special handling
+        and don't actually appear to the command loop.  */
+      if (event->kind == selection_request_event)
+       {
+#ifdef HAVE_X11
+         x_handle_selection_request (event);
+         kbd_fetch_ptr = event + 1;
+         goto retry;
+#else
+         /* We're getting selection request events, but we don't have
+             a window system.  */
+         abort ();
+#endif
+       }
+
+      if (event->kind == selection_clear_event)
+       {
+#ifdef HAVE_X11
+         x_handle_selection_clear (event);
+         kbd_fetch_ptr = event + 1;
+         goto retry;
+#else
+         /* We're getting selection request events, but we don't have
+             a window system.  */
+         abort ();
+#endif
+       }
+
 #ifdef MULTI_FRAME
       /* If this event is on a different frame, return a switch-frame this
         time, and leave the event in the queue for next time.  */
@@ -1662,11 +1862,10 @@ kbd_buffer_get_event ()
        if (! NILP (focus))
          frame = focus;
 
-       if (! EQ (frame, Vlast_event_frame))
-         {
-           Vlast_event_frame = frame;
-           obj = make_lispy_switch_frame (frame);
-         }
+       if (! EQ (frame, internal_last_event_frame)
+           && XFRAME (frame) != selected_frame)
+         obj = make_lispy_switch_frame (frame);
+       internal_last_event_frame = frame;
       }
 #endif
 
@@ -1675,8 +1874,6 @@ kbd_buffer_get_event ()
       if (NILP (obj))
        {
          obj = make_lispy_event (event);
-         if (XTYPE (obj) == Lisp_Int)
-           XSET (obj, Lisp_Int, XINT (obj) & (meta_key ? 0377 : 0177));
       
          /* Wipe out this event, to catch bugs.  */
          event->kind = no_event;
@@ -1690,7 +1887,7 @@ kbd_buffer_get_event ()
     {
       FRAME_PTR f;
       Lisp_Object bar_window;
-      enum scrollbar_part part;
+      enum scroll_bar_part part;
       Lisp_Object x, y;
       unsigned long time;
 
@@ -1709,11 +1906,10 @@ kbd_buffer_get_event ()
          if (NILP (frame))
            XSET (frame, Lisp_Frame, f);
 
-         if (! EQ (frame, Vlast_event_frame))
-           {
-             XSET (Vlast_event_frame, Lisp_Frame, frame);
-             obj = make_lispy_switch_frame (Vlast_event_frame);
-           }
+         if (! EQ (frame, internal_last_event_frame)
+             && XFRAME (frame) != selected_frame)
+           obj = make_lispy_switch_frame (internal_last_event_frame);
+         internal_last_event_frame = frame;
        }
 #endif
 
@@ -1727,8 +1923,17 @@ kbd_buffer_get_event ()
        something for us to read!  */
     abort ();
 
+  /* If something gave back nil as the Lispy event,
+     it means the event was discarded, so try again.  */
+  if (NILP (obj))
+    goto retry;
+
   input_pending = readable_events ();
 
+#ifdef MULTI_FRAME
+  Vlast_event_frame = internal_last_event_frame;
+#endif
+
   return (obj);
 }
 
@@ -1743,6 +1948,23 @@ static char *lispy_function_keys[] =
   {
     /* X Keysym value */
 
+    0, 0, 0, 0, 0, 0, 0, 0,    /* 0xff00 */
+    "backspace",
+    "tab",
+    "linefeed",
+    "clear",
+    0,
+    "return",
+    0, 0,
+    0, 0, 0,                   /* 0xff10 */
+    "pause",
+    0, 0, 0, 0, 0, 0, 0,
+    "escape",
+    0, 0, 0, 0,
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   /* 0xff20...2f */
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   /* 0xff30...3f */
+    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   /* 0xff40...4f */
+
     "home",                    /* 0xff50 */    /* IsCursorKey */
     "left",
     "up",
@@ -1806,11 +2028,15 @@ static char *lispy_function_keys[] =
     0, 0,
     "kp-equal",                        /* 0xffbd */
     "f1",                      /* 0xffbe */    /* IsFunctionKey */
-    "f2",      "f3",   "f4",
-    "f5",      "f6",   "f7",   "f8",   "f9",   "f10",  "f11",  "f12",
-    "f13",     "f14",  "f15",  "f16",  "f17",  "f18",  "f19",  "f20",
-    "f21",     "f22",  "f23",  "f24",  "f25",  "f26",  "f27",  "f28",
-    "f29",     "f30",  "f31",  "f32",  "f33",  "f34",  "f35"   /* 0xffe0 */
+    "f2",
+    "f3", "f4", "f5", "f6", "f7", "f8",        "f9", "f10", /* 0xffc0 */
+    "f11", "f12", "f13", "f14", "f15", "f16", "f17", "f18",
+    "f19", "f20", "f21", "f22", "f23", "f24", "f25", "f26", /* 0xffd0 */
+    "f27", "f28", "f29", "f30", "f31", "f32", "f33", "f34",
+    "f35", 0, 0, 0, 0, 0, 0, 0,        /* 0xffe0 */
+    0, 0, 0, 0, 0, 0, 0, 0,
+    0, 0, 0, 0, 0, 0, 0, 0,     /* 0xfff0 */
+    0, 0, 0, 0, 0, 0, 0, "delete"
     };
 
 static char *lispy_mouse_names[] = 
@@ -1818,17 +2044,17 @@ static char *lispy_mouse_names[] =
   "mouse-1", "mouse-2", "mouse-3", "mouse-4", "mouse-5"
 };
 
-/* Scrollbar parts.  */
+/* Scroll bar parts.  */
 Lisp_Object Qabove_handle, Qhandle, Qbelow_handle;
 
-/* An array of scrollbar parts, indexed by an enum scrollbar_part value.  */
-Lisp_Object *scrollbar_parts[] = {
+/* An array of scroll bar parts, indexed by an enum scroll_bar_part value.  */
+Lisp_Object *scroll_bar_parts[] = {
   &Qabove_handle, &Qhandle, &Qbelow_handle
 };
 
 
 /* A vector, indexed by button number, giving the down-going location
-   of currently depressed buttons, both scrollbar and non-scrollbar.
+   of currently depressed buttons, both scroll bar and non-scroll bar.
 
    The elements have the form
      (BUTTON-NUMBER MODIFIER-MASK . REST)
@@ -1860,8 +2086,21 @@ make_lispy_event (event)
     {
       /* A simple keystroke.  */
     case ascii_keystroke:
-      return XFASTINT (event->code);
-      break;
+      {
+       int c = XFASTINT (event->code);
+       /* Include the bits for control and shift
+          only if the basic ASCII code can't indicate them.  */
+       if ((event->modifiers & ctrl_modifier)
+           && c >= 040)
+         c |= ctrl_modifier;
+       if (XFASTINT (event->code) < 040
+           && (event->modifiers & shift_modifier))
+         c |= shift_modifier;
+       c |= (event->modifiers
+             & (meta_modifier | alt_modifier
+                | hyper_modifier | super_modifier));
+       return c;
+      }
 
       /* A function key.  The symbol may need to have modifier prefixes
         tacked onto it.  */
@@ -1876,11 +2115,12 @@ make_lispy_event (event)
       /* A mouse click.  Figure out where it is, decide whether it's 
          a press, click or drag, and build the appropriate structure.  */
     case mouse_click:
-    case scrollbar_click:
+    case scroll_bar_click:
       {
        int button = XFASTINT (event->code);
        Lisp_Object position;
-       Lisp_Object *start_pos;
+       Lisp_Object *start_pos_ptr;
+       Lisp_Object start_pos;
 
        if (button < 0 || button >= NUM_MOUSE_BUTTONS)
          abort ();
@@ -1889,78 +2129,121 @@ make_lispy_event (event)
        if (event->kind == mouse_click)
          {
            int part;
-           Lisp_Object window =
-             window_from_coordinates (XFRAME (event->frame_or_window),
-                                      XINT (event->x), XINT (event->y),
-                                      &part);
+           FRAME_PTR f = XFRAME (event->frame_or_window);
+           Lisp_Object window
+             = window_from_coordinates (f, XINT (event->x), XINT (event->y),
+                                        &part);
            Lisp_Object posn;
 
-           if (XTYPE (window) != Lisp_Window)
+           if (XINT (event->y) < FRAME_MENU_BAR_LINES (f))
+             {
+               int hpos;
+               Lisp_Object items;
+               items = FRAME_MENU_BAR_ITEMS (f);
+               for (; CONSP (items); items = XCONS (items)->cdr)
+                 {
+                   Lisp_Object pos, string;
+                   pos = Fcdr (Fcdr (Fcar (items)));
+                   string = Fcar (Fcdr (Fcar (items)));
+                   if (XINT (event->x) > XINT (pos)
+                       && XINT (event->x) <= XINT (pos) + XSTRING (string)->size)
+                     break;
+                 }
+               position
+                 = Fcons (event->frame_or_window,
+                          Fcons (Qmenu_bar,
+                                 Fcons (Fcons (event->x, event->y),
+                                        Fcons (make_number (event->timestamp),
+                                               Qnil))));
+
+               if (CONSP (items))
+                 return Fcons (Fcar (Fcar (items)),
+                               Fcons (position, Qnil));
+               else
+                 return Fcons (Qnil, Fcons (position, Qnil));
+             }
+           else if (XTYPE (window) != Lisp_Window)
              posn = Qnil;
            else
              {
+               XSETINT (event->x, 
+                        (XINT (event->x) - XINT (XWINDOW (window)->left)));
+               XSETINT (event->y,
+                        (XINT (event->y) - XINT (XWINDOW (window)->top)));
+
                if (part == 1)
                  posn = Qmode_line;
                else if (part == 2)
                  posn = Qvertical_line;
                else
-                 {
-                   XSETINT (event->x, (XINT (event->x)
-                                       - XINT (XWINDOW (window)->left)));
-                   XSETINT (event->y, (XINT (event->y)
-                                       - XINT (XWINDOW (window)->top)));
-                   XSET (posn, Lisp_Int,
-                         buffer_posn_from_coords (XWINDOW (window),
-                                                  XINT (event->x),
-                                                  XINT (event->y)));
-                 }
+                 XSET (posn, Lisp_Int,
+                       buffer_posn_from_coords (XWINDOW (window),
+                                                XINT (event->x),
+                                                XINT (event->y)));
              }
 
-           position =
-             Fcons (window,
-                    Fcons (posn,
-                           Fcons (Fcons (event->x, event->y),
-                                  Fcons (make_number (event->timestamp),
-                                         Qnil))));
+           position
+             Fcons (window,
+                      Fcons (posn,
+                             Fcons (Fcons (event->x, event->y),
+                                    Fcons (make_number (event->timestamp),
+                                           Qnil))));
          }
        else
          {
            Lisp_Object window = event->frame_or_window;
            Lisp_Object portion_whole = Fcons (event->x, event->y);
-           Lisp_Object part = *scrollbar_parts[(int) event->part];
+           Lisp_Object part = *scroll_bar_parts[(int) event->part];
 
            position =
              Fcons (window,
-                    Fcons (Qvertical_scrollbar,
+                    Fcons (Qvertical_scroll_bar,
                            Fcons (portion_whole,
                                   Fcons (make_number (event->timestamp),
                                          Fcons (part,
                                                 Qnil)))));
          }
 
-       start_pos = &XVECTOR (button_down_location)->contents[button];
+       start_pos_ptr = &XVECTOR (button_down_location)->contents[button];
+
+       start_pos = *start_pos_ptr;
+       *start_pos_ptr = Qnil;
 
        /* If this is a button press, squirrel away the location, so
            we can decide later whether it was a click or a drag.  */
        if (event->modifiers & down_modifier)
-         *start_pos = Fcopy_alist (position);
+         *start_pos_ptr = Fcopy_alist (position);
 
        /* Now we're releasing a button - check the co-ordinates to
            see if this was a click or a drag.  */
        else if (event->modifiers & up_modifier)
          {
-           Lisp_Object down = Fnth (make_number (2), *start_pos);
+           /* If we did not see a down before this up,
+              ignore the up.  Probably this happened because
+              the down event chose a menu item.
+              It would be an annoyance to treat the release
+              of the button that chose the menu item
+              as a separate event.  */
 
-           /* The third element of every position should be the (x,y)
-              pair.  */
-           if (! CONSP (down))
-             abort ();
+           if (XTYPE (start_pos) != Lisp_Cons)
+             return Qnil;
 
            event->modifiers &= ~up_modifier;
-           event->modifiers |= ((EQ (event->x, XCONS (down)->car)
-                                 && EQ (event->y, XCONS (down)->cdr))
-                                ? click_modifier
-                                : drag_modifier);
+#if 0 /* Formerly we treated an up with no down as a click event.  */
+           if (XTYPE (start_pos) != Lisp_Cons)
+             event->modifiers |= click_modifier;
+           else
+#endif
+             {
+               /* The third element of every position should be the (x,y)
+                  pair.  */
+               Lisp_Object down = Fnth (make_number (2), start_pos);
+
+               event->modifiers |= ((EQ (event->x, XCONS (down)->car)
+                                     && EQ (event->y, XCONS (down)->cdr))
+                                    ? click_modifier
+                                    : drag_modifier);
+             }
          }
        else
          /* Every mouse event should either have the down_modifier or
@@ -1969,27 +2252,19 @@ make_lispy_event (event)
 
        {
          /* Get the symbol we should use for the mouse click.  */
-         Lisp_Object head =
-           modify_event_symbol (button,
-                                event->modifiers,
-                                Qmouse_click,
-                                lispy_mouse_names, &mouse_syms,
-                                (sizeof (lispy_mouse_names)
-                                 / sizeof (lispy_mouse_names[0])));
+         Lisp_Object head
+           modify_event_symbol (button,
+                                  event->modifiers,
+                                  Qmouse_click,
+                                  lispy_mouse_names, &mouse_syms,
+                                  (sizeof (lispy_mouse_names)
+                                   / sizeof (lispy_mouse_names[0])));
          
          if (event->modifiers & drag_modifier)
-           {
-             Lisp_Object lispy_event =
-               Fcons (head,
-                      Fcons (*start_pos,
-                             Fcons (position,
-                                    Qnil)));
-             
-             /* Allow this to be GC'd.  */
-             *start_pos = Qnil;
-
-             return lispy_event;
-           }
+           return Fcons (head,
+                         Fcons (start_pos,
+                                Fcons (position,
+                                       Qnil)));
          else
            return Fcons (head,
                          Fcons (position,
@@ -1999,7 +2274,7 @@ make_lispy_event (event)
 
       /* The 'kind' field of the event is something we don't recognize.  */
     default:
-      abort();
+      abort ();
     }
 }
 
@@ -2007,21 +2282,21 @@ static Lisp_Object
 make_lispy_movement (frame, bar_window, part, x, y, time)
      FRAME_PTR frame;
      Lisp_Object bar_window;
-     enum scrollbar_part part;
+     enum scroll_bar_part part;
      Lisp_Object x, y;
      unsigned long time;
 {
-  /* Is it a scrollbar movement?  */
+  /* Is it a scroll bar movement?  */
   if (frame && ! NILP (bar_window))
     {
-      Lisp_Object part = *scrollbar_parts[(int) part];
+      Lisp_Object part_sym = *scroll_bar_parts[(int) part];
 
-      return Fcons (Qscrollbar_movement,
+      return Fcons (Qscroll_bar_movement,
                    (Fcons (Fcons (bar_window,
-                                  Fcons (Qvertical_scrollbar,
+                                  Fcons (Qvertical_scroll_bar,
                                          Fcons (Fcons (x, y),
                                                 Fcons (make_number (time),
-                                                       Fcons (part,
+                                                       Fcons (part_sym,
                                                               Qnil))))),
                            Qnil)));
     }
@@ -2066,7 +2341,6 @@ make_lispy_movement (frame, bar_window, part, x, y, time)
     }
 }
 
-
 /* Construct a switch frame event.  */
 static Lisp_Object
 make_lispy_switch_frame (frame)
@@ -2074,7 +2348,6 @@ make_lispy_switch_frame (frame)
 {
   return Fcons (Qswitch_frame, Fcons (frame, Qnil));
 }
-
 \f
 /* Manipulating modifiers.  */
 
@@ -2130,11 +2403,7 @@ parse_modifiers_uncached (symbol, modifier_end)
        break;
 
       case 's':
-       if (i + 6 > name->size
-           || strncmp (name->data + i, "super-", 6))
-         goto no_more_modifiers;
-       modifiers |= super_modifier;
-       i += 6;
+       SINGLE_LETTER_MOD (super_modifier);
        break;
 
       case 'd':
@@ -2188,7 +2457,7 @@ apply_modifiers_uncached (modifiers, base, base_len)
      to use Fintern, which expects a genuine Lisp_String, and keeps a
      reference to it.  */
   char *new_mods =
-    (char *) alloca (sizeof ("A-C-H-M-S-super-down-drag-"));
+    (char *) alloca (sizeof ("A-C-H-M-S-s-down-drag-"));
   int mod_len;
 
   {
@@ -2204,7 +2473,7 @@ apply_modifiers_uncached (modifiers, base, base_len)
     if (modifiers & hyper_modifier) { *p++ = 'H'; *p++ = '-'; }
     if (modifiers & meta_modifier)  { *p++ = 'M'; *p++ = '-'; }
     if (modifiers & shift_modifier) { *p++ = 'S'; *p++ = '-'; }
-    if (modifiers & super_modifier) { strcpy (p, "super-"); p += 6; }
+    if (modifiers & super_modifier) { *p++ = 's'; *p++ = '-'; }
     if (modifiers & down_modifier)  { strcpy (p, "down-");  p += 5; }
     if (modifiers & drag_modifier)  { strcpy (p, "drag-");  p += 5; }
     /* The click modifier is denoted by the absence of other modifiers.  */
@@ -2227,9 +2496,11 @@ apply_modifiers_uncached (modifiers, base, base_len)
 
 static char *modifier_names[] =
 {
-  "up", "alt", "control", "hyper", "meta", "shift", "super", "down", "drag",
-  "click"
+  "up", 0, 0, 0, 0, 0, 0, "down",
+  "drag", "click", 0, 0, 0, 0, 0, 0,
+  0, 0, "alt", "super", "hyper", "shift", "control", "meta"
 };
+#define NUM_MOD_NAMES (sizeof (modifier_names) / sizeof (modifier_names[0]))
 
 static Lisp_Object modifier_symbols;
 
@@ -2242,14 +2513,10 @@ lispy_modifier_list (modifiers)
   int i;
 
   modifier_list = Qnil;
-  for (i = 0; (1<<i) <= modifiers; i++)
+  for (i = 0; (1<<i) <= modifiers && i < NUM_MOD_NAMES; i++)
     if (modifiers & (1<<i))
-      {
-       if (i >= XVECTOR (modifier_symbols)->size)
-         abort ();
-       modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
-                              modifier_list);
-      }
+      modifier_list = Fcons (XVECTOR (modifier_symbols)->contents[i],
+                            modifier_list);
 
   return modifier_list;
 }
@@ -2279,6 +2546,8 @@ parse_modifiers (symbol)
                   Qnil);
       Lisp_Object mask;
 
+      if (modifiers & ~((1<<VALBITS) - 1))
+       abort ();
       XFASTINT (mask) = modifiers;
       elements = Fcons (unmodified, Fcons (mask, Qnil));
 
@@ -2313,6 +2582,9 @@ apply_modifiers (modifiers, base)
 {
   Lisp_Object cache, index, entry, new_symbol;
 
+  /* Mask out upper bits.  We don't know where this value's been.  */
+  modifiers &= (1<<VALBITS) - 1;
+
   /* The click modifier never figures into cache indices.  */
   cache = Fget (base, Qmodifier_cache);
   XFASTINT (index) = (modifiers & ~click_modifier);
@@ -2344,8 +2616,8 @@ apply_modifiers (modifiers, base)
      You'd think we could just set this once and for all when we
      intern the symbol above, but reorder_modifiers may call us when
      BASE's property isn't set right; we can't assume that just
-     because we found something in the cache it must have its kind set
-     right.  */
+     because it has a Qmodifier_cache property it must have its
+     Qevent_kind set right as well.  */
   if (NILP (Fget (new_symbol, Qevent_kind)))
     {
       Lisp_Object kind = Fget (base, Qevent_kind);
@@ -2453,21 +2725,6 @@ modify_event_symbol (symbol_num, modifiers, symbol_kind, name_table,
   return apply_modifiers (modifiers, *slot);
 }
 
-\f
-DEFUN ("mouse-click-p", Fmouse_click_p, Smouse_click_p, 1, 1, 0,
-  "Return non-nil iff OBJECT is a representation of a mouse event.\n\
-A mouse event is a list of five elements whose car is a symbol of the\n\
-form <MODIFIERS>mouse-<DIGIT>.  I hope this is a temporary hack.")
-  (object)
-     Lisp_Object object;
-{
-  if (EVENT_HAS_PARAMETERS (object)
-      && EQ (EVENT_HEAD_KIND (EVENT_HEAD (object)),
-            Qmouse_click))
-    return Qt;
-  else
-    return Qnil;
-}
 \f
 /* Store into *addr a value nonzero if terminal input chars are available.
    Serves the purpose of ioctl (0, FIONREAD, addr)
@@ -2546,6 +2803,9 @@ read_avail_input (expected)
        /* Formerly simply reported no input, but that sometimes led to
           a failure of Emacs to terminate.
           SIGHUP seems appropriate if we can't reach the terminal.  */
+       /* ??? Is it really right to send the signal just to this process
+          rather than to the whole process group?
+          Perhaps on systems with FIONREAD Emacs is alone in its group.  */
        kill (getpid (), SIGHUP);
       if (nread == 0)
        return 0;
@@ -2591,6 +2851,12 @@ read_avail_input (expected)
       for (i = 0; i < nread; i++)
        {
          buf[i].kind = ascii_keystroke;
+         buf[i].modifiers = 0;
+         if (meta_key == 1 && (cbuf[i] & 0x80))
+           buf[i].modifiers = meta_modifier;
+         if (meta_key != 2)
+           cbuf[i] &= ~0x80;
+           
          XSET (buf[i].code,            Lisp_Int,   cbuf[i]);
 #ifdef MULTI_FRAME
          XSET (buf[i].frame_or_window, Lisp_Frame, selected_frame);
@@ -2662,6 +2928,22 @@ input_available_signal (signo)
   errno = old_errno;
 }
 #endif /* SIGIO */
+
+/* Send ourselves a SIGIO.
+
+   This function exists so that the UNBLOCK_INPUT macro in
+   blockinput.h can have some way to take care of input we put off
+   dealing with, without assuming that every file which uses
+   UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
+void
+reinvoke_input_signal ()
+{
+#ifdef SIGIO  
+  kill (0, SIGIO);
+#endif
+}
+
+
 \f
 /* Return the prompt-string of a sparse keymap.
    This is the first element which is a string.
@@ -2682,11 +2964,152 @@ map_prompt (map)
   return Qnil;
 }
 
+static Lisp_Object menu_bar_item ();
+static Lisp_Object menu_bar_one_keymap ();
+
+/* Return a list of menu items for a menu bar, appropriate
+   to the current buffer.
+   The elements have the form (KEY STRING . nil).  */
+
+Lisp_Object
+menu_bar_items ()
+{
+  /* The number of keymaps we're scanning right now, and the number of
+     keymaps we have allocated space for.  */
+  int nmaps;
+
+  /* maps[0..nmaps-1] are the prefix definitions of KEYBUF[0..t-1]
+     in the current keymaps, or nil where it is not a prefix.  */
+  Lisp_Object *maps;
+
+  Lisp_Object def, tem;
+
+  Lisp_Object result;
+
+  int mapno;
+
+  /* Build our list of keymaps.
+     If we recognize a function key and replace its escape sequence in
+     keybuf with its symbol, or if the sequence starts with a mouse
+     click and we need to switch buffers, we jump back here to rebuild
+     the initial keymaps from the current buffer.  */
+  { 
+    Lisp_Object *tmaps;
+
+    nmaps = current_minor_maps (0, &tmaps) + 2;
+    maps = (Lisp_Object *) alloca (nmaps * sizeof (maps[0]));
+    bcopy (tmaps, maps, (nmaps - 2) * sizeof (maps[0]));
+#ifdef USE_TEXT_PROPERTIES
+    maps[nmaps-2] = get_local_map (PT, current_buffer);
+#else
+    maps[nmaps-2] = current_buffer->local_map;
+#endif
+    maps[nmaps-1] = global_map;
+  }
+
+  /* Look up in each map the dummy prefix key `menu-bar'.  */
+
+  result = Qnil;
+
+  for (mapno = 0; mapno < nmaps; mapno++)
+    {
+      if (! NILP (maps[mapno]))
+       def = get_keyelt (access_keymap (maps[mapno], Qmenu_bar, 1));
+      else
+       def = Qnil;
+
+      tem = Fkeymapp (def);
+      if (!NILP (tem))
+       result = menu_bar_one_keymap (def, result);
+    }
+
+  return Fnreverse (result);
+}
+\f
+/* Scan one map KEYMAP, accumulating any menu items it defines
+   that have not yet been seen in RESULT.  Return the updated RESULT.  */
+
+static Lisp_Object
+menu_bar_one_keymap (keymap, result)
+     Lisp_Object keymap, result;
+{
+  Lisp_Object tail, item, key, binding, item_string, table;
+
+  /* Loop over all keymap entries that have menu strings.  */
+  for (tail = keymap; XTYPE (tail) == Lisp_Cons; tail = XCONS (tail)->cdr)
+    {
+      item = XCONS (tail)->car;
+      if (XTYPE (item) == Lisp_Cons)
+       {
+         key = XCONS (item)->car;
+         binding = XCONS (item)->cdr;
+         if (XTYPE (binding) == Lisp_Cons)
+           {
+             item_string = XCONS (binding)->car;
+             if (XTYPE (item_string) == Lisp_String)
+               result = menu_bar_item (key, item_string,
+                                       Fcdr (binding), result);
+           }
+       }
+      else if (XTYPE (item) == Lisp_Vector)
+       {
+         /* Loop over the char values represented in the vector.  */
+         int len = XVECTOR (item)->size;
+         int c;
+         for (c = 0; c < len; c++)
+           {
+             Lisp_Object character;
+             XFASTINT (character) = c;
+             binding = XVECTOR (item)->contents[c];
+             if (XTYPE (binding) == Lisp_Cons)
+               {
+                 item_string = XCONS (binding)->car;
+                 if (XTYPE (item_string) == Lisp_String)
+                   result = menu_bar_item (key, item_string,
+                                           Fcdr (binding), result);
+               }
+           }
+       }
+    }
+
+  return result;
+}
+
+static Lisp_Object
+menu_bar_item (key, item_string, def, result)
+     Lisp_Object key, item_string, def, result;
+{
+  Lisp_Object tem, elt;
+  Lisp_Object enabled;
+
+  /* See if this entry is enabled.  */
+  enabled = Qt;
+
+  if (XTYPE (def) == Lisp_Symbol)
+    {
+      /* No property, or nil, means enable.
+        Otherwise, enable if value is not nil.  */
+      tem = Fget (def, Qmenu_enable);
+      if (!NILP (tem))
+       enabled = Feval (tem);
+    }
+
+  /* Add an entry for this key and string
+     if there is none yet.  */
+  elt = Fassq (key, result);
+  if (!NILP (enabled) && NILP (elt))
+    result = Fcons (Fcons (key, Fcons (item_string, Qnil)), result);
+
+  return result;
+}
+\f
 static int echo_flag;
 static int echo_now;
 
 /* Read a character like read_char but optionally prompt based on maps
-   in the array MAPS.  NMAPS is the length of MAPS.
+   in the array MAPS.  NMAPS is the length of MAPS.  Return nil if we
+   decided not to read a character, because there are no menu items in
+   MAPS.
 
    PREV_EVENT is the previous input event, or nil if we are reading
    the first event of a key sequence.
@@ -2730,14 +3153,14 @@ read_char_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
     }
 
   /* If we don't have any menus, just read a character normally.  */
-  if (NILP (name))
+  if (mapno >= nmaps)
     return Qnil;
 
-#ifdef HAVE_X_WINDOW
-#ifndef NO_X_MENU
+#ifdef HAVE_X_WINDOWS
+#ifdef HAVE_X_MENU
   /* If we got to this point via a mouse click,
      use a real menu for mouse selection.  */
-  if (XTYPE (prev_event) == Lisp_Cons)
+  if (EVENT_HAS_PARAMETERS (prev_event))
     {
       /* Display the menu and get the selection.  */
       Lisp_Object *realmaps
@@ -2751,14 +3174,23 @@ read_char_menu_prompt (nmaps, maps, prev_event, used_mouse_menu)
          realmaps[nmaps1++] = maps[mapno];
 
       value = Fx_popup_menu (prev_event, Flist (nmaps1, realmaps));
+      if (CONSP (value))
+       {
+         /* If we got more than one event, put all but the first
+            onto this list to be read later.
+            Return just the first event now.  */
+         unread_command_events
+           = nconc2 (XCONS (value)->cdr, unread_command_events);
+         value = XCONS (value)->car;
+       }
       if (NILP (value))
        XSET (value, Lisp_Int, quit_char);
       if (used_mouse_menu)
        *used_mouse_menu = 1;
       return value;
     }
-#endif /* not NO_X_MENU */
-#endif /* HAVE_X_WINDOW */
+#endif /* HAVE_X_MENU */
+#endif /* HAVE_X_WINDOWS */
 
   /* Prompt string always starts with map's prompt, and a space.  */
   strcpy (menu, XSTRING (name)->data);
@@ -2909,8 +3341,7 @@ follow_key (key, nmaps, current, defs, next)
 
   /* If KEY is a meta ASCII character, treat it like meta-prefix-char
      followed by the corresponding non-meta character.  */
-  if (XTYPE (key) == Lisp_Int
-      && XINT (key) >= 0200)
+  if (XTYPE (key) == Lisp_Int && (XINT (key) & CHAR_META))
     {
       for (i = 0; i < nmaps; i++)
        if (! NILP (current[i]))
@@ -2927,7 +3358,7 @@ follow_key (key, nmaps, current, defs, next)
          next[i] = Qnil;
 
       current = next;
-      XSET (key, Lisp_Int, XFASTINT (key) & 0177);
+      XSET (key, Lisp_Int, XFASTINT (key) & ~CHAR_META);
     }
 
   first_binding = nmaps;
@@ -2948,9 +3379,13 @@ follow_key (key, nmaps, current, defs, next)
      lower-case letter, return the bindings for the lower-case letter.  */
   if (first_binding == nmaps
       && XTYPE (key) == Lisp_Int
-      && UPPERCASEP (XINT (key)))
+      && (UPPERCASEP (XINT (key) & 0x3ffff)
+         || (XINT (key) & shift_modifier)))
     {
-      XSETINT (key, DOWNCASE (XINT (key)));
+      if (XINT (key) & shift_modifier)
+       XSETINT (key, XINT (key) & ~shift_modifier);
+      else
+       XSETINT (key, DOWNCASE (XINT (key)));
 
       first_binding = nmaps;
       for (i = nmaps - 1; i >= 0; i--)
@@ -2965,7 +3400,7 @@ follow_key (key, nmaps, current, defs, next)
            defs[i] = Qnil;
        }
     }
-  
+
   /* Given the set of bindings we've found, produce the next set of maps.  */
   if (first_binding < nmaps)
     for (i = 0; i < nmaps; i++)
@@ -3004,6 +3439,7 @@ follow_key (key, nmaps, current, defs, next)
    If the user switches frames in the midst of a key sequence, we put
    off the switch-frame event until later; the next call to
    read_char will return it.  */
+
 static int
 read_key_sequence (keybuf, bufsize, prompt)
      Lisp_Object *keybuf;
@@ -3120,7 +3556,11 @@ read_key_sequence (keybuf, bufsize, prompt)
        nmaps_allocated = nmaps;
       }
     bcopy (maps, submaps, (nmaps - 2) * sizeof (submaps[0]));
-    submaps[nmaps-2] = current_buffer->keymap;
+#ifdef USE_TEXT_PROPERTIES
+    submaps[nmaps-2] = get_local_map (PT, current_buffer);
+#else
+    submaps[nmaps-2] = current_buffer->local_map;
+#endif
     submaps[nmaps-1] = global_map;
   }
 
@@ -3199,7 +3639,7 @@ read_key_sequence (keybuf, bufsize, prompt)
          /* read_char returns -1 at the end of a macro.
             Emacs 18 handles this by returning immediately with a
             zero, so that's what we'll do.  */
-         if (XTYPE (key) == Lisp_Int && XINT (key) < 0)
+         if (XTYPE (key) == Lisp_Int && XINT (key) == -1)
            {
              t = 0;
              goto done;
@@ -3272,6 +3712,28 @@ read_key_sequence (keybuf, bufsize, prompt)
                      goto replay_key;
                    }
                }
+             else
+               {
+                 Lisp_Object posn   = POSN_BUFFER_POSN (EVENT_START (key));
+
+                 /* Handle menu-bar events:
+                    insert the dummy prefix char `menu-bar'.  */
+                 if (EQ (posn, Qmenu_bar))
+                   {
+                     if (t + 1 >= bufsize)
+                       error ("key sequence too long");
+                     /* Run the Lucid hook.  */
+                     call1 (Vrun_hooks, Qactivate_menubar_hook);
+                     /* If it has changed current-menubar from previous value,
+                        really recompute the menubar from the value.  */
+                     if (! NILP (Vlucid_menu_bar_dirty_flag))
+                       call0 (Qrecompute_lucid_menubar);
+                     keybuf[t] = posn;
+                     keybuf[t+1] = key;
+                     mock_input = t + 2;
+                     goto replay_sequence;
+                   }
+               }
            }
        }
 
@@ -3292,8 +3754,7 @@ read_key_sequence (keybuf, bufsize, prompt)
          if (XTYPE (head) == Lisp_Symbol)
            {
              Lisp_Object breakdown = parse_modifiers (head);
-             Lisp_Object modifiers =
-               XINT (XCONS (XCONS (breakdown)->cdr)->car);
+             int modifiers = XINT (XCONS (XCONS (breakdown)->cdr)->car);
 
              /* We drop unbound `down-' events altogether.  */
              if (modifiers & down_modifier)
@@ -3395,7 +3856,7 @@ read_key_sequence (keybuf, bufsize, prompt)
              key = keybuf[fkey_end++];
              /* Look up meta-characters by prefixing them
                 with meta_prefix_char.  I hate this.  */
-             if (XTYPE (key) == Lisp_Int && XINT (key) & 0x80)
+             if (XTYPE (key) == Lisp_Int && XINT (key) & meta_modifier)
                {
                  fkey_next =
                    get_keymap_1
@@ -3403,7 +3864,7 @@ read_key_sequence (keybuf, bufsize, prompt)
                       (access_keymap
                        (fkey_map, meta_prefix_char, 1)),
                       0, 1);
-                 XFASTINT (key) = XFASTINT (key) & 0x7f;
+                 XFASTINT (key) = XFASTINT (key) & ~meta_modifier;
                }
              else
                fkey_next = fkey_map;
@@ -3464,30 +3925,29 @@ First arg PROMPT is a prompt string.  If nil, do not prompt specially.\n\
 Second (optional) arg CONTINUE-ECHO, if non-nil, means this key echos\n\
 as a continuation of the previous key.\n\
 \n\
-
-A C-g typed while in this function is treated like any other character,
-and `quit-flag' is not set.
-
-If the key sequence starts with a mouse click, then the sequence is read
-using the keymaps of the buffer of the window clicked in, not the buffer
-of the selected window as normal.
-
-`read-key-sequence' drops unbound button-down events, since you normally
-only care about the click or drag events which follow them.  If a drag
-event is unbound, but the corresponding click event would be bound,
-`read-key-sequence' turns the drag event into a click event at the
-drag's starting position.  This means that you don't have to distinguish
-between click and drag events unless you want to.
-
-`read-key-sequence' prefixes mouse events on mode lines, the vertical
-lines separating windows, and scrollbars with imaginary keys
-`mode-line', `vertical-line', and `vertical-scrollbar'.
-
-If the user switches frames in the middle of a key sequence, the
-frame-switch event is put off until after the current key sequence.
-
-`read-key-sequence' checks `function-key-map' for function key
-sequences, where they wouldn't conflict with ordinary bindings.  See
+A C-g typed while in this function is treated like any other character,\n\
+and `quit-flag' is not set.\n\
+\n\
+If the key sequence starts with a mouse click, then the sequence is read\n\
+using the keymaps of the buffer of the window clicked in, not the buffer\n\
+of the selected window as normal.\n\
+\n\
+`read-key-sequence' drops unbound button-down events, since you normally\n\
+only care about the click or drag events which follow them.  If a drag\n\
+event is unbound, but the corresponding click event would be bound,\n\
+`read-key-sequence' turns the drag event into a click event at the\n\
+drag's starting position.  This means that you don't have to distinguish\n\
+between click and drag events unless you want to.\n\
+\n\
+`read-key-sequence' prefixes mouse events on mode lines, the vertical\n\
+lines separating windows, and scroll bars with imaginary keys\n\
+`mode-line', `vertical-line', and `vertical-scroll-bar'.\n\
+\n\
+If the user switches frames in the middle of a key sequence, the\n\
+frame-switch event is put off until after the current key sequence.\n\
+\n\
+`read-key-sequence' checks `function-key-map' for function key\n\
+sequences, where they wouldn't conflict with ordinary bindings.  See\n\
 `function-key-map' for more details.")
   (prompt, continue_echo)
      Lisp_Object prompt, continue_echo;
@@ -3511,7 +3971,7 @@ sequences, where they wouldn't conflict with ordinary bindings.  See
                         NILP (prompt) ? 0 : XSTRING (prompt)->data);
 
   UNGCPRO;
-  return make_array (i, keybuf);
+  return make_event_array (i, keybuf);
 }
 \f
 DEFUN ("command-execute", Fcommand_execute, Scommand_execute, 1, 2, 0,
@@ -3677,7 +4137,7 @@ DEFUN ("input-pending-p", Finput_pending_p, Sinput_pending_p, 0, 0, 0,
 Actually, the value is nil only if we can be sure that no input is available.")
   ()
 {
-  if (!NILP (unread_command_event))
+  if (!NILP (unread_command_events) || unread_command_char != -1)
     return (Qt);
 
   return detect_input_pending () ? Qt : Qnil;
@@ -3706,11 +4166,12 @@ DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
 }
 
 DEFUN ("this-command-keys", Fthis_command_keys, Sthis_command_keys, 0, 0, 0,
-  "Return string of the keystrokes that invoked this command.")
+  "Return the key sequence that invoked this command.\n\
+The value is a string or a vector.")
   ()
 {
-  return make_array (this_command_key_count,
-                    XVECTOR (this_command_keys)->contents);
+  return make_event_array (this_command_key_count,
+                          XVECTOR (this_command_keys)->contents);
 }
 
 DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
@@ -3724,7 +4185,8 @@ DEFUN ("recursion-depth", Frecursion_depth, Srecursion_depth, 0, 0, 0,
 
 DEFUN ("open-dribble-file", Fopen_dribble_file, Sopen_dribble_file, 1, 1,
   "FOpen dribble file: ",
-  "Start writing all keyboard characters to FILE.")
+  "Start writing all keyboard characters to a dribble file called FILE.\n\
+If FILE is nil, close any open dribble file.")
   (file)
      Lisp_Object file;
 {
@@ -3749,7 +4211,8 @@ Also cancel any kbd macro being defined.")
   defining_kbd_macro = 0;
   update_mode_lines++;
 
-  unread_command_event = Qnil;
+  unread_command_events = Qnil;
+  unread_command_char = -1;
 
   discard_tty_input ();
 
@@ -3767,36 +4230,33 @@ DEFUN ("suspend-emacs", Fsuspend_emacs, Ssuspend_emacs, 0, 1, "",
   "Stop Emacs and return to superior process.  You can resume later.\n\
 On systems that don't have job control, run a subshell instead.\n\n\
 If optional arg STUFFSTRING is non-nil, its characters are stuffed\n\
-to be read as terminal input by Emacs's superior shell.\n\
-Before suspending, if `suspend-hook' is bound and value is non-nil\n\
-call the value as a function of no args.  Don't suspend if it returns non-nil.\n\
-Otherwise, suspend normally and after resumption call\n\
+to be read as terminal input by Emacs's parent, after suspension.\n\
+\n\
+Before suspending, call the functions in `suspend-hook' with no args.\n\
+If any of them returns nil, don't call the rest and don't suspend.\n\
+Otherwise, suspend normally and after resumption run the normal hook\n\
 `suspend-resume-hook' if that is bound and non-nil.\n\
 \n\
 Some operating systems cannot stop the Emacs process and resume it later.\n\
-On such systems, Emacs will start a subshell and wait for it to exit.")
+On such systems, Emacs starts a subshell instead of suspending.")
   (stuffstring)
      Lisp_Object stuffstring;
 {
-  register Lisp_Object tem;
+  Lisp_Object tem;
   int count = specpdl_ptr - specpdl;
   int old_height, old_width;
   int width, height;
-  struct gcpro gcpro1;
+  struct gcpro gcpro1, gcpro2;
   extern init_sys_modes ();
 
   if (!NILP (stuffstring))
     CHECK_STRING (stuffstring, 0);
-  GCPRO1 (stuffstring);
 
-  /* Call value of suspend-hook
-     if it is bound and value is non-nil.  */
+  /* Run the functions in suspend-hook.  */
   if (!NILP (Vrun_hooks))
-    {
-      tem = call1 (Vrun_hooks, intern ("suspend-hook"));
-      if (!EQ (tem, Qnil)) return Qnil;
-    }
+    call1 (Vrun_hooks, intern ("suspend-hook"));
 
+  GCPRO1 (stuffstring);
   get_frame_size (&old_width, &old_height);
   reset_sys_modes ();
   /* sys_suspend can get an error if it tries to fork a subshell
@@ -3813,8 +4273,7 @@ On such systems, Emacs will start a subshell and wait for it to exit.")
   if (width != old_width || height != old_height)
     change_frame_size (0, height, width, 0, 0);
 
-  /* Call value of suspend-resume-hook
-     if it is bound and value is non-nil.  */
+  /* Run suspend-resume-hook.  */
   if (!NILP (Vrun_hooks))
     call1 (Vrun_hooks, intern ("suspend-resume-hook"));
   
@@ -3907,7 +4366,6 @@ interrupt_signal ()
   char c;
   /* Must preserve main program's value of errno.  */
   int old_errno = errno;
-  extern Lisp_Object Vwindow_system;
 
 #ifdef USG
   /* USG systems forget handlers when they are used;
@@ -3997,7 +4455,14 @@ quit_throw_to_read_char ()
   clear_waiting_for_input ();
   input_pending = 0;
 
-  unread_command_event = Qnil;
+  unread_command_events = Qnil;
+  unread_command_char = -1;
+
+#ifdef POLL_FOR_INPUT
+  /* May be > 1 if in recursive minibuffer.  */
+  if (poll_suppress_count == 0)
+    abort ();
+#endif
 
   _longjmp (getcjmp, 1);
 }
@@ -4008,8 +4473,9 @@ First arg INTERRUPT non-nil means use input interrupts;\n\
  nil means use CBREAK mode.\n\
 Second arg FLOW non-nil means use ^S/^Q flow control for output to terminal\n\
  (no effect except in CBREAK mode).\n\
-Third arg META non-nil means accept 8-bit input (for a Meta key).\n\
- Otherwise, the top bit is ignored, on the assumption it is parity.\n\
+Third arg META t means accept 8-bit input (for a Meta key).\n\
+ META nil means ignore the top bit, on the assumption it is parity.\n\
+ Otherwise, accept 8-bit input and don't use the top bit for Meta.\n\
 Optional fourth arg QUIT if non-nil specifies character to use for quitting.")
   (interrupt, flow, meta, quit)
      Lisp_Object interrupt, flow, meta, quit;
@@ -4036,7 +4502,12 @@ Optional fourth arg QUIT if non-nil specifies character to use for quitting.")
   interrupt_input = 1;
 #endif
   flow_control = !NILP (flow);
-  meta_key = !NILP (meta);
+  if (NILP (meta))
+    meta_key = 0;
+  else if (EQ (meta, Qt))
+    meta_key = 1;
+  else
+    meta_key = 2;
   if (!NILP (quit))
     /* Don't let this value be out of range.  */
     quit_char = XINT (quit) & (meta_key ? 0377 : 0177);
@@ -4044,6 +4515,31 @@ Optional fourth arg QUIT if non-nil specifies character to use for quitting.")
   init_sys_modes ();
   return Qnil;
 }
+
+DEFUN ("current-input-mode", Fcurrent_input_mode, Scurrent_input_mode, 0, 0, 0,
+  "Return information about the way Emacs currently reads keyboard input.\n\
+The value is a list of the form (INTERRUPT FLOW META QUIT), where\n\
+  INTERRUPT is non-nil if Emacs is using interrupt-driven input; if\n\
+    nil, Emacs is using CBREAK mode.\n\
+  FLOW is non-nil if Emacs uses ^S/^Q flow control for output to the\n\
+    terminal; this does not apply if Emacs uses interrupt-driven input.\n\
+  META is non-nil if Emacs is accepting 8-bit input; otherwise, Emacs\n\
+    clears the eighth bit of every input character.\n\
+  QUIT is the character Emacs currently uses to quit.\n\
+The elements of this list correspond to the arguments of\n\
+set-input-mode.")
+  ()
+{
+  Lisp_Object val[4];
+
+  val[0] = interrupt_input ? Qt : Qnil;
+  val[1] = flow_control ? Qt : Qnil;
+  val[2] = meta_key ? Qt : Qnil;
+  XSETINT (val[3], quit_char);
+
+  return Flist (val, sizeof (val) / sizeof (val[0]));
+}
+
 \f
 init_keyboard ()
 {
@@ -4051,7 +4547,8 @@ init_keyboard ()
   command_loop_level = -1;
   immediate_quit = 0;
   quit_char = Ctl ('g');
-  unread_command_event = Qnil;
+  unread_command_events = Qnil;
+  unread_command_char = -1;
   total_keys = 0;
   recent_keys_index = 0;
   kbd_fetch_ptr = kbd_buffer;
@@ -4062,7 +4559,8 @@ init_keyboard ()
 #ifdef MULTI_FRAME
   /* This means that command_loop_1 won't try to select anything the first
      time through.  */
-  Vlast_event_frame = Qnil;
+  internal_last_event_frame = Qnil;
+  Vlast_event_frame = internal_last_event_frame;
 #endif
 
   /* If we're running a dumped Emacs, we need to clear out
@@ -4077,7 +4575,7 @@ init_keyboard ()
   if (!noninteractive)
     {
       signal (SIGINT, interrupt_signal);
-#ifdef HAVE_TERMIO
+#if defined (HAVE_TERMIO) || defined (HAVE_TERMIOS)
       /* For systems with SysV TERMIO, C-g is set up for both SIGINT and
         SIGQUIT and we can't tell which one it will give us.  */
       signal (SIGQUIT, interrupt_signal);
@@ -4124,7 +4622,7 @@ struct event_head {
 
 struct event_head head_table[] = {
   &Qmouse_movement,    "mouse-movement",       &Qmouse_movement,
-  &Qscrollbar_movement, "scrollbar-movement",  &Qmouse_movement,
+  &Qscroll_bar_movement, "scroll-bar-movement",        &Qmouse_movement,
   &Qswitch_frame,      "switch-frame",         &Qswitch_frame,
 };
 
@@ -4142,17 +4640,28 @@ syms_of_keyboard ()
   Qdisabled = intern ("disabled");
   staticpro (&Qdisabled);
 
+  Qpre_command_hook = intern ("pre-command-hook");
+  staticpro (&Qpre_command_hook);
+
+  Qpost_command_hook = intern ("post-command-hook");
+  staticpro (&Qpost_command_hook);
+
   Qfunction_key = intern ("function-key");
   staticpro (&Qfunction_key);
   Qmouse_click = intern ("mouse-click");
   staticpro (&Qmouse_click);
 
+  Qmenu_enable = intern ("menu-enable");
+  staticpro (&Qmenu_enable);
+
   Qmode_line = intern ("mode-line");
   staticpro (&Qmode_line);
   Qvertical_line = intern ("vertical-line");
   staticpro (&Qvertical_line);
-  Qvertical_scrollbar = intern ("vertical-scrollbar");
-  staticpro (&Qvertical_scrollbar);
+  Qvertical_scroll_bar = intern ("vertical-scroll-bar");
+  staticpro (&Qvertical_scroll_bar);
+  Qmenu_bar = intern ("menu-bar");
+  staticpro (&Qmenu_bar);
 
   Qabove_handle = intern ("above-handle");
   staticpro (&Qabove_handle);
@@ -4170,6 +4679,11 @@ syms_of_keyboard ()
   Qmodifier_cache = intern ("modifier-cache");
   staticpro (&Qmodifier_cache);
 
+  Qrecompute_lucid_menubar = intern ("recompute-lucid-menubar");
+  staticpro (&Qrecompute_lucid_menubar);
+  Qactivate_menubar_hook = intern ("activate-menubar-hook");
+  staticpro (&Qactivate_menubar_hook);
+
   {
     struct event_head *p;
 
@@ -4193,7 +4707,8 @@ syms_of_keyboard ()
 
     modifier_symbols = Fmake_vector (make_number (len), Qnil);
     for (i = 0; i < len; i++)
-      XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
+      if (modifier_names[i])
+       XVECTOR (modifier_symbols)->contents[i] = intern (modifier_names[i]);
     staticpro (&modifier_symbols);
   }
 
@@ -4219,7 +4734,6 @@ syms_of_keyboard ()
   defsubr (&Sread_key_sequence);
   defsubr (&Srecursive_edit);
   defsubr (&Strack_mouse);
-  defsubr (&Smouse_click_p);
   defsubr (&Sinput_pending_p);
   defsubr (&Scommand_execute);
   defsubr (&Srecent_keys);
@@ -4232,6 +4746,7 @@ syms_of_keyboard ()
   defsubr (&Sdiscard_input);
   defsubr (&Sopen_dribble_file);
   defsubr (&Sset_input_mode);
+  defsubr (&Scurrent_input_mode);
   defsubr (&Sexecute_extended_command);
 
   DEFVAR_LISP ("disabled-command-hook", &Vdisabled_command_hook,
@@ -4239,19 +4754,28 @@ syms_of_keyboard ()
 \(has a non-nil `disabled' property).");
 
   DEFVAR_LISP ("last-command-char", &last_command_char,
-    "Last terminal input key that was part of a command.");
+    "Last input event that was part of a command.");
+
+  DEFVAR_LISP ("last-command-event", &last_command_char,
+    "Last input event that was part of a command.");
 
   DEFVAR_LISP ("last-nonmenu-event", &last_nonmenu_event,
-    "Last terminal input key in a command, except for mouse menus.\n\
+    "Last input event in a command, except for mouse menu events.\n\
 Mouse menus give back keys that don't look like mouse events;\n\
 this variable holds the actual mouse event that led to the menu,\n\
 so that you can determine whether the command was run by mouse or not.");
 
   DEFVAR_LISP ("last-input-char", &last_input_char,
-    "Last terminal input key.");
+    "Last input event.");
 
-  DEFVAR_LISP ("unread-command-event", &unread_command_event,
-    "Object to be read as next input from input stream, or nil if none.");
+  DEFVAR_LISP ("last-input-event", &last_input_char,
+    "Last input event.");
+
+  DEFVAR_LISP ("unread-command-events", &unread_command_events,
+    "List of objects to be read as next command input events.");
+
+  DEFVAR_INT ("unread-command-char", &unread_command_char,
+    "If not -1, an object to be read as next command input event.");
 
   DEFVAR_LISP ("meta-prefix-char", &meta_prefix_char,
     "Meta-prefix character code.  Meta-foo as command input\n\
@@ -4341,6 +4865,38 @@ Otherwise, menu prompting uses the echo area.");
     "Character to see next line of menu prompt.\n\
 Type this character while in a menu prompt to rotate around the lines of it.");
   XSET (menu_prompt_more_char, Lisp_Int, ' ');
+
+  DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
+    "A mask of additional modifier keys to use with every keyboard character.\n\
+Emacs applies the modifiers of the character stored here to each keyboard\n\
+character it reads.  For example, after evaluating the expression\n\
+    (setq extra-keyboard-modifiers ?\C-x)\n\
+all input characters will have the control modifier applied to them.\n\
+\n\
+Note that the character ?\C-@, equivalent to the integer zero, does\n\
+not count as a control character; rather, it counts as a character\n\
+with no modifiers; thus, setting extra_keyboard_modifiers to zero\n\
+cancels any modification.");
+  extra_keyboard_modifiers = 0;
+
+  DEFVAR_LISP ("deactivate-mark", &Vdeactivate_mark,
+    "If an editing command sets this to t, deactivate the mark afterward.\n\
+The command loop sets this to nil before each command,\n\
+and tests the value when the command returns.\n\
+Buffer modification stores t in this variable.");
+  Vdeactivate_mark = Qnil;
+
+  DEFVAR_LISP ("pre-command-hook", &Vpre_command_hook,
+    "Normal hook run before each command is executed.");
+  Vpre_command_hook = Qnil;
+
+  DEFVAR_LISP ("post-command-hook", &Vpost_command_hook,
+    "Normal hook run before each command is executed.");
+  Vpost_command_hook = Qnil;
+
+  DEFVAR_LISP ("lucid-menu-bar-dirty-flag", &Vlucid_menu_bar_dirty_flag,
+    "t means menu bar, specified Lucid style, needs to be recomputed.");
+  Vlucid_menu_bar_dirty_flag = Qnil;
 }
 
 keys_of_keyboard ()