]> code.delx.au - gnu-emacs/blobdiff - src/keyboard.c
(lispy_function_keys): Add kp-numlock. Fix kp-backspace.
[gnu-emacs] / src / keyboard.c
index d5e15152526a171a7b42c1d8dfa7b715defcd0c5..6b267bd8e087b362c33fc15593239c301ef89c54 100644 (file)
@@ -271,7 +271,9 @@ 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;
@@ -330,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
@@ -941,6 +943,7 @@ command_loop_1 ()
       if (! NILP (Vlucid_menu_bar_dirty_flag))
        call0 (Qrecompute_lucid_menubar);
 
+#if 0 /* This is done in xdisp.c now.  */
 #ifdef MULTI_FRAME
       for (tem = Vframe_list; CONSP (tem); tem = XCONS (tem)->cdr)
        {
@@ -967,6 +970,7 @@ command_loop_1 ()
            }
        }
 #endif /* MULTI_FRAME */
+#endif /* 0 */
 
       /* Read next key sequence; i gets its length.  */
       i = read_key_sequence (keybuf, (sizeof keybuf / sizeof (keybuf[0])), 0);
@@ -1010,7 +1014,7 @@ command_loop_1 ()
       if (!NILP (Vpre_command_hook))
        call1 (Vrun_hooks, Qpre_command_hook);
 
-      if (NILP (cmd))
+      if (NILP (this_command))
        {
          /* nil means key is undefined.  */
          bitch_at_user ();
@@ -1025,7 +1029,7 @@ command_loop_1 ()
            {
              /* Recognize some common commands in common situations and
                 do them directly.  */
-             if (EQ (cmd, Qforward_char) && point < ZV)
+             if (EQ (this_command, Qforward_char) && point < ZV)
                {
                   struct Lisp_Vector *dp
                    = window_display_table (XWINDOW (selected_window));
@@ -1046,7 +1050,7 @@ command_loop_1 ()
                    no_redisplay = direct_output_forward_char (1);
                  goto directly_done;
                }
-             else if (EQ (cmd, Qbackward_char) && point > BEGV)
+             else if (EQ (this_command, Qbackward_char) && point > BEGV)
                {
                   struct Lisp_Vector *dp
                    = window_display_table (XWINDOW (selected_window));
@@ -1067,7 +1071,7 @@ command_loop_1 ()
                    no_redisplay = direct_output_forward_char (-1);
                  goto directly_done;
                }
-             else if (EQ (cmd, Qself_insert_command)
+             else if (EQ (this_command, Qself_insert_command)
                       /* Try this optimization only on ascii keystrokes.  */
                       && XTYPE (last_command_char) == Lisp_Int)
                {
@@ -1130,7 +1134,7 @@ command_loop_1 ()
          nonundocount = 0;
          if (NILP (Vprefix_arg))
            Fundo_boundary ();
-         Fcommand_execute (cmd, Qnil);
+         Fcommand_execute (this_command, Qnil);
 
        }
     directly_done: ;
@@ -1226,6 +1230,45 @@ stop_polling ()
     }
 #endif
 }
+\f
+/* Applying the control modifier to CHARACTER.  */
+int
+make_ctrl_char (c)
+     int c;
+{
+  /* Save the upper bits here.  */
+  int upper = c & ~0177;
+
+  c &= 0177;
+
+  /* Everything in the columns containing the upper-case letters
+     denotes a control character.  */
+  if (c >= 0100 && c < 0140)
+    {
+      int oc = c;
+      c &= ~0140;
+      /* Set the shift modifier for a control char
+        made from a shifted letter.  But only for letters!  */
+      if (oc >= 'A' && oc <= 'Z')
+       c |= shift_modifier;
+    }
+
+  /* The lower-case letters denote control characters too.  */
+  else if (c >= 'a' && c <= 'z')
+    c &= ~0140;
+
+  /* Include the bits for control and shift
+     only if the basic ASCII code can't indicate them.  */
+  else if (c >= ' ')
+    c |= ctrl_modifier;
+
+  /* Replace the high bits.  */
+  c |= (upper & ~ctrl_modifier);
+
+  return c;
+}
+
+
 \f
 /* Input of single characters from keyboard */
 
@@ -1297,7 +1340,11 @@ read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
       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;
@@ -1448,11 +1495,19 @@ read_char (commandflag, nmaps, maps, prev_event, used_mouse_menu)
   if (noninteractive && XTYPE (c) == Lisp_Int && XINT (c) < 0)
     Fkill_emacs (make_number (1));
 
-  /* Test for ControlMask and Mod1Mask.  */
-  if (extra_keyboard_modifiers & 4)
-    c &= ~0140;
-  if (extra_keyboard_modifiers & 8)
-    c |= 0200;
+  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))
+       XSETINT (c, make_ctrl_char (XINT (c)));
+
+      /* 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:
 
@@ -1657,6 +1712,9 @@ kbd_buffer_store_event (event)
     {
       register int c = XFASTINT (event->code) & 0377;
 
+      if (event->modifiers & ctrl_modifier)
+       c = make_ctrl_char (c);
+
       if (c == quit_char)
        {
          extern SIGTYPE interrupt_signal ();
@@ -1829,11 +1887,10 @@ kbd_buffer_get_event ()
        if (! NILP (focus))
          frame = focus;
 
-       if (! EQ (frame, internal_last_event_frame))
-         {
-           internal_last_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
 
@@ -1874,11 +1931,10 @@ kbd_buffer_get_event ()
          if (NILP (frame))
            XSET (frame, Lisp_Frame, f);
 
-         if (! EQ (frame, internal_last_event_frame))
-           {
-             XSET (internal_last_event_frame, Lisp_Frame, frame);
-             obj = make_lispy_switch_frame (internal_last_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
 
@@ -1970,9 +2026,9 @@ static char *lispy_function_keys[] =
     "insertchar",
     "deletechar",
     "backtab",
-    "kp_backtab",              /* 0x1000ff75 */
+    "kp-backtab",              /* 0x1000ff75 */
     0,                         /* 0xff76 */
-    0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xff7f */
+    0, 0, 0, 0, 0, 0, 0, 0, "kp-numlock",      /* 0xff7f */
     "kp-space",                        /* 0xff80 */    /* IsKeypadKey */
     0, 0, 0, 0, 0, 0, 0, 0,
     "kp-tab",                  /* 0xff89 */
@@ -2057,14 +2113,14 @@ make_lispy_event (event)
     case ascii_keystroke:
       {
        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;
+       /* Turn ASCII characters into control characters
+          when proper.  */
+       if (event->modifiers & ctrl_modifier)
+         c = make_ctrl_char (c);
+
+       /* Add in the other modifier bits.  We took care of ctrl_modifier
+          just above, and the shift key was taken care of by the X code,
+          and applied to control characters by make_ctrl_char.  */
        c |= (event->modifiers
              & (meta_modifier | alt_modifier
                 | hyper_modifier | super_modifier));
@@ -2298,6 +2354,8 @@ make_lispy_movement (frame, bar_window, part, x, y, time)
        {
          window = Qnil;
          posn = Qnil;
+         XFASTINT (x) = 0;
+         XFASTINT (y) = 0;
        }
 
       return Fcons (Qmouse_movement,
@@ -2465,10 +2523,11 @@ apply_modifiers_uncached (modifiers, base, base_len)
 
 static char *modifier_names[] =
 {
-  "up", 0, 0, 0, 0, 0, 0, "down",
-  "drag", "click", 0, 0, 0, 0, 0, 0,
+  "up", "down", "drag", "click", 0, 0, 0, 0,
+  0, 0, 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;
 
@@ -2481,14 +2540,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;
 }
@@ -2554,10 +2609,11 @@ 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);
-  if (modifiers & ~((1<<VALBITS) - 1))
-    abort ();
   XFASTINT (index) = (modifiers & ~click_modifier);
   entry = Fassq (index, cache);
 
@@ -2575,8 +2631,6 @@ apply_modifiers (modifiers, base)
       Fput (base, Qmodifier_cache, Fcons (entry, cache));
 
       /* We have the parsing info now for free, so add it to the caches.  */
-      if (modifiers & ~((1<<VALBITS) - 1))
-       abort ();
       XFASTINT (index) = modifiers;
       Fput (new_symbol, Qevent_symbol_element_mask,
            Fcons (base, Fcons (index, Qnil)));
@@ -2589,8 +2643,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);
@@ -2685,7 +2739,14 @@ modify_event_symbol (symbol_num, modifiers, symbol_kind, name_table,
   if (NILP (*slot))
     {
       /* No; let's create it.  */
-      *slot = intern (name_table[symbol_num]);
+      if (name_table[symbol_num])
+       *slot = intern (name_table[symbol_num]);
+      else
+       {
+         char buf[20];
+         sprintf (buf, "key-%d", symbol_num);
+         *slot = intern (buf);
+       }
 
       /* Fill in the cache entries for this symbol; this also  
         builds the Qevent_symbol_elements property, which the user
@@ -2825,9 +2886,10 @@ read_avail_input (expected)
        {
          buf[i].kind = ascii_keystroke;
          buf[i].modifiers = 0;
-         if (meta_key && (cbuf[i] & 0x80))
+         if (meta_key == 1 && (cbuf[i] & 0x80))
            buf[i].modifiers = meta_modifier;
-         cbuf[i] &= ~0x80;
+         if (meta_key != 2)
+           cbuf[i] &= ~0x80;
            
          XSET (buf[i].code,            Lisp_Int,   cbuf[i]);
 #ifdef MULTI_FRAME
@@ -2900,6 +2962,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.
@@ -3130,6 +3208,15 @@ 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)
@@ -3326,7 +3413,9 @@ 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) & 0x3ffff)
+      && ((((XINT (key) & 0x3ffff)
+           < XSTRING (current_buffer->downcase_table)->size)
+          && UPPERCASEP (XINT (key) & 0x3ffff))
          || (XINT (key) & shift_modifier)))
     {
       if (XINT (key) & shift_modifier)
@@ -3456,14 +3545,6 @@ read_key_sequence (keybuf, bufsize, prompt)
   if (NILP (Fkeymapp (Vfunction_key_map)))
     fkey_start = fkey_end = bufsize + 1;
 
-  /* We need to save the current buffer in case we switch buffers to
-     find the right binding for a mouse click.  Note that we can't use
-     save_excursion_{save,restore} here, because they save point as
-     well as the current buffer; we don't want to save point, because
-     redisplay may change it, to accomodate a Fset_window_start or
-     something.  */
-  record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
-                        
   last_nonmenu_event = Qnil;
 
   if (INTERACTIVE)
@@ -3634,6 +3715,18 @@ read_key_sequence (keybuf, bufsize, prompt)
                          mock_input = t + 1;
                        }
 
+                     /* Arrange to go back to the original buffer once we're
+                        done reading the key sequence.  Note that we can't
+                        use save_excursion_{save,restore} here, because they
+                        save point as well as the current buffer; we don't
+                        want to save point, because redisplay may change it,
+                        to accomodate a Fset_window_start or something.  We
+                        don't want to do this at the top of the function,
+                        because we may get input from a subprocess which
+                        wants to change the selected window and stuff (say,
+                        emacsclient).  */
+                     record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
+                        
                      set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
                      goto replay_sequence;
                    }
@@ -3701,8 +3794,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)
@@ -4421,9 +4513,11 @@ 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\
-Optional fourth arg QUIT if non-nil specifies character to use for quitting.")
+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.\n\
+See also `current-input-mode'.")
   (interrupt, flow, meta, quit)
      Lisp_Object interrupt, flow, meta, quit;
 {
@@ -4449,7 +4543,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);
@@ -4457,6 +4556,33 @@ 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 t if accepting 8-bit input with 8th bit as Meta flag.\n\
+    META nil means ignoring the top bit, on the assumption it is parity.\n\
+    META is neither t nor nil if accepting 8-bit input and using\n\
+    all 8 bits as the character code.\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 == 2 ? make_number (0) : meta_key == 1 ? Qt : Qnil;
+  XSETINT (val[3], quit_char);
+
+  return Flist (val, sizeof (val) / sizeof (val[0]));
+}
+
 \f
 init_keyboard ()
 {
@@ -4663,6 +4789,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,
@@ -4784,10 +4911,15 @@ Type this character while in a menu prompt to rotate around the lines of it.");
 
   DEFVAR_INT ("extra-keyboard-modifiers", &extra_keyboard_modifiers,
     "A mask of additional modifier keys to use with every keyboard character.\n\
-These bits follow the convention for X windows,\n\
-but the control and meta bits work even when you are not using X:\n\
-  1 -- shift bit      2 -- lock bit\n\
-  4 -- control bit    8 -- meta bit.");
+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,