Merge from trunk
[bpt/emacs.git] / src / keyboard.c
index e29c7e9..d307250 100644 (file)
@@ -254,7 +254,6 @@ Lisp_Object Qecho_area_clear_hook;
 /* Hooks to run before and after each command.  */
 Lisp_Object Qpre_command_hook;
 Lisp_Object Qpost_command_hook;
-Lisp_Object Qcommand_hook_internal;
 
 Lisp_Object Qdeferred_action_function;
 
@@ -1135,7 +1134,7 @@ command_loop_2 (Lisp_Object ignore)
 static Lisp_Object
 top_level_2 (void)
 {
-  return Feval (Vtop_level);
+  return Feval (Vtop_level, Qnil);
 }
 
 Lisp_Object
@@ -1270,7 +1269,7 @@ some_mouse_moved (void)
 /* This is the actual command reading loop,
    sans error-handling encapsulation.  */
 
-static int read_key_sequence (Lisp_Object *, int, Lisp_Object,
+static int read_key_sequence (Lisp_Object *, size_t, Lisp_Object,
                               int, int, int);
 void safe_run_hooks (Lisp_Object);
 static void adjust_point_for_property (EMACS_INT, int);
@@ -1815,20 +1814,63 @@ adjust_point_for_property (EMACS_INT last_pt, int modified)
 static Lisp_Object
 safe_run_hooks_1 (void)
 {
-  return Frun_hooks (1, &Vinhibit_quit);
+  eassert (CONSP (Vinhibit_quit));
+  return call0 (XCDR (Vinhibit_quit));
 }
 
-/* Subroutine for safe_run_hooks: handle an error by clearing out the hook.  */
+/* Subroutine for safe_run_hooks: handle an error by clearing out the function
+   from the hook.  */
 
 static Lisp_Object
-safe_run_hooks_error (Lisp_Object data)
+safe_run_hooks_error (Lisp_Object error_data)
+{
+  Lisp_Object hook
+    = CONSP (Vinhibit_quit) ? XCAR (Vinhibit_quit) : Vinhibit_quit;
+  Lisp_Object fun = CONSP (Vinhibit_quit) ? XCDR (Vinhibit_quit) : Qnil;
+  Lisp_Object args[4];
+  args[0] = build_string ("Error in %s (%s): %s");
+  args[1] = hook;
+  args[2] = fun;
+  args[3] = error_data;
+  Fmessage (4, args);
+  if (SYMBOLP (hook))
+    {
+      Lisp_Object val;
+      int found = 0;
+      Lisp_Object newval = Qnil;
+      for (val = find_symbol_value (hook); CONSP (val); val = XCDR (val))
+       if (EQ (fun, XCAR (val)))
+         found = 1;
+       else
+         newval = Fcons (XCAR (val), newval);
+      if (found)
+       return Fset (hook, Fnreverse (newval));
+      /* Not found in the local part of the hook.  Let's look at the global
+        part.  */
+      newval = Qnil;
+      for (val = (NILP (Fdefault_boundp (hook)) ? Qnil
+                 : Fdefault_value (hook));
+          CONSP (val); val = XCDR (val))
+       if (EQ (fun, XCAR (val)))
+         found = 1;
+       else
+         newval = Fcons (XCAR (val), newval);
+      if (found)
+       return Fset_default (hook, Fnreverse (newval));
+    }
+  return Qnil;
+}
+
+static Lisp_Object
+safe_run_hook_funcall (size_t nargs, Lisp_Object *args)
 {
-  Lisp_Object args[3];
-  args[0] = build_string ("Error in %s: %s");
-  args[1] = Vinhibit_quit;
-  args[2] = data;
-  Fmessage (3, args);
-  return Fset (Vinhibit_quit, Qnil);
+  eassert (nargs == 1);
+  if (CONSP (Vinhibit_quit))
+    XSETCDR (Vinhibit_quit, args[0]);
+  else
+    Vinhibit_quit = Fcons (Vinhibit_quit, args[0]);
+
+  return internal_condition_case (safe_run_hooks_1, Qt, safe_run_hooks_error);
 }
 
 /* If we get an error while running the hook, cause the hook variable
@@ -1838,10 +1880,13 @@ safe_run_hooks_error (Lisp_Object data)
 void
 safe_run_hooks (Lisp_Object hook)
 {
+  /* FIXME: our `internal_condition_case' does not provide any way to pass data
+     to its body or to its handlers other than via globals such as
+     dynamically-bound variables ;-)  */
   int count = SPECPDL_INDEX ();
   specbind (Qinhibit_quit, hook);
 
-  internal_condition_case (safe_run_hooks_1, Qt, safe_run_hooks_error);
+  run_hook_with_args (1, &hook, safe_run_hook_funcall);
 
   unbind_to (count, Qnil);
 }
@@ -3050,7 +3095,7 @@ read_char (int commandflag, int nmaps, Lisp_Object *maps, Lisp_Object prev_event
                 help_form_saved_window_configs);
       record_unwind_protect (read_char_help_form_unwind, Qnil);
 
-      tem0 = Feval (Vhelp_form);
+      tem0 = Feval (Vhelp_form, Qnil);
       if (STRINGP (tem0))
        internal_with_output_to_temp_buffer ("*Help*", print_help, tem0);
 
@@ -7526,6 +7571,12 @@ menu_item_eval_property_1 (Lisp_Object arg)
   return Qnil;
 }
 
+static Lisp_Object
+eval_dyn (Lisp_Object form)
+{
+  return Feval (form, Qnil);
+}
+
 /* Evaluate an expression and return the result (or nil if something
    went wrong).  Used to evaluate dynamic parts of menu items.  */
 Lisp_Object
@@ -7534,7 +7585,7 @@ menu_item_eval_property (Lisp_Object sexpr)
   int count = SPECPDL_INDEX ();
   Lisp_Object val;
   specbind (Qinhibit_redisplay, Qt);
-  val = internal_condition_case_1 (Feval, sexpr, Qerror,
+  val = internal_condition_case_1 (eval_dyn, sexpr, Qerror,
                                   menu_item_eval_property_1);
   return unbind_to (count, val);
 }
@@ -8748,7 +8799,7 @@ access_keymap_keyremap (Lisp_Object map, Lisp_Object key, Lisp_Object prompt,
    The return value is non-zero if the remapping actually took place.  */
 
 static int
-keyremap_step (Lisp_Object *keybuf, int bufsize, volatile keyremap *fkey,
+keyremap_step (Lisp_Object *keybuf, size_t bufsize, volatile keyremap *fkey,
               int input, int doit, int *diff, Lisp_Object prompt)
 {
   Lisp_Object next, key;
@@ -8841,7 +8892,7 @@ keyremap_step (Lisp_Object *keybuf, int bufsize, volatile keyremap *fkey,
    from the selected window's buffer.  */
 
 static int
-read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt,
+read_key_sequence (Lisp_Object *keybuf, size_t bufsize, Lisp_Object prompt,
                   int dont_downcase_last, int can_return_switch_frame,
                   int fix_current_buffer)
 {
@@ -9359,80 +9410,84 @@ read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt,
                    last_real_key_start = t - 1;
                }
 
-             /* Key sequences beginning with mouse clicks are
-                read using the keymaps in the buffer clicked on,
-                not the current buffer.  If we're at the
-                beginning of a key sequence, switch buffers.  */
-             if (last_real_key_start == 0
-                 && WINDOWP (window)
-                 && BUFFERP (XWINDOW (window)->buffer)
-                 && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
+             if (last_real_key_start == 0)
                {
-                 XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
-                 keybuf[t] = key;
-                 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 accommodate 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 ());
-
-                 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
-                   Fkill_emacs (Qnil);
-                 set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
-                 orig_local_map = get_local_map (PT, current_buffer,
-                                                 Qlocal_map);
-                 orig_keymap = get_local_map (PT, current_buffer, Qkeymap);
-                 goto replay_sequence;
-               }
+                 /* Key sequences beginning with mouse clicks are
+                    read using the keymaps in the buffer clicked on,
+                    not the current buffer.  If we're at the
+                    beginning of a key sequence, switch buffers.  */
+                 if (WINDOWP (window)
+                     && BUFFERP (XWINDOW (window)->buffer)
+                     && XBUFFER (XWINDOW (window)->buffer) != current_buffer)
+                   {
+                     XVECTOR (raw_keybuf)->contents[raw_keybuf_count++] = key;
+                     keybuf[t] = key;
+                     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 accommodate 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 ());
+
+                     if (! FRAME_LIVE_P (XFRAME (selected_frame)))
+                       Fkill_emacs (Qnil);
+                     set_buffer_internal (XBUFFER (XWINDOW (window)->buffer));
+                     orig_local_map = get_local_map (PT, current_buffer,
+                                                     Qlocal_map);
+                     orig_keymap = get_local_map (PT, current_buffer,
+                                                  Qkeymap);
+                     goto replay_sequence;
+                   }
 
-             /* For a mouse click, get the local text-property keymap
-                of the place clicked on, rather than point.  */
-             if (last_real_key_start == 0
-                 && CONSP (XCDR (key))
-                 && ! localized_local_map)
-               {
-                 Lisp_Object map_here, start, pos;
+                 /* For a mouse click, get the local text-property keymap
+                    of the place clicked on, rather than point.  */
+                 if (CONSP (XCDR (key))
+                     && ! localized_local_map)
+                   {
+                     Lisp_Object map_here, start, pos;
 
-                 localized_local_map = 1;
-                 start = EVENT_START (key);
+                     localized_local_map = 1;
+                     start = EVENT_START (key);
 
-                 if (CONSP (start) && POSN_INBUFFER_P (start))
-                   {
-                     pos = POSN_BUFFER_POSN (start);
-                     if (INTEGERP (pos)
-                         && XINT (pos) >= BEGV
-                         && XINT (pos) <= ZV)
+                     if (CONSP (start) && POSN_INBUFFER_P (start))
                        {
-                         map_here = get_local_map (XINT (pos),
-                                                   current_buffer, Qlocal_map);
-                         if (!EQ (map_here, orig_local_map))
+                         pos = POSN_BUFFER_POSN (start);
+                         if (INTEGERP (pos)
+                             && XINT (pos) >= BEGV
+                             && XINT (pos) <= ZV)
                            {
-                             orig_local_map = map_here;
-                             ++localized_local_map;
-                           }
+                             map_here = get_local_map (XINT (pos),
+                                                       current_buffer,
+                                                       Qlocal_map);
+                             if (!EQ (map_here, orig_local_map))
+                               {
+                                 orig_local_map = map_here;
+                                 ++localized_local_map;
+                               }
 
-                         map_here = get_local_map (XINT (pos),
-                                                    current_buffer, Qkeymap);
-                         if (!EQ (map_here, orig_keymap))
-                           {
-                             orig_keymap = map_here;
-                             ++localized_local_map;
-                           }
+                             map_here = get_local_map (XINT (pos),
+                                                       current_buffer,
+                                                       Qkeymap);
+                             if (!EQ (map_here, orig_keymap))
+                               {
+                                 orig_keymap = map_here;
+                                 ++localized_local_map;
+                               }
 
-                         if (localized_local_map > 1)
-                           {
-                             keybuf[t] = key;
-                             mock_input = t + 1;
+                             if (localized_local_map > 1)
+                               {
+                                 keybuf[t] = key;
+                                 mock_input = t + 1;
 
-                             goto replay_sequence;
+                                 goto replay_sequence;
+                               }
                            }
                        }
                    }
@@ -10291,9 +10346,9 @@ give to the command you invoke, if it asks for an argument.  */)
          sprintf (newmessage, "You can run the command `%s' with %s",
                   SDATA (SYMBOL_NAME (function)),
                   SDATA (binding));
-         message2_nolog (newmessage,
-                         strlen (newmessage),
-                         STRING_MULTIBYTE (binding));
+         message2 (newmessage,
+                   strlen (newmessage),
+                   STRING_MULTIBYTE (binding));
          if (NUMBERP (Vsuggest_key_bindings))
            waited = sit_for (Vsuggest_key_bindings, 0, 2);
          else
@@ -11438,9 +11493,6 @@ syms_of_keyboard (void)
   Qdeferred_action_function = intern_c_string ("deferred-action-function");
   staticpro (&Qdeferred_action_function);
 
-  Qcommand_hook_internal = intern_c_string ("command-hook-internal");
-  staticpro (&Qcommand_hook_internal);
-
   Qfunction_key = intern_c_string ("function-key");
   staticpro (&Qfunction_key);
   Qmouse_click = intern_c_string ("mouse-click");
@@ -11908,22 +11960,18 @@ Buffer modification stores t in this variable.  */);
   Qdeactivate_mark = intern_c_string ("deactivate-mark");
   staticpro (&Qdeactivate_mark);
 
-  DEFVAR_LISP ("command-hook-internal", Vcommand_hook_internal,
-              doc: /* Temporary storage of `pre-command-hook' or `post-command-hook'.  */);
-  Vcommand_hook_internal = Qnil;
-
   DEFVAR_LISP ("pre-command-hook", Vpre_command_hook,
               doc: /* Normal hook run before each command is executed.
 If an unhandled error happens in running this hook,
-the hook value is set to nil, since otherwise the error
-might happen repeatedly and make Emacs nonfunctional.  */);
+the function in which the error occurred is unconditionally removed, since
+otherwise the error might happen repeatedly and make Emacs nonfunctional.  */);
   Vpre_command_hook = Qnil;
 
   DEFVAR_LISP ("post-command-hook", Vpost_command_hook,
               doc: /* Normal hook run after each command is executed.
 If an unhandled error happens in running this hook,
-the hook value is set to nil, since otherwise the error
-might happen repeatedly and make Emacs nonfunctional.  */);
+the function in which the error occurred is unconditionally removed, since
+otherwise the error might happen repeatedly and make Emacs nonfunctional.  */);
   Vpost_command_hook = Qnil;
 
 #if 0