]> code.delx.au - gnu-emacs/blobdiff - src/keyboard.c
* make-dist: Distribute some VMS files we got from Richard Levitte.
[gnu-emacs] / src / keyboard.c
index 30b78f8e0b1c82f68cd01437c19d0ed65e709596..98c9250fb6d829256d57aec4e36be2f2b4b51443 100644 (file)
@@ -53,11 +53,11 @@ extern int errno;
 /* Variables for blockinput.h: */
 
 /* Non-zero if interrupt input is blocked right now.  */
-extern int interrupt_input_blocked;
+int interrupt_input_blocked;
 
 /* Nonzero means an input interrupt has arrived
    during the current critical section.  */
-extern int interrupt_input_pending;
+int interrupt_input_pending;
 
 
 #ifdef HAVE_X_WINDOWS
@@ -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
@@ -354,7 +356,6 @@ int mouse_moved;
 /* Symbols to head events.  */
 Lisp_Object Qmouse_movement;
 Lisp_Object Qscroll_bar_movement;
-
 Lisp_Object Qswitch_frame;
 
 /* Symbols to denote kinds of events.  */
@@ -366,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
@@ -521,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.  */
@@ -937,12 +943,22 @@ 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)
        {
          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)))
@@ -954,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);
@@ -1284,7 +1301,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;
@@ -1435,11 +1456,38 @@ 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))
+       {
+         /* 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:
 
@@ -1778,16 +1826,28 @@ kbd_buffer_get_event ()
         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
@@ -1804,11 +1864,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
 
@@ -1849,11 +1908,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
 
@@ -2032,12 +2090,20 @@ 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
+       /* Turn ASCII characters into control characters
+          when proper.  */
+       if (event->modifiers & ctrl_modifier)
+         {
+           if (c >= 0100 && c < 0140)
+             c &= ~040;
+           /* Include the bits for control and shift
+              only if the basic ASCII code can't indicate them.  */
+           c |= ctrl_modifier;
+         }
+       /* Set the shift modifier for a control char
+          made from a shifted letter.  But only for letters!  */
+       if (XFASTINT (event->code) >= 'A' - 0100
+           && XFASTINT (event->code) <= 'Z' - 0100
            && (event->modifiers & shift_modifier))
          c |= shift_modifier;
        c |= (event->modifiers
@@ -2073,7 +2139,7 @@ make_lispy_event (event)
        if (event->kind == mouse_click)
          {
            int part;
-           struct frame *f = XFRAME (event->frame_or_window);
+           FRAME_PTR f = XFRAME (event->frame_or_window);
            Lisp_Object window
              = window_from_coordinates (f, XINT (event->x), XINT (event->y),
                                         &part);
@@ -2444,6 +2510,7 @@ static char *modifier_names[] =
   "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;
 
@@ -2456,14 +2523,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;
 }
@@ -2529,10 +2592,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 (index & ~((1<<VALBITS) - 1))
-    abort ();
   XFASTINT (index) = (modifiers & ~click_modifier);
   entry = Fassq (index, cache);
 
@@ -2550,8 +2614,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)));
@@ -2564,8 +2626,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);
@@ -2800,9 +2862,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
@@ -2875,6 +2938,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.
@@ -3105,6 +3184,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)
@@ -3431,14 +3519,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)
@@ -3609,6 +3689,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;
                    }
@@ -3676,8 +3768,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)
@@ -4089,7 +4180,8 @@ 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_event_array (this_command_key_count,
@@ -4154,7 +4246,7 @@ 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 parent, after suspension.\n\
 \n\
-Before suspending, call the functions in `suspend-hooks' with no args.\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\
@@ -4395,8 +4487,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;
@@ -4423,7 +4516,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);
@@ -4431,6 +4529,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 ()
 {
@@ -4542,6 +4665,9 @@ syms_of_keyboard ()
   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");
@@ -4634,6 +4760,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,
@@ -4755,10 +4882,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,