* src/eval.c: Add `inhibit-debugger'.
[bpt/emacs.git] / src / keyboard.c
index 0d686e2..8091258 100644 (file)
@@ -18,7 +18,9 @@ You should have received a copy of the GNU General Public License
 along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <config.h>
-#include <signal.h>
+
+#define KEYBOARD_INLINE EXTERN_INLINE
+
 #include <stdio.h>
 #include <setjmp.h>
 #include "lisp.h"
@@ -60,20 +62,11 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <unistd.h>
 #include <fcntl.h>
 
-/* This is to get the definitions of the XK_ symbols.  */
-#ifdef HAVE_X_WINDOWS
-#include "xterm.h"
-#endif
-
-#ifdef HAVE_NTGUI
-#include "w32term.h"
-#endif /* HAVE_NTGUI */
-
-#ifdef HAVE_NS
-#include "nsterm.h"
-#endif
+#ifdef HAVE_WINDOW_SYSTEM
+#include TERM_HEADER
+#endif /* HAVE_WINDOW_SYSTEM */
 
-/* Variables for blockinput.h:  */
+/* Variables for blockinput.h: */
 
 /* Non-zero if interrupt input is blocked right now.  */
 volatile int interrupt_input_blocked;
@@ -392,7 +385,7 @@ int interrupt_input;
 /* Nonzero while interrupts are temporarily deferred during redisplay.  */
 int interrupts_deferred;
 
-/* Allow m- file to inhibit use of FIONREAD.  */
+/* Allow configure to inhibit use of FIONREAD.  */
 #ifdef BROKEN_FIONREAD
 #undef FIONREAD
 #endif
@@ -455,9 +448,8 @@ static void restore_getcjmp (jmp_buf);
 static Lisp_Object apply_modifiers (int, Lisp_Object);
 static void clear_event (struct input_event *);
 static Lisp_Object restore_kboard_configuration (Lisp_Object);
-static void interrupt_signal (int signalnum);
 #ifdef SIGIO
-static void input_available_signal (int signo);
+static void deliver_input_available_signal (int signo);
 #endif
 static void handle_interrupt (void);
 static _Noreturn void quit_throw_to_read_char (int);
@@ -465,10 +457,57 @@ static void process_special_events (void);
 static void timer_start_idle (void);
 static void timer_stop_idle (void);
 static void timer_resume_idle (void);
-static void handle_user_signal (int);
+static void deliver_user_signal (int);
 static char *find_user_signal_name (int);
 static int store_user_signal_events (void);
 
+/* These setters are used only in this file, so they can be private.  */
+static inline void
+kset_echo_string (struct kboard *kb, Lisp_Object val)
+{
+  kb->INTERNAL_FIELD (echo_string) = val;
+}
+static inline void
+kset_kbd_queue (struct kboard *kb, Lisp_Object val)
+{
+  kb->INTERNAL_FIELD (kbd_queue) = val;
+}
+static inline void
+kset_keyboard_translate_table (struct kboard *kb, Lisp_Object val)
+{
+  kb->INTERNAL_FIELD (Vkeyboard_translate_table) = val;
+}
+static inline void
+kset_last_prefix_arg (struct kboard *kb, Lisp_Object val)
+{
+  kb->INTERNAL_FIELD (Vlast_prefix_arg) = val;
+}
+static inline void
+kset_last_repeatable_command (struct kboard *kb, Lisp_Object val)
+{
+  kb->INTERNAL_FIELD (Vlast_repeatable_command) = val;
+}
+static inline void
+kset_local_function_key_map (struct kboard *kb, Lisp_Object val)
+{
+  kb->INTERNAL_FIELD (Vlocal_function_key_map) = val;
+}
+static inline void
+kset_overriding_terminal_local_map (struct kboard *kb, Lisp_Object val)
+{
+  kb->INTERNAL_FIELD (Voverriding_terminal_local_map) = val;
+}
+static inline void
+kset_real_last_command (struct kboard *kb, Lisp_Object val)
+{
+  kb->INTERNAL_FIELD (Vreal_last_command) = val;
+}
+static inline void
+kset_system_key_syms (struct kboard *kb, Lisp_Object val)
+{
+  kb->INTERNAL_FIELD (system_key_syms) = val;
+}
+
 \f
 /* Add C to the echo string, if echoing is going on.
    C can be a character, which is printed prettily ("M-C-x" and all that
@@ -480,7 +519,7 @@ echo_char (Lisp_Object c)
   if (current_kboard->immediate_echo)
     {
       int size = KEY_DESCRIPTION_SIZE + 100;
-      char *buffer = (char *) alloca (size);
+      char *buffer = alloca (size);
       char *ptr = buffer;
       Lisp_Object echo_string;
 
@@ -502,7 +541,7 @@ echo_char (Lisp_Object c)
            {
              int offset = ptr - buffer;
              size = max (2 * size, size + nbytes);
-             buffer = (char *) alloca (size);
+             buffer = alloca (size);
              ptr = buffer + offset;
            }
 
@@ -520,7 +559,7 @@ echo_char (Lisp_Object c)
            {
              int offset = ptr - buffer;
              size += len;
-             buffer = (char *) alloca (size);
+             buffer = alloca (size);
              ptr = buffer + offset;
            }
 
@@ -551,8 +590,9 @@ echo_char (Lisp_Object c)
       else if (STRINGP (echo_string))
        echo_string = concat2 (echo_string, build_string (" "));
 
-      KVAR (current_kboard, echo_string)
-       = concat2 (echo_string, make_string (buffer, ptr - buffer));
+      kset_echo_string
+       (current_kboard,
+        concat2 (echo_string, make_string (buffer, ptr - buffer)));
 
       echo_now ();
     }
@@ -597,8 +637,9 @@ echo_dash (void)
 
   /* Put a dash at the end of the buffer temporarily,
      but make it go away when the next character is added.  */
-  KVAR (current_kboard, echo_string) = concat2 (KVAR (current_kboard, echo_string),
-                                        build_string ("-"));
+  kset_echo_string
+    (current_kboard,
+     concat2 (KVAR (current_kboard, echo_string), build_string ("-")));
   echo_now ();
 }
 
@@ -660,7 +701,7 @@ cancel_echoing (void)
 {
   current_kboard->immediate_echo = 0;
   current_kboard->echo_after_prompt = -1;
-  KVAR (current_kboard, echo_string) = Qnil;
+  kset_echo_string (current_kboard, Qnil);
   ok_to_echo_at_next_pause = NULL;
   echo_kboard = NULL;
   echo_message_buffer = Qnil;
@@ -684,9 +725,9 @@ static void
 echo_truncate (ptrdiff_t nchars)
 {
   if (STRINGP (KVAR (current_kboard, echo_string)))
-    KVAR (current_kboard, echo_string)
-      = Fsubstring (KVAR (current_kboard, echo_string),
-                   make_number (0), make_number (nchars));
+    kset_echo_string (current_kboard,
+                     Fsubstring (KVAR (current_kboard, echo_string),
+                                 make_number (0), make_number (nchars)));
   truncate_echo_area (nchars);
 }
 
@@ -884,7 +925,7 @@ static struct kboard_stack *kboard_stack;
 void
 push_kboard (struct kboard *k)
 {
-  struct kboard_stack *p = xmalloc (sizeof (struct kboard_stack));
+  struct kboard_stack *p = xmalloc (sizeof *p);
 
   p->next = kboard_stack;
   p->kboard = current_kboard;
@@ -980,7 +1021,7 @@ restore_kboard_configuration (Lisp_Object was_locked)
       pop_kboard ();
       /* The pop should not change the kboard.  */
       if (single_kboard && current_kboard != prev)
-        abort ();
+        emacs_abort ();
     }
   return Qnil;
 }
@@ -1016,8 +1057,8 @@ cmd_error (Lisp_Object data)
   Vstandard_input = Qt;
   Vexecuting_kbd_macro = Qnil;
   executing_kbd_macro = Qnil;
-  KVAR (current_kboard, Vprefix_arg) = Qnil;
-  KVAR (current_kboard, Vlast_prefix_arg) = Qnil;
+  kset_prefix_arg (current_kboard, Qnil);
+  kset_last_prefix_arg (current_kboard, Qnil);
   cancel_echoing ();
 
   /* Avoid unquittable loop if data contains a circular list.  */
@@ -1318,6 +1359,9 @@ cancel_hourglass_unwind (Lisp_Object arg)
 }
 #endif
 
+/* The last boundary auto-added to buffer-undo-list.  */
+Lisp_Object last_undo_boundary;
+
 /* FIXME: This is wrong rather than test window-system, we should call
    a new set-selection, which will then dispatch to x-set-selection, or
    tty-set-selection, or w32-set-selection, ...  */
@@ -1335,8 +1379,8 @@ command_loop_1 (void)
 #endif
   int already_adjusted = 0;
 
-  KVAR (current_kboard, Vprefix_arg) = Qnil;
-  KVAR (current_kboard, Vlast_prefix_arg) = Qnil;
+  kset_prefix_arg (current_kboard, Qnil);
+  kset_last_prefix_arg (current_kboard, Qnil);
   Vdeactivate_mark = Qnil;
   waiting_for_input = 0;
   cancel_echoing ();
@@ -1368,10 +1412,10 @@ command_loop_1 (void)
     }
 
   /* Do this after running Vpost_command_hook, for consistency.  */
-  KVAR (current_kboard, Vlast_command) = Vthis_command;
-  KVAR (current_kboard, Vreal_last_command) = Vreal_this_command;
+  kset_last_command (current_kboard, Vthis_command);
+  kset_real_last_command (current_kboard, Vreal_this_command);
   if (!CONSP (last_command_event))
-    KVAR (current_kboard, Vlast_repeatable_command) = Vreal_this_command;
+    kset_last_repeatable_command (current_kboard, Vreal_this_command);
 
   while (1)
     {
@@ -1379,8 +1423,7 @@ command_loop_1 (void)
        Fkill_emacs (Qnil);
 
       /* Make sure the current window's buffer is selected.  */
-      if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
-       set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
+      set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
 
       /* Display any malloc warning that just came out.  Use while because
         displaying one warning can cause another.  */
@@ -1390,6 +1433,15 @@ command_loop_1 (void)
 
       Vdeactivate_mark = Qnil;
 
+#if defined (HAVE_MOUSE) || defined (HAVE_GPM)
+
+      /* Don't ignore mouse movements for more than a single command
+        loop.  (This flag is set in xdisp.c whenever the tool bar is
+        resized, because the resize moves text up or down, and would
+        generate false mouse drag events if we don't ignore them.)  */
+      ignore_mouse_drag_p = 0;
+#endif
+
       /* If minibuffer on and echo area in use,
         wait a short time and redraw minibuffer.  */
 
@@ -1449,8 +1501,7 @@ command_loop_1 (void)
       /* A filter may have run while we were reading the input.  */
       if (! FRAME_LIVE_P (XFRAME (selected_frame)))
        Fkill_emacs (Qnil);
-      if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
-       set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
+      set_buffer_internal (XBUFFER (XWINDOW (selected_window)->buffer));
 
       ++num_input_keys;
 
@@ -1534,7 +1585,7 @@ command_loop_1 (void)
          keys = Fkey_description (keys, Qnil);
          bitch_at_user ();
          message_with_string ("%s is undefined", keys, 0);
-         KVAR (current_kboard, defining_kbd_macro) = Qnil;
+         kset_defining_kbd_macro (current_kboard, Qnil);
          update_mode_lines = 1;
          /* If this is a down-mouse event, don't reset prefix-arg;
             pass it to the command run by the up event.  */
@@ -1544,10 +1595,10 @@ command_loop_1 (void)
                = parse_modifiers (EVENT_HEAD (last_command_event));
              int modifiers = XINT (XCAR (XCDR (breakdown)));
              if (!(modifiers & down_modifier))
-               KVAR (current_kboard, Vprefix_arg) = Qnil;
+               kset_prefix_arg (current_kboard, Qnil);
            }
          else
-           KVAR (current_kboard, Vprefix_arg) = Qnil;
+           kset_prefix_arg (current_kboard, Qnil);
        }
       else
        {
@@ -1565,7 +1616,13 @@ command_loop_1 (void)
 #endif
 
             if (NILP (KVAR (current_kboard, Vprefix_arg))) /* FIXME: Why?  --Stef  */
-              Fundo_boundary ();
+              {
+               Lisp_Object undo = BVAR (current_buffer, undo_list);
+               Fundo_boundary ();
+               last_undo_boundary
+                 = (EQ (undo, BVAR (current_buffer, undo_list))
+                    ? Qnil : BVAR (current_buffer, undo_list));
+             }
             Fcommand_execute (Vthis_command, Qnil, Qnil, Qnil);
 
 #ifdef HAVE_WINDOW_SYSTEM
@@ -1578,7 +1635,7 @@ command_loop_1 (void)
             unbind_to (scount, Qnil);
 #endif
           }
-      KVAR (current_kboard, Vlast_prefix_arg) = Vcurrent_prefix_arg;
+      kset_last_prefix_arg (current_kboard, Vcurrent_prefix_arg);
 
       safe_run_hooks (Qpost_command_hook);
 
@@ -1609,11 +1666,10 @@ command_loop_1 (void)
       if (NILP (KVAR (current_kboard, Vprefix_arg))
          || CONSP (last_command_event))
        {
-         KVAR (current_kboard, Vlast_command) = Vthis_command;
-         KVAR (current_kboard, Vreal_last_command) = Vreal_this_command;
+         kset_last_command (current_kboard, Vthis_command);
+         kset_real_last_command (current_kboard, Vreal_this_command);
          if (!CONSP (last_command_event))
-           KVAR (current_kboard, Vlast_repeatable_command)
-             = Vreal_this_command;
+           kset_last_repeatable_command (current_kboard, Vreal_this_command);
          cancel_echoing ();
          this_command_key_count = 0;
          this_command_key_count_reset = 0;
@@ -2017,12 +2073,11 @@ start_polling (void)
          || EMACS_SECS (poll_timer->interval) != polling_period)
        {
          time_t period = max (1, min (polling_period, TYPE_MAXIMUM (time_t)));
-         EMACS_TIME interval;
+         EMACS_TIME interval = make_emacs_time (period, 0);
 
          if (poll_timer)
            cancel_atimer (poll_timer);
 
-         EMACS_SET_SECS_USECS (interval, period, 0);
          poll_timer = start_atimer (ATIMER_CONTINUOUS, interval,
                                     poll_for_input, NULL);
        }
@@ -2177,14 +2232,7 @@ show_help_echo (Lisp_Object help, Lisp_Object window, Lisp_Object object,
   if (!NILP (help) && !STRINGP (help))
     {
       if (FUNCTIONP (help))
-       {
-         Lisp_Object args[4];
-         args[0] = help;
-         args[1] = window;
-         args[2] = object;
-         args[3] = pos;
-         help = safe_call (4, args);
-       }
+       help = safe_call (4, help, window, object, pos);
       else
        help = safe_eval (help);
 
@@ -2555,16 +2603,16 @@ read_char (int commandflag, ptrdiff_t nmaps, Lisp_Object *maps,
            Lisp_Object last = KVAR (kb, kbd_queue);
            /* We shouldn't get here if we were in single-kboard mode!  */
            if (single_kboard)
-             abort ();
+             emacs_abort ();
            if (CONSP (last))
              {
                while (CONSP (XCDR (last)))
                  last = XCDR (last);
                if (!NILP (XCDR (last)))
-                 abort ();
+                 emacs_abort ();
              }
            if (!CONSP (last))
-             KVAR (kb, kbd_queue) = Fcons (c, Qnil);
+             kset_kbd_queue (kb, Fcons (c, Qnil));
            else
              XSETCDR (last, Fcons (c, Qnil));
            kb->kbd_queue_has_data = 1;
@@ -2697,17 +2745,13 @@ read_char (int commandflag, ptrdiff_t nmaps, Lisp_Object *maps,
              && ! CONSP (Vunread_command_events))
            {
              Fdo_auto_save (Qnil, Qnil);
-
-             /* If we have auto-saved and there is still no input
-                available, garbage collect if there has been enough
-                consing going on to make it worthwhile.  */
-             if (!detect_input_pending_run_timers (0)
-                 && consing_since_gc > gc_cons_threshold / 2)
-               Fgarbage_collect ();
-
              redisplay ();
            }
        }
+
+      /* If there is still no input available, ask for GC.  */
+      if (!detect_input_pending_run_timers (0))
+       maybe_gc ();
     }
 
   /* Notify the caller if an autosave hook, or a timer, sentinel or
@@ -2738,10 +2782,10 @@ read_char (int commandflag, ptrdiff_t nmaps, Lisp_Object *maps,
       if (current_kboard->kbd_queue_has_data)
        {
          if (!CONSP (KVAR (current_kboard, kbd_queue)))
-           abort ();
+           emacs_abort ();
          c = XCAR (KVAR (current_kboard, kbd_queue));
-         KVAR (current_kboard, kbd_queue)
-           = XCDR (KVAR (current_kboard, kbd_queue));
+         kset_kbd_queue (current_kboard,
+                         XCDR (KVAR (current_kboard, kbd_queue)));
          if (NILP (KVAR (current_kboard, kbd_queue)))
            current_kboard->kbd_queue_has_data = 0;
          input_pending = readable_events (0);
@@ -2786,13 +2830,8 @@ read_char (int commandflag, ptrdiff_t nmaps, Lisp_Object *maps,
     {
       KBOARD *kb IF_LINT (= NULL);
 
-      if (end_time)
-       {
-         EMACS_TIME now;
-         EMACS_GET_TIME (now);
-         if (EMACS_TIME_GE (now, *end_time))
-           goto exit;
-       }
+      if (end_time && EMACS_TIME_LE (*end_time, current_emacs_time ()))
+       goto exit;
 
       /* Actually read a character, waiting if necessary.  */
       save_getcjmp (save_jump);
@@ -2810,10 +2849,10 @@ read_char (int commandflag, ptrdiff_t nmaps, Lisp_Object *maps,
              while (CONSP (XCDR (last)))
                last = XCDR (last);
              if (!NILP (XCDR (last)))
-               abort ();
+               emacs_abort ();
            }
          if (!CONSP (last))
-           KVAR (kb, kbd_queue) = Fcons (c, Qnil);
+           kset_kbd_queue (kb, Fcons (c, Qnil));
          else
            XSETCDR (last, Fcons (c, Qnil));
          kb->kbd_queue_has_data = 1;
@@ -3071,7 +3110,7 @@ read_char (int commandflag, ptrdiff_t nmaps, Lisp_Object *maps,
 
       cancel_echoing ();
       ok_to_echo_at_next_pause = saved_ok_to_echo;
-      KVAR (current_kboard, echo_string) = saved_echo_string;
+      kset_echo_string (current_kboard, saved_echo_string);
       current_kboard->echo_after_prompt = saved_echo_after_prompt;
       if (saved_immediate_echo)
        echo_now ();
@@ -3519,7 +3558,7 @@ kbd_buffer_store_event_hold (register struct input_event *event,
                             struct input_event *hold_quit)
 {
   if (event->kind == NO_EVENT)
-    abort ();
+    emacs_abort ();
 
   if (hold_quit && hold_quit->kind != NO_EVENT)
     return;
@@ -3542,9 +3581,9 @@ kbd_buffer_store_event_hold (register struct input_event *event,
 
          if (single_kboard && kb != current_kboard)
            {
-             KVAR (kb, kbd_queue)
-               = Fcons (make_lispy_switch_frame (event->frame_or_window),
-                        Fcons (make_number (c), Qnil));
+             kset_kbd_queue
+               (kb, Fcons (make_lispy_switch_frame (event->frame_or_window),
+                           Fcons (make_number (c), Qnil)));
              kb->kbd_queue_has_data = 1;
              for (sp = kbd_fetch_ptr; sp != kbd_store_ptr; sp++)
                {
@@ -3563,7 +3602,7 @@ kbd_buffer_store_event_hold (register struct input_event *event,
 
          if (hold_quit)
            {
-             memcpy (hold_quit, event, sizeof (*event));
+             *hold_quit = *event;
              return;
            }
 
@@ -3640,7 +3679,7 @@ kbd_buffer_store_event_hold (register struct input_event *event,
       if (immediate_quit && NILP (Vinhibit_quit))
        {
          immediate_quit = 0;
-         sigfree ();
+         pthread_sigmask (SIG_SETMASK, &empty_mask, 0);
          QUIT;
        }
     }
@@ -3792,7 +3831,11 @@ kbd_buffer_get_event (KBOARD **kbp,
       unhold_keyboard_input ();
 #ifdef SIGIO
       if (!noninteractive)
-        signal (SIGIO, input_available_signal);
+       {
+         struct sigaction action;
+         emacs_sigaction_init (&action, deliver_input_available_signal);
+         sigaction (SIGIO, &action, 0);
+       }
 #endif /* SIGIO */
       start_polling ();
     }
@@ -3847,13 +3890,12 @@ kbd_buffer_get_event (KBOARD **kbp,
 #endif
       if (end_time)
        {
-         EMACS_TIME duration;
-         EMACS_GET_TIME (duration);
-         if (EMACS_TIME_GE (duration, *end_time))
+         EMACS_TIME now = current_emacs_time ();
+         if (EMACS_TIME_LE (*end_time, now))
            return Qnil;        /* Finished waiting.  */
          else
            {
-             EMACS_SUB_TIME (duration, *end_time, duration);
+             EMACS_TIME duration = sub_emacs_time (*end_time, now);
              wait_reading_process_output (min (EMACS_SECS (duration),
                                                WAIT_READING_MAX),
                                           EMACS_NSECS (duration),
@@ -3915,7 +3957,7 @@ kbd_buffer_get_event (KBOARD **kbp,
 #else
          /* We're getting selection request events, but we don't have
              a window system.  */
-         abort ();
+         emacs_abort ();
 #endif
        }
 
@@ -4091,7 +4133,7 @@ kbd_buffer_get_event (KBOARD **kbp,
                *used_mouse_menu = 1;
 #endif
 #ifdef HAVE_NS
-             /* certain system events are non-key events */
+             /* Certain system events are non-key events.  */
              if (used_mouse_menu
                   && event->kind == NS_NONKEY_EVENT)
                *used_mouse_menu = 1;
@@ -4119,7 +4161,7 @@ kbd_buffer_get_event (KBOARD **kbp,
         so x remains nil.  */
       x = Qnil;
 
-      /* XXX Can f or mouse_position_hook be NULL here? */
+      /* XXX Can f or mouse_position_hook be NULL here?  */
       if (f && FRAME_TERMINAL (f)->mouse_position_hook)
         (*FRAME_TERMINAL (f)->mouse_position_hook) (&f, 0, &bar_window,
                                                     &part, &x, &y, &t);
@@ -4152,7 +4194,7 @@ kbd_buffer_get_event (KBOARD **kbp,
   else
     /* We were promised by the above while loop that there was
        something for us to read!  */
-    abort ();
+    emacs_abort ();
 
   input_pending = readable_events (0);
 
@@ -4221,7 +4263,7 @@ process_special_events (void)
 #else
          /* We're getting selection request events, but we don't have
              a window system.  */
-         abort ();
+         emacs_abort ();
 #endif
        }
     }
@@ -4256,8 +4298,7 @@ timer_start_idle (void)
   if (EMACS_TIME_VALID_P (timer_idleness_start_time))
     return;
 
-  EMACS_GET_TIME (timer_idleness_start_time);
-
+  timer_idleness_start_time = current_emacs_time ();
   timer_last_idleness_start_time = timer_idleness_start_time;
 
   /* Mark all idle-time timers as once again candidates for running.  */
@@ -4278,7 +4319,7 @@ timer_start_idle (void)
 static void
 timer_stop_idle (void)
 {
-  EMACS_SET_INVALID_TIME (timer_idleness_start_time);
+  timer_idleness_start_time = invalid_emacs_time ();
 }
 
 /* Resume idle timer from last idle start time.  */
@@ -4300,9 +4341,8 @@ struct input_event last_timer_event EXTERNALLY_VISIBLE;
    ...).  Each element has the form (FUN . ARGS).  */
 Lisp_Object pending_funcalls;
 
-/* If TIMER is a valid timer, return nonzero and place its value into
-   *RESULT.  Otherwise return zero.  */
-static int
+/* Return true if TIMER is a valid timer, placing its value into *RESULT.  */
+static bool
 decode_timer (Lisp_Object timer, EMACS_TIME *result)
 {
   Lisp_Object *vector;
@@ -4339,7 +4379,7 @@ timer_check_2 (void)
   Lisp_Object timers, idle_timers, chosen_timer;
   struct gcpro gcpro1, gcpro2, gcpro3;
 
-  EMACS_SET_INVALID_TIME (nexttime);
+  nexttime = invalid_emacs_time ();
 
   /* Always consider the ordinary timers.  */
   timers = Vtimer_list;
@@ -4361,25 +4401,21 @@ timer_check_2 (void)
 
   if (CONSP (timers) || CONSP (idle_timers))
     {
-      EMACS_GET_TIME (now);
-      if (EMACS_TIME_VALID_P (timer_idleness_start_time))
-       EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time);
-      else
-       EMACS_SET_SECS_NSECS (idleness_now, 0, 0);
+      now = current_emacs_time ();
+      idleness_now = (EMACS_TIME_VALID_P (timer_idleness_start_time)
+                     ? sub_emacs_time (now, timer_idleness_start_time)
+                     : make_emacs_time (0, 0));
     }
 
   while (CONSP (timers) || CONSP (idle_timers))
     {
-      Lisp_Object *vector;
       Lisp_Object timer = Qnil, idle_timer = Qnil;
       EMACS_TIME timer_time, idle_timer_time;
       EMACS_TIME difference;
-      EMACS_TIME timer_difference, idle_timer_difference;
+      EMACS_TIME timer_difference = invalid_emacs_time ();
+      EMACS_TIME idle_timer_difference = invalid_emacs_time ();
       int ripe, timer_ripe = 0, idle_timer_ripe = 0;
 
-      EMACS_SET_INVALID_TIME (timer_difference);
-      EMACS_SET_INVALID_TIME (idle_timer_difference);
-
       /* Set TIMER and TIMER_DIFFERENCE
         based on the next ordinary timer.
         TIMER_DIFFERENCE is the distance in time from NOW to when
@@ -4395,10 +4431,9 @@ timer_check_2 (void)
            }
 
          timer_ripe = EMACS_TIME_LE (timer_time, now);
-         if (timer_ripe)
-           EMACS_SUB_TIME (timer_difference, now, timer_time);
-         else
-           EMACS_SUB_TIME (timer_difference, timer_time, now);
+         timer_difference = (timer_ripe
+                             ? sub_emacs_time (now, timer_time)
+                             : sub_emacs_time (timer_time, now));
        }
 
       /* Likewise for IDLE_TIMER and IDLE_TIMER_DIFFERENCE
@@ -4413,12 +4448,10 @@ timer_check_2 (void)
            }
 
          idle_timer_ripe = EMACS_TIME_LE (idle_timer_time, idleness_now);
-         if (idle_timer_ripe)
-           EMACS_SUB_TIME (idle_timer_difference,
-                           idleness_now, idle_timer_time);
-         else
-           EMACS_SUB_TIME (idle_timer_difference,
-                           idle_timer_time, idleness_now);
+         idle_timer_difference =
+           (idle_timer_ripe
+            ? sub_emacs_time (idleness_now, idle_timer_time)
+            : sub_emacs_time (idle_timer_time, idleness_now));
        }
 
       /* Decide which timer is the next timer,
@@ -4451,15 +4484,14 @@ timer_check_2 (void)
       /* If timer is ripe, run it if it hasn't been run.  */
       if (ripe)
        {
-         vector = XVECTOR (chosen_timer)->contents;
-         if (NILP (vector[0]))
+         if (NILP (AREF (chosen_timer, 0)))
            {
              ptrdiff_t count = SPECPDL_INDEX ();
              Lisp_Object old_deactivate_mark = Vdeactivate_mark;
 
              /* Mark the timer as triggered to prevent problems if the lisp
                 code fails to reschedule it right.  */
-             vector[0] = Qt;
+             ASET (chosen_timer, 0, Qt);
 
              specbind (Qinhibit_quit, Qt);
 
@@ -4474,8 +4506,8 @@ timer_check_2 (void)
                  return 0 to indicate that.  */
            }
 
-          EMACS_SET_SECS (nexttime, 0);
-          EMACS_SET_USECS (nexttime, 0);
+         nexttime = make_emacs_time (0, 0);
+          break;
        }
       else
        /* When we encounter a timer that is still waiting,
@@ -4527,14 +4559,8 @@ NSEC is a multiple of the system clock resolution.  */)
   (void)
 {
   if (EMACS_TIME_VALID_P (timer_idleness_start_time))
-    {
-      EMACS_TIME now, idleness_now;
-
-      EMACS_GET_TIME (now);
-      EMACS_SUB_TIME (idleness_now, now, timer_idleness_start_time);
-
-      return make_lisp_time (idleness_now);
-    }
+    return make_lisp_time (sub_emacs_time (current_emacs_time (),
+                                          timer_idleness_start_time));
 
   return Qnil;
 }
@@ -5342,7 +5368,7 @@ make_lispy_event (struct input_event *event)
 {
   int i;
 
-  switch (SWITCH_ENUM_CAST (event->kind))
+  switch (event->kind)
     {
       /* A simple keystroke.  */
     case ASCII_KEYSTROKE_EVENT:
@@ -5430,7 +5456,7 @@ make_lispy_event (struct input_event *event)
          /* We need to use an alist rather than a vector as the cache
             since we can't make a vector long enough.  */
          if (NILP (KVAR (current_kboard, system_key_syms)))
-           KVAR (current_kboard, system_key_syms) = Fcons (Qnil, Qnil);
+           kset_system_key_syms (current_kboard, Fcons (Qnil, Qnil));
          return modify_event_symbol (event->code,
                                      event->modifiers,
                                      Qfunction_key,
@@ -5574,7 +5600,7 @@ make_lispy_event (struct input_event *event)
            mouse_syms = larger_vector (mouse_syms, incr, -1);
          }
 
-       start_pos_ptr = &AREF (button_down_location, button);
+       start_pos_ptr = aref_addr (button_down_location, button);
        start_pos = *start_pos_ptr;
        *start_pos_ptr = Qnil;
 
@@ -5590,7 +5616,7 @@ make_lispy_event (struct input_event *event)
          else if (FRAMEP (event->frame_or_window))
            f = XFRAME (event->frame_or_window);
          else
-           abort ();
+           emacs_abort ();
 
          if (FRAME_WINDOW_P (f))
            fuzz = double_click_fuzz;
@@ -5697,7 +5723,7 @@ make_lispy_event (struct input_event *event)
        else
          /* Every mouse event should either have the down_modifier or
              the up_modifier set.  */
-         abort ();
+         emacs_abort ();
 
        {
          /* Get the symbol we should use for the mouse click.  */
@@ -5758,7 +5784,7 @@ make_lispy_event (struct input_event *event)
          else if (FRAMEP (event->frame_or_window))
            fr = XFRAME (event->frame_or_window);
          else
-           abort ();
+           emacs_abort ();
 
          fuzz = FRAME_WINDOW_P (fr)
            ? double_click_fuzz : double_click_fuzz / 8;
@@ -5778,7 +5804,7 @@ make_lispy_event (struct input_event *event)
          else
            /* Every wheel event should either have the down_modifier or
               the up_modifier set.  */
-           abort ();
+           emacs_abort ();
 
           if (event->kind == HORIZ_WHEEL_EVENT)
             symbol_num += 2;
@@ -5947,7 +5973,7 @@ make_lispy_event (struct input_event *event)
       {
        char *name = find_user_signal_name (event->code);
        if (!name)
-         abort ();
+         emacs_abort ();
        return intern (name);
       }
 
@@ -5982,7 +6008,7 @@ make_lispy_event (struct input_event *event)
            mouse_syms = larger_vector (mouse_syms, incr, -1);
          }
 
-       start_pos_ptr = &AREF (button_down_location, button);
+       start_pos_ptr = aref_addr (button_down_location, button);
        start_pos = *start_pos_ptr;
 
        position = make_lispy_position (f, event->x, event->y,
@@ -6028,7 +6054,7 @@ make_lispy_event (struct input_event *event)
 
       /* The 'kind' field of the event is something we don't recognize.  */
     default:
-      abort ();
+      emacs_abort ();
     }
 }
 
@@ -6134,7 +6160,7 @@ parse_modifiers_uncached (Lisp_Object symbol, ptrdiff_t *modifier_end)
 
 #define MULTI_LETTER_MOD(BIT, NAME, LEN)                       \
          if (i + LEN + 1 <= SBYTES (name)                      \
-             && ! strncmp (SSDATA (name) + i, NAME, LEN))      \
+             && ! memcmp (SDATA (name) + i, NAME, LEN))        \
            {                                                   \
              this_mod_end = i + LEN;                           \
              this_mod = BIT;                                   \
@@ -6172,13 +6198,13 @@ parse_modifiers_uncached (Lisp_Object symbol, ptrdiff_t *modifier_end)
   if (! (modifiers & (down_modifier | drag_modifier
                      | double_modifier | triple_modifier))
       && i + 7 == SBYTES (name)
-      && strncmp (SSDATA (name) + i, "mouse-", 6) == 0
+      && memcmp (SDATA (name) + i, "mouse-", 6) == 0
       && ('0' <= SREF (name, i + 6) && SREF (name, i + 6) <= '9'))
     modifiers |= click_modifier;
 
   if (! (modifiers & (double_modifier | triple_modifier))
       && i + 6 < SBYTES (name)
-      && strncmp (SSDATA (name) + i, "wheel-", 6) == 0)
+      && memcmp (SDATA (name) + i, "wheel-", 6) == 0)
     modifiers |= click_modifier;
 
   if (modifier_end)
@@ -6196,8 +6222,7 @@ apply_modifiers_uncached (int modifiers, char *base, int base_len, int base_len_
   /* Since BASE could contain nulls, we can't use intern here; we have
      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-s-down-drag-double-triple-"));
+  char new_mods[sizeof "A-C-H-M-S-s-down-drag-double-triple-"];
   int mod_len;
 
   {
@@ -6206,7 +6231,7 @@ apply_modifiers_uncached (int modifiers, char *base, int base_len, int base_len_
     /* Only the event queue may use the `up' modifier; it should always
        be turned into a click or drag event before presented to lisp code.  */
     if (modifiers & up_modifier)
-      abort ();
+      emacs_abort ();
 
     if (modifiers & alt_modifier)   { *p++ = 'A'; *p++ = '-'; }
     if (modifiers & ctrl_modifier)  { *p++ = 'C'; *p++ = '-'; }
@@ -6301,7 +6326,7 @@ parse_modifiers (Lisp_Object symbol)
                            Qnil);
 
       if (modifiers & ~INTMASK)
-       abort ();
+       emacs_abort ();
       XSETFASTINT (mask, modifiers);
       elements = Fcons (unmodified, Fcons (mask, Qnil));
 
@@ -6504,7 +6529,7 @@ modify_event_symbol (ptrdiff_t symbol_num, int modifiers, Lisp_Object symbol_kin
          ptrdiff_t len = (SBYTES (name_alist_or_stem)
                           + sizeof "-" + INT_STRLEN_BOUND (EMACS_INT));
          USE_SAFE_ALLOCA;
-         SAFE_ALLOCA (buf, char *, len);
+         buf = SAFE_ALLOCA (len);
          esprintf (buf, "%s-%"pI"d", SDATA (name_alist_or_stem),
                    XINT (symbol_int) + 1);
          value = intern (buf);
@@ -6631,7 +6656,7 @@ parse_solitary_modifier (Lisp_Object symbol)
 
 #define MULTI_LETTER_MOD(BIT, NAME, LEN)               \
       if (LEN == SBYTES (name)                         \
-         && ! strncmp (SSDATA (name), NAME, LEN))      \
+         && ! memcmp (SDATA (name), NAME, LEN))        \
        return BIT;
 
     case 'A':
@@ -6758,10 +6783,12 @@ gobble_input (int expected)
 #ifdef SIGIO
   if (interrupt_input)
     {
-      SIGMASKTYPE mask;
-      mask = sigblock (sigmask (SIGIO));
+      sigset_t blocked, procmask;
+      sigemptyset (&blocked);
+      sigaddset (&blocked, SIGIO);
+      pthread_sigmask (SIG_BLOCK, &blocked, &procmask);
       read_avail_input (expected);
-      sigsetmask (mask);
+      pthread_sigmask (SIG_SETMASK, &procmask, 0);
     }
   else
 #ifdef POLL_FOR_INPUT
@@ -6770,10 +6797,12 @@ gobble_input (int expected)
      it's always set.  */
   if (!interrupt_input && poll_suppress_count == 0)
     {
-      SIGMASKTYPE mask;
-      mask = sigblock (sigmask (SIGALRM));
+      sigset_t blocked, procmask;
+      sigemptyset (&blocked);
+      sigaddset (&blocked, SIGALRM);
+      pthread_sigmask (SIG_BLOCK, &blocked, &procmask);
       read_avail_input (expected);
-      sigsetmask (mask);
+      pthread_sigmask (SIG_SETMASK, &procmask, 0);
     }
   else
 #endif
@@ -6809,10 +6838,12 @@ record_asynch_buffer_change (void)
 #ifdef SIGIO
   if (interrupt_input)
     {
-      SIGMASKTYPE mask;
-      mask = sigblock (sigmask (SIGIO));
+      sigset_t blocked, procmask;
+      sigemptyset (&blocked);
+      sigaddset (&blocked, SIGIO);
+      pthread_sigmask (SIG_BLOCK, &blocked, &procmask);
       kbd_buffer_store_event (&event);
-      sigsetmask (mask);
+      pthread_sigmask (SIG_SETMASK, &procmask, 0);
     }
   else
 #endif
@@ -6978,7 +7009,7 @@ tty_read_avail_input (struct terminal *terminal,
 
   if (terminal->type != output_termcap
       && terminal->type != output_msdos_raw)
-    abort ();
+    emacs_abort ();
 
   /* XXX I think the following code should be moved to separate hook
      functions in system-dependent files.  */
@@ -7213,25 +7244,25 @@ process_pending_signals (void)
 /* Note SIGIO has been undef'd if FIONREAD is missing.  */
 
 static void
-input_available_signal (int signo)
+handle_input_available_signal (int sig)
 {
-  /* Must preserve main program's value of errno.  */
-  int old_errno = errno;
-  SIGNAL_THREAD_CHECK (signo);
-
 #ifdef SYNC_INPUT
   interrupt_input_pending = 1;
   pending_signals = 1;
 #endif
 
   if (input_available_clear_time)
-    EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
+    *input_available_clear_time = make_emacs_time (0, 0);
 
 #ifndef SYNC_INPUT
   handle_async_input ();
 #endif
+}
 
-  errno = old_errno;
+static void
+deliver_input_available_signal (int sig)
+{
+  handle_on_main_thread (sig, handle_input_available_signal);
 }
 #endif /* SIGIO */
 
@@ -7273,6 +7304,7 @@ static struct user_signal_info *user_signals = NULL;
 void
 add_user_signal (int sig, const char *name)
 {
+  struct sigaction action;
   struct user_signal_info *p;
 
   for (p = user_signals; p; p = p->next)
@@ -7280,25 +7312,23 @@ add_user_signal (int sig, const char *name)
       /* Already added.  */
       return;
 
-  p = xmalloc (sizeof (struct user_signal_info));
+  p = xmalloc (sizeof *p);
   p->sig = sig;
   p->name = xstrdup (name);
   p->npending = 0;
   p->next = user_signals;
   user_signals = p;
 
-  signal (sig, handle_user_signal);
+  emacs_sigaction_init (&action, deliver_user_signal);
+  sigaction (sig, &action, 0);
 }
 
 static void
 handle_user_signal (int sig)
 {
-  int old_errno = errno;
   struct user_signal_info *p;
   const char *special_event_name = NULL;
 
-  SIGNAL_THREAD_CHECK (sig);
-
   if (SYMBOLP (Vdebug_on_event))
     special_event_name = SSDATA (SYMBOL_NAME (Vdebug_on_event));
 
@@ -7328,12 +7358,16 @@ handle_user_signal (int sig)
            /* Tell wait_reading_process_output that it needs to wake
               up and look around.  */
            if (input_available_clear_time)
-             EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
+             *input_available_clear_time = make_emacs_time (0, 0);
          }
        break;
       }
+}
 
-  errno = old_errno;
+static void
+deliver_user_signal (int sig)
+{
+  handle_on_main_thread (sig, handle_user_signal);
 }
 
 static char *
@@ -7358,7 +7392,7 @@ store_user_signal_events (void)
   for (p = user_signals; p; p = p->next)
     if (p->npending > 0)
       {
-       SIGMASKTYPE mask;
+       sigset_t blocked, procmask;
 
        if (nstored == 0)
          {
@@ -7368,7 +7402,10 @@ store_user_signal_events (void)
          }
        nstored += p->npending;
 
-       mask = sigblock (sigmask (p->sig));
+       sigemptyset (&blocked);
+       sigaddset (&blocked, p->sig);
+       pthread_sigmask (SIG_BLOCK, &blocked, &procmask);
+
        do
          {
            buf.code = p->sig;
@@ -7376,7 +7413,8 @@ store_user_signal_events (void)
            p->npending--;
          }
        while (p->npending > 0);
-       sigsetmask (mask);
+
+       pthread_sigmask (SIG_SETMASK, &procmask, 0);
       }
 
   return nstored;
@@ -7419,7 +7457,7 @@ menu_separator_name_p (const char *label)
   if (!label)
     return 0;
   else if (strlen (label) > 3
-          && strncmp (label, "--", 2) == 0
+          && memcmp (label, "--", 2) == 0
           && label[2] != '-')
     {
       int i;
@@ -7489,7 +7527,7 @@ menu_bar_items (Lisp_Object old)
     if (!NILP (Voverriding_local_map_menu_flag))
       {
        /* Yes, use them (if non-nil) as well as the global map.  */
-       maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
+       maps = alloca (3 * sizeof (maps[0]));
        nmaps = 0;
        if (!NILP (KVAR (current_kboard, Voverriding_terminal_local_map)))
          maps[nmaps++] = KVAR (current_kboard, Voverriding_terminal_local_map);
@@ -7506,7 +7544,7 @@ menu_bar_items (Lisp_Object old)
        Lisp_Object tem;
        ptrdiff_t nminor;
        nminor = current_minor_maps (NULL, &tmaps);
-       maps = (Lisp_Object *) alloca ((nminor + 3) * sizeof (maps[0]));
+       maps = alloca ((nminor + 3) * sizeof *maps);
        nmaps = 0;
        if (tem = get_local_map (PT, current_buffer, Qkeymap), !NILP (tem))
          maps[nmaps++] = tem;
@@ -7549,9 +7587,9 @@ menu_bar_items (Lisp_Object old)
            tem2 = AREF (menu_bar_items_vector, i + 2);
            tem3 = AREF (menu_bar_items_vector, i + 3);
            if (end > i + 4)
-             memmove (&AREF (menu_bar_items_vector, i),
-                      &AREF (menu_bar_items_vector, i + 4),
-                      (end - i - 4) * sizeof (Lisp_Object));
+             memmove (aref_addr (menu_bar_items_vector, i),
+                      aref_addr (menu_bar_items_vector, i + 4),
+                      (end - i - 4) * word_size);
            ASET (menu_bar_items_vector, end - 4, tem0);
            ASET (menu_bar_items_vector, end - 3, tem1);
            ASET (menu_bar_items_vector, end - 2, tem2);
@@ -7599,9 +7637,9 @@ menu_bar_item (Lisp_Object key, Lisp_Object item, Lisp_Object dummy1, void *dumm
        if (EQ (key, AREF (menu_bar_items_vector, i)))
          {
            if (menu_bar_items_index > i + 4)
-             memmove (&AREF (menu_bar_items_vector, i),
-                      &AREF (menu_bar_items_vector, i + 4),
-                      (menu_bar_items_index - i - 4) * sizeof (Lisp_Object));
+             memmove (aref_addr (menu_bar_items_vector, i),
+                      aref_addr (menu_bar_items_vector, i + 4),
+                      (menu_bar_items_index - i - 4) * word_size);
            menu_bar_items_index -= 4;
          }
     }
@@ -7918,7 +7956,8 @@ parse_menu_item (Lisp_Object item, int inmenubar)
                    /* If the command is an alias for another
                       (such as lmenu.el set it up), check if the
                       original command matches the cached command.  */
-                   && !(SYMBOLP (def) && EQ (tem, XSYMBOL (def)->function))))
+                   && !(SYMBOLP (def)
+                        && EQ (tem, XSYMBOL (def)->function))))
              keys = Qnil;
          }
 
@@ -8033,7 +8072,7 @@ tool_bar_items (Lisp_Object reuse, int *nitems)
   if (!NILP (Voverriding_local_map_menu_flag))
     {
       /* Yes, use them (if non-nil) as well as the global map.  */
-      maps = (Lisp_Object *) alloca (3 * sizeof (maps[0]));
+      maps = alloca (3 * sizeof *maps);
       nmaps = 0;
       if (!NILP (KVAR (current_kboard, Voverriding_terminal_local_map)))
        maps[nmaps++] = KVAR (current_kboard, Voverriding_terminal_local_map);
@@ -8050,7 +8089,7 @@ tool_bar_items (Lisp_Object reuse, int *nitems)
       Lisp_Object tem;
       ptrdiff_t nminor;
       nminor = current_minor_maps (NULL, &tmaps);
-      maps = (Lisp_Object *) alloca ((nminor + 3) * sizeof (maps[0]));
+      maps = alloca ((nminor + 3) * sizeof *maps);
       nmaps = 0;
       if (tem = get_local_map (PT, current_buffer, Qkeymap), !NILP (tem))
        maps[nmaps++] = tem;
@@ -8105,7 +8144,7 @@ process_tool_bar_item (Lisp_Object key, Lisp_Object def, Lisp_Object data, void
              if (ntool_bar_items > i + TOOL_BAR_ITEM_NSLOTS)
                memmove (v, v + TOOL_BAR_ITEM_NSLOTS,
                         ((ntool_bar_items - i - TOOL_BAR_ITEM_NSLOTS)
-                         * sizeof (Lisp_Object)));
+                         * word_size));
              ntool_bar_items -= TOOL_BAR_ITEM_NSLOTS;
              break;
            }
@@ -8119,6 +8158,14 @@ process_tool_bar_item (Lisp_Object key, Lisp_Object def, Lisp_Object data, void
   UNGCPRO;
 }
 
+/* Access slot with index IDX of vector tool_bar_item_properties.  */
+#define PROP(IDX) AREF (tool_bar_item_properties, (IDX))
+static inline void
+set_prop (ptrdiff_t idx, Lisp_Object val)
+{
+  ASET (tool_bar_item_properties, idx, val);
+}
+
 
 /* Parse a tool bar item specification ITEM for key KEY and return the
    result in tool_bar_item_properties.  Value is zero if ITEM is
@@ -8169,9 +8216,6 @@ process_tool_bar_item (Lisp_Object key, Lisp_Object def, Lisp_Object data, void
 static int
 parse_tool_bar_item (Lisp_Object key, Lisp_Object item)
 {
-  /* Access slot with index IDX of vector tool_bar_item_properties.  */
-#define PROP(IDX) AREF (tool_bar_item_properties, (IDX))
-
   Lisp_Object filter = Qnil;
   Lisp_Object caption;
   int i, have_label = 0;
@@ -8195,15 +8239,15 @@ parse_tool_bar_item (Lisp_Object key, Lisp_Object item)
   if (VECTORP (tool_bar_item_properties))
     {
       for (i = 0; i < TOOL_BAR_ITEM_NSLOTS; ++i)
-       PROP (i) = Qnil;
+       set_prop (i, Qnil);
     }
   else
     tool_bar_item_properties
       = Fmake_vector (make_number (TOOL_BAR_ITEM_NSLOTS), Qnil);
 
   /* Set defaults.  */
-  PROP (TOOL_BAR_ITEM_KEY) = key;
-  PROP (TOOL_BAR_ITEM_ENABLED_P) = Qt;
+  set_prop (TOOL_BAR_ITEM_KEY, key);
+  set_prop (TOOL_BAR_ITEM_ENABLED_P, Qt);
 
   /* Get the caption of the item.  If the caption is not a string,
      evaluate it to get a string.  If we don't get a string, skip this
@@ -8215,7 +8259,7 @@ parse_tool_bar_item (Lisp_Object key, Lisp_Object item)
       if (!STRINGP (caption))
        return 0;
     }
-  PROP (TOOL_BAR_ITEM_CAPTION) = caption;
+  set_prop (TOOL_BAR_ITEM_CAPTION, caption);
 
   /* If the rest following the caption is not a list, the menu item is
      either a separator, or invalid.  */
@@ -8224,7 +8268,7 @@ parse_tool_bar_item (Lisp_Object key, Lisp_Object item)
     {
       if (menu_separator_name_p (SSDATA (caption)))
        {
-         PROP (TOOL_BAR_ITEM_TYPE) = Qt;
+         set_prop (TOOL_BAR_ITEM_TYPE, Qt);
 #if !defined (USE_GTK) && !defined (HAVE_NS)
          /* If we use build_desired_tool_bar_string to render the
             tool bar, the separator is rendered as an image.  */
@@ -8240,7 +8284,7 @@ parse_tool_bar_item (Lisp_Object key, Lisp_Object item)
     }
 
   /* Store the binding.  */
-  PROP (TOOL_BAR_ITEM_BINDING) = XCAR (item);
+  set_prop (TOOL_BAR_ITEM_BINDING, XCAR (item));
   item = XCDR (item);
 
   /* Ignore cached key binding, if any.  */
@@ -8259,9 +8303,9 @@ parse_tool_bar_item (Lisp_Object key, Lisp_Object item)
        {
          /* `:enable FORM'.  */
          if (!NILP (Venable_disabled_menus_and_buttons))
-           PROP (TOOL_BAR_ITEM_ENABLED_P) = Qt;
+           set_prop (TOOL_BAR_ITEM_ENABLED_P, Qt);
          else
-           PROP (TOOL_BAR_ITEM_ENABLED_P) = value;
+           set_prop (TOOL_BAR_ITEM_ENABLED_P, value);
        }
       else if (EQ (ikey, QCvisible))
        {
@@ -8272,17 +8316,16 @@ parse_tool_bar_item (Lisp_Object key, Lisp_Object item)
        }
       else if (EQ (ikey, QChelp))
         /* `:help HELP-STRING'.  */
-        PROP (TOOL_BAR_ITEM_HELP) = value;
+        set_prop (TOOL_BAR_ITEM_HELP, value);
       else if (EQ (ikey, QCvert_only))
         /* `:vert-only t/nil'.  */
-        PROP (TOOL_BAR_ITEM_VERT_ONLY) = value;
+        set_prop (TOOL_BAR_ITEM_VERT_ONLY, value);
       else if (EQ (ikey, QClabel))
         {
           const char *bad_label = "!!?GARBLED ITEM?!!";
           /* `:label LABEL-STRING'.  */
-          PROP (TOOL_BAR_ITEM_LABEL) = STRINGP (value)
-            ? value
-            : build_string (bad_label);
+          set_prop (TOOL_BAR_ITEM_LABEL,
+                   STRINGP (value) ? value : build_string (bad_label));
           have_label = 1;
         }
       else if (EQ (ikey, QCfilter))
@@ -8297,8 +8340,8 @@ parse_tool_bar_item (Lisp_Object key, Lisp_Object item)
          selected = XCDR (value);
          if (EQ (type, QCtoggle) || EQ (type, QCradio))
            {
-             PROP (TOOL_BAR_ITEM_SELECTED_P) = selected;
-             PROP (TOOL_BAR_ITEM_TYPE) = type;
+             set_prop (TOOL_BAR_ITEM_SELECTED_P, selected);
+             set_prop (TOOL_BAR_ITEM_TYPE, type);
            }
        }
       else if (EQ (ikey, QCimage)
@@ -8306,10 +8349,10 @@ parse_tool_bar_item (Lisp_Object key, Lisp_Object item)
                   || (VECTORP (value) && ASIZE (value) == 4)))
        /* Value is either a single image specification or a vector
           of 4 such specifications for the different button states.  */
-       PROP (TOOL_BAR_ITEM_IMAGES) = value;
+       set_prop (TOOL_BAR_ITEM_IMAGES, value);
       else if (EQ (ikey, QCrtl))
         /* ':rtl STRING' */
-       PROP (TOOL_BAR_ITEM_RTL_IMAGE) = value;
+       set_prop (TOOL_BAR_ITEM_RTL_IMAGE, value);
     }
 
 
@@ -8351,18 +8394,19 @@ parse_tool_bar_item (Lisp_Object key, Lisp_Object item)
 
       new_lbl = Fupcase_initials (build_string (label));
       if (SCHARS (new_lbl) <= tool_bar_max_label_size)
-        PROP (TOOL_BAR_ITEM_LABEL) = new_lbl;
+        set_prop (TOOL_BAR_ITEM_LABEL, new_lbl);
       else
-        PROP (TOOL_BAR_ITEM_LABEL) = make_string ("", 0);
+        set_prop (TOOL_BAR_ITEM_LABEL, empty_unibyte_string);
       xfree (buf);
     }
 
   /* If got a filter apply it on binding.  */
   if (!NILP (filter))
-    PROP (TOOL_BAR_ITEM_BINDING)
-      = menu_item_eval_property (list2 (filter,
-                                       list2 (Qquote,
-                                              PROP (TOOL_BAR_ITEM_BINDING))));
+    set_prop (TOOL_BAR_ITEM_BINDING,
+             (menu_item_eval_property
+              (list2 (filter,
+                      list2 (Qquote,
+                             PROP (TOOL_BAR_ITEM_BINDING))))));
 
   /* See if the binding is a keymap.  Give up if it is.  */
   if (CONSP (get_keymap (PROP (TOOL_BAR_ITEM_BINDING), 0, 1)))
@@ -8370,13 +8414,13 @@ parse_tool_bar_item (Lisp_Object key, Lisp_Object item)
 
   /* Enable or disable selection of item.  */
   if (!EQ (PROP (TOOL_BAR_ITEM_ENABLED_P), Qt))
-    PROP (TOOL_BAR_ITEM_ENABLED_P)
-      = menu_item_eval_property (PROP (TOOL_BAR_ITEM_ENABLED_P));
+    set_prop (TOOL_BAR_ITEM_ENABLED_P,
+             menu_item_eval_property (PROP (TOOL_BAR_ITEM_ENABLED_P)));
 
   /* Handle radio buttons or toggle boxes.  */
   if (!NILP (PROP (TOOL_BAR_ITEM_SELECTED_P)))
-    PROP (TOOL_BAR_ITEM_SELECTED_P)
-      = menu_item_eval_property (PROP (TOOL_BAR_ITEM_SELECTED_P));
+    set_prop (TOOL_BAR_ITEM_SELECTED_P,
+             menu_item_eval_property (PROP (TOOL_BAR_ITEM_SELECTED_P)));
 
   return 1;
 
@@ -8404,7 +8448,6 @@ init_tool_bar_items (Lisp_Object reuse)
 static void
 append_tool_bar_item (void)
 {
-  Lisp_Object *to, *from;
   ptrdiff_t incr =
     (ntool_bar_items
      - (ASIZE (tool_bar_items_vector) - TOOL_BAR_ITEM_NSLOTS));
@@ -8416,9 +8459,8 @@ append_tool_bar_item (void)
 
   /* Append entries from tool_bar_item_properties to the end of
      tool_bar_items_vector.  */
-  to = XVECTOR (tool_bar_items_vector)->contents + ntool_bar_items;
-  from = XVECTOR (tool_bar_item_properties)->contents;
-  memcpy (to, from, TOOL_BAR_ITEM_NSLOTS * sizeof *to);
+  vcopy (tool_bar_items_vector, ntool_bar_items,
+        XVECTOR (tool_bar_item_properties)->contents, TOOL_BAR_ITEM_NSLOTS);
   ntool_bar_items += TOOL_BAR_ITEM_NSLOTS;
 }
 
@@ -8450,7 +8492,9 @@ static Lisp_Object
 read_char_x_menu_prompt (ptrdiff_t nmaps, Lisp_Object *maps,
                         Lisp_Object prev_event, int *used_mouse_menu)
 {
+#ifdef HAVE_MENUS
   ptrdiff_t mapno;
+#endif
 
   if (used_mouse_menu)
     *used_mouse_menu = 0;
@@ -8475,8 +8519,7 @@ read_char_x_menu_prompt (ptrdiff_t nmaps, Lisp_Object *maps,
       && !EQ (XCAR (prev_event), Qtool_bar))
     {
       /* Display the menu and get the selection.  */
-      Lisp_Object *realmaps
-       = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
+      Lisp_Object *realmaps = alloca (nmaps * sizeof *realmaps);
       Lisp_Object value;
       ptrdiff_t nmaps1 = 0;
 
@@ -8570,7 +8613,7 @@ read_char_minibuf_menu_prompt (int commandflag,
   if (width + 4 > read_char_minibuf_menu_width)
     {
       read_char_minibuf_menu_text
-       = (char *) xrealloc (read_char_minibuf_menu_text, width + 4);
+       = xrealloc (read_char_minibuf_menu_text, width + 4);
       read_char_minibuf_menu_width = width + 4;
     }
   menu = read_char_minibuf_menu_text;
@@ -8747,11 +8790,11 @@ read_char_minibuf_menu_prompt (int commandflag,
         is not used on replay.
         */
       orig_defn_macro = KVAR (current_kboard, defining_kbd_macro);
-      KVAR (current_kboard, defining_kbd_macro) = Qnil;
+      kset_defining_kbd_macro (current_kboard, Qnil);
       do
        obj = read_char (commandflag, 0, 0, Qt, 0, NULL);
       while (BUFFERP (obj));
-      KVAR (current_kboard, defining_kbd_macro) = orig_defn_macro;
+      kset_defining_kbd_macro (current_kboard, orig_defn_macro);
 
       if (!INTEGERP (obj))
        return obj;
@@ -8845,23 +8888,17 @@ access_keymap_keyremap (Lisp_Object map, Lisp_Object key, Lisp_Object prompt,
 
   next = access_keymap (map, key, 1, 0, 1);
 
-  /* Handle symbol with autoload definition.  */
-  if (SYMBOLP (next) && !NILP (Ffboundp (next))
-      && CONSP (XSYMBOL (next)->function)
-      && EQ (XCAR (XSYMBOL (next)->function), Qautoload))
-    do_autoload (XSYMBOL (next)->function, next);
-
   /* Handle a symbol whose function definition is a keymap
      or an array.  */
   if (SYMBOLP (next) && !NILP (Ffboundp (next))
       && (ARRAYP (XSYMBOL (next)->function)
          || KEYMAPP (XSYMBOL (next)->function)))
-    next = XSYMBOL (next)->function;
+    next = Fautoload_do_load (XSYMBOL (next)->function, next, Qnil);
 
   /* If the keymap gives a function, not an
      array, then call the function with one arg and use
      its value instead.  */
-  if (SYMBOLP (next) && !NILP (Ffboundp (next)) && do_funcall)
+  if (do_funcall && FUNCTIONP (next))
     {
       Lisp_Object tem;
       tem = next;
@@ -9113,7 +9150,7 @@ read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt,
          /* Install the string STR as the beginning of the string of
             echoing, so that it serves as a prompt for the next
             character.  */
-         KVAR (current_kboard, echo_string) = prompt;
+         kset_echo_string (current_kboard, prompt);
          current_kboard->echo_after_prompt = SCHARS (prompt);
          echo_now ();
        }
@@ -9177,8 +9214,8 @@ read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt,
     {
       if (2 > nmaps_allocated)
        {
-         submaps = (Lisp_Object *) alloca (2 * sizeof (submaps[0]));
-         defs    = (Lisp_Object *) alloca (2 * sizeof (defs[0]));
+         submaps = alloca (2 * sizeof *submaps);
+         defs    = alloca (2 * sizeof *defs);
          nmaps_allocated = 2;
        }
       submaps[nmaps++] = KVAR (current_kboard, Voverriding_terminal_local_map);
@@ -9187,8 +9224,8 @@ read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt,
     {
       if (2 > nmaps_allocated)
        {
-         submaps = (Lisp_Object *) alloca (2 * sizeof (submaps[0]));
-         defs    = (Lisp_Object *) alloca (2 * sizeof (defs[0]));
+         submaps = alloca (2 * sizeof *submaps);
+         defs    = alloca (2 * sizeof *defs);
          nmaps_allocated = 2;
        }
       submaps[nmaps++] = Voverriding_local_map;
@@ -9204,8 +9241,8 @@ read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt,
 
       if (total > nmaps_allocated)
        {
-         submaps = (Lisp_Object *) alloca (total * sizeof (submaps[0]));
-         defs    = (Lisp_Object *) alloca (total * sizeof (defs[0]));
+         submaps = alloca (total * sizeof *submaps);
+         defs    = alloca (total * sizeof *defs);
          nmaps_allocated = total;
        }
 
@@ -9359,15 +9396,17 @@ read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt,
 
                if (!NILP (delayed_switch_frame))
                  {
-                   KVAR (interrupted_kboard, kbd_queue)
-                     = Fcons (delayed_switch_frame,
-                              KVAR (interrupted_kboard, kbd_queue));
+                   kset_kbd_queue
+                     (interrupted_kboard,
+                      Fcons (delayed_switch_frame,
+                             KVAR (interrupted_kboard, kbd_queue)));
                    delayed_switch_frame = Qnil;
                  }
 
                while (t > 0)
-                 KVAR (interrupted_kboard, kbd_queue)
-                   = Fcons (keybuf[--t], KVAR (interrupted_kboard, kbd_queue));
+                 kset_kbd_queue
+                   (interrupted_kboard,
+                    Fcons (keybuf[--t], KVAR (interrupted_kboard, kbd_queue)));
 
                /* If the side queue is non-empty, ensure it begins with a
                   switch-frame, so we'll replay it in the right context.  */
@@ -9379,9 +9418,10 @@ read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt,
                  {
                    Lisp_Object frame;
                    XSETFRAME (frame, interrupted_frame);
-                   KVAR (interrupted_kboard, kbd_queue)
-                     = Fcons (make_lispy_switch_frame (frame),
-                              KVAR (interrupted_kboard, kbd_queue));
+                   kset_kbd_queue
+                     (interrupted_kboard,
+                      Fcons (make_lispy_switch_frame (frame),
+                             KVAR (interrupted_kboard, kbd_queue)));
                  }
                mock_input = 0;
                orig_local_map = get_local_map (PT, current_buffer, Qlocal_map);
@@ -9537,7 +9577,7 @@ read_key_sequence (Lisp_Object *keybuf, int bufsize, Lisp_Object prompt,
                         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 ());
+                     record_unwind_current_buffer ();
 
                      if (! FRAME_LIVE_P (XFRAME (selected_frame)))
                        Fkill_emacs (Qnil);
@@ -10275,7 +10315,7 @@ a special event, so ignore the prefix argument and don't clear it.  */)
     {
       prefixarg = KVAR (current_kboard, Vprefix_arg);
       Vcurrent_prefix_arg = prefixarg;
-      KVAR (current_kboard, Vprefix_arg) = Qnil;
+      kset_prefix_arg (current_kboard, Qnil);
     }
   else
     prefixarg = Qnil;
@@ -10300,7 +10340,7 @@ a special event, so ignore the prefix argument and don't clear it.  */)
          struct gcpro gcpro1, gcpro2;
 
          GCPRO2 (cmd, prefixarg);
-         do_autoload (final, cmd);
+         Fautoload_do_load (final, cmd, Qnil);
          UNGCPRO;
        }
       else
@@ -10449,10 +10489,10 @@ DEFUN ("recent-keys", Frecent_keys, Srecent_keys, 0, 0, 0,
   else
     {
       val = Fvector (NUM_RECENT_KEYS, keys);
-      memcpy (XVECTOR (val)->contents, keys + recent_keys_index,
-             (NUM_RECENT_KEYS - recent_keys_index) * sizeof (Lisp_Object));
-      memcpy (XVECTOR (val)->contents + NUM_RECENT_KEYS - recent_keys_index,
-             keys, recent_keys_index * sizeof (Lisp_Object));
+      vcopy (val, 0, keys + recent_keys_index,
+            NUM_RECENT_KEYS - recent_keys_index);
+      vcopy (val, NUM_RECENT_KEYS - recent_keys_index,
+            keys, recent_keys_index);
       return val;
     }
 }
@@ -10751,17 +10791,10 @@ clear_waiting_for_input (void)
    Otherwise, tell QUIT to kill Emacs.  */
 
 static void
-interrupt_signal (int signalnum)       /* If we don't have an argument, some */
-                                       /* compilers complain in signal calls.  */
+handle_interrupt_signal (int sig)
 {
-  /* Must preserve main program's value of errno.  */
-  int old_errno = errno;
-  struct terminal *terminal;
-
-  SIGNAL_THREAD_CHECK (signalnum);
-
   /* See if we have an active terminal on our controlling tty.  */
-  terminal = get_named_tty ("/dev/tty");
+  struct terminal *terminal = get_named_tty ("/dev/tty");
   if (!terminal)
     {
       /* If there are no frames there, let's pretend that we are a
@@ -10782,10 +10815,15 @@ interrupt_signal (int signalnum)      /* If we don't have an argument, some */
 
       handle_interrupt ();
     }
+}
 
-  errno = old_errno;
+static void
+deliver_interrupt_signal (int sig)
+{
+  handle_on_main_thread (sig, handle_interrupt_signal);
 }
 
+
 /* If Emacs is stuck because `inhibit-quit' is true, then keep track
    of the number of times C-g has been requested.  If C-g is pressed
    enough times, then quit anyway.  See bug#6585.  */
@@ -10815,7 +10853,10 @@ handle_interrupt (void)
       /* If SIGINT isn't blocked, don't let us be interrupted by
         another SIGINT, it might be harmful due to non-reentrancy
         in I/O functions.  */
-      sigblock (sigmask (SIGINT));
+      sigset_t blocked;
+      sigemptyset (&blocked);
+      sigaddset (&blocked, SIGINT);
+      pthread_sigmask (SIG_BLOCK, &blocked, 0);
 
       fflush (stdout);
       reset_all_sys_modes ();
@@ -10877,7 +10918,7 @@ handle_interrupt (void)
 #endif /* not MSDOS */
       fflush (stdout);
       if (((c = getchar ()) & ~040) == 'Y')
-       abort ();
+       emacs_abort ();
       while (c != '\n') c = getchar ();
 #ifdef MSDOS
       printf ("\r\nContinuing...\r\n");
@@ -10886,7 +10927,7 @@ handle_interrupt (void)
 #endif /* not MSDOS */
       fflush (stdout);
       init_all_sys_modes ();
-      sigfree ();
+      pthread_sigmask (SIG_SETMASK, &empty_mask, 0);
     }
   else
     {
@@ -10899,7 +10940,7 @@ handle_interrupt (void)
          struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
 
          immediate_quit = 0;
-          sigfree ();
+         pthread_sigmask (SIG_SETMASK, &empty_mask, 0);
          saved = gl_state;
          GCPRO4 (saved.object, saved.global_code,
                  saved.current_syntax_table, saved.old_prop);
@@ -10944,7 +10985,7 @@ quit_throw_to_read_char (int from_signal)
   if (!from_signal && EQ (Vquit_flag, Qkill_emacs))
     Fkill_emacs (Qnil);
 
-  sigfree ();
+  pthread_sigmask (SIG_SETMASK, &empty_mask, 0);
   /* Prevent another signal from doing this before we finish.  */
   clear_waiting_for_input ();
   input_pending = 0;
@@ -10958,7 +10999,7 @@ quit_throw_to_read_char (int from_signal)
 #ifdef POLL_FOR_INPUT
   /* May be > 1 if in recursive minibuffer.  */
   if (poll_suppress_count == 0)
-    abort ();
+    emacs_abort ();
 #endif
 #endif
   if (FRAMEP (internal_last_event_frame)
@@ -11214,11 +11255,8 @@ The `posn-' functions access elements of such lists.  */)
 
   if (WINDOWP (frame_or_window))
     {
-      struct window *w;
+      struct window *w = decode_live_window (frame_or_window);
 
-      CHECK_LIVE_WINDOW (frame_or_window);
-
-      w = XWINDOW (frame_or_window);
       XSETINT (x, (XINT (x)
                   + WINDOW_LEFT_EDGE_X (w)
                   + (NILP (whole)
@@ -11273,30 +11311,30 @@ The `posn-' functions access elements of such lists.  */)
 void
 init_kboard (KBOARD *kb)
 {
-  KVAR (kb, Voverriding_terminal_local_map) = Qnil;
-  KVAR (kb, Vlast_command) = Qnil;
-  KVAR (kb, Vreal_last_command) = Qnil;
-  KVAR (kb, Vkeyboard_translate_table) = Qnil;
-  KVAR (kb, Vlast_repeatable_command) = Qnil;
-  KVAR (kb, Vprefix_arg) = Qnil;
-  KVAR (kb, Vlast_prefix_arg) = Qnil;
-  KVAR (kb, kbd_queue) = Qnil;
+  kset_overriding_terminal_local_map (kb, Qnil);
+  kset_last_command (kb, Qnil);
+  kset_real_last_command (kb, Qnil);
+  kset_keyboard_translate_table (kb, Qnil);
+  kset_last_repeatable_command (kb, Qnil);
+  kset_prefix_arg (kb, Qnil);
+  kset_last_prefix_arg (kb, Qnil);
+  kset_kbd_queue (kb, Qnil);
   kb->kbd_queue_has_data = 0;
   kb->immediate_echo = 0;
-  KVAR (kb, echo_string) = Qnil;
+  kset_echo_string (kb, Qnil);
   kb->echo_after_prompt = -1;
   kb->kbd_macro_buffer = 0;
   kb->kbd_macro_bufsize = 0;
-  KVAR (kb, defining_kbd_macro) = Qnil;
-  KVAR (kb, Vlast_kbd_macro) = Qnil;
+  kset_defining_kbd_macro (kb, Qnil);
+  kset_last_kbd_macro (kb, Qnil);
   kb->reference_count = 0;
-  KVAR (kb, Vsystem_key_alist) = Qnil;
-  KVAR (kb, system_key_syms) = Qnil;
-  KVAR (kb, Vwindow_system) = Qt;      /* Unset.  */
-  KVAR (kb, Vinput_decode_map) = Fmake_sparse_keymap (Qnil);
-  KVAR (kb, Vlocal_function_key_map) = Fmake_sparse_keymap (Qnil);
+  kset_system_key_alist (kb, Qnil);
+  kset_system_key_syms (kb, Qnil);
+  kset_window_system (kb, Qt); /* Unset.  */
+  kset_input_decode_map (kb, Fmake_sparse_keymap (Qnil));
+  kset_local_function_key_map (kb, Fmake_sparse_keymap (Qnil));
   Fset_keymap_parent (KVAR (kb, Vlocal_function_key_map), Vfunction_key_map);
-  KVAR (kb, Vdefault_minibuffer_frame) = Qnil;
+  kset_default_minibuffer_frame (kb, Qnil);
 }
 
 /*
@@ -11319,7 +11357,7 @@ delete_kboard (KBOARD *kb)
 
   for (kbp = &all_kboards; *kbp != kb; kbp = &(*kbp)->next_kboard)
     if (*kbp == NULL)
-      abort ();
+      emacs_abort ();
   *kbp = kb->next_kboard;
 
   /* Prevent a dangling reference to KB.  */
@@ -11330,7 +11368,7 @@ delete_kboard (KBOARD *kb)
       current_kboard = FRAME_KBOARD (XFRAME (selected_frame));
       single_kboard = 0;
       if (current_kboard == kb)
-       abort ();
+       emacs_abort ();
     }
 
   wipe_kboard (kb);
@@ -11346,7 +11384,7 @@ init_keyboard (void)
   quit_char = Ctl ('g');
   Vunread_command_events = Qnil;
   unread_command_char = -1;
-  EMACS_SET_INVALID_TIME (timer_idleness_start_time);
+  timer_idleness_start_time = invalid_emacs_time ();
   total_keys = 0;
   recent_keys_index = 0;
   kbd_fetch_ptr = kbd_buffer;
@@ -11372,7 +11410,7 @@ init_keyboard (void)
   init_kboard (current_kboard);
   /* A value of nil for Vwindow_system normally means a tty, but we also use
      it for the initial terminal since there is no window system there.  */
-  KVAR (current_kboard, Vwindow_system) = Qnil;
+  kset_window_system (current_kboard, Qnil);
 
   if (!noninteractive)
     {
@@ -11382,17 +11420,23 @@ init_keyboard (void)
          SIGINT.  There is special code in interrupt_signal to exit
          Emacs on SIGINT when there are no termcap frames on the
          controlling terminal.  */
-      signal (SIGINT, interrupt_signal);
+      struct sigaction action;
+      emacs_sigaction_init (&action, deliver_interrupt_signal);
+      sigaction (SIGINT, &action, 0);
 #ifndef DOS_NT
       /* 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);
+      sigaction (SIGQUIT, &action, 0);
 #endif /* not DOS_NT */
     }
 /* Note SIGIO has been undef'd if FIONREAD is missing.  */
 #ifdef SIGIO
   if (!noninteractive)
-    signal (SIGIO, input_available_signal);
+    {
+      struct sigaction action;
+      emacs_sigaction_init (&action, deliver_input_available_signal);
+      sigaction (SIGIO, &action, 0);
+    }
 #endif /* SIGIO */
 
 /* Use interrupt input by default, if it works and noninterrupt input
@@ -11404,7 +11448,7 @@ init_keyboard (void)
   interrupt_input = 0;
 #endif
 
-  sigfree ();
+  pthread_sigmask (SIG_SETMASK, &empty_mask, 0);
   dribble = 0;
 
   if (keyboard_init_hook)
@@ -11443,7 +11487,7 @@ syms_of_keyboard (void)
   pending_funcalls = Qnil;
   staticpro (&pending_funcalls);
 
-  Vlispy_mouse_stem = make_pure_c_string ("mouse");
+  Vlispy_mouse_stem = build_pure_c_string ("mouse");
   staticpro (&Vlispy_mouse_stem);
 
   /* Tool-bars.  */
@@ -12187,7 +12231,7 @@ variable are `sigusr1' and `sigusr2'.  */);
   Vdebug_on_event = intern_c_string ("sigusr2");
 
   /* Create the initial keyboard.  */
-  initial_kboard = xmalloc (sizeof (KBOARD));
+  initial_kboard = xmalloc (sizeof *initial_kboard);
   init_kboard (initial_kboard);
   /* Vwindow_system is left at t for now.  */
   initial_kboard->next_kboard = all_kboards;
@@ -12250,6 +12294,10 @@ keys_of_keyboard (void)
 
   initial_define_lispy_key (Vspecial_event_map, "config-changed-event",
                            "ignore");
+#if defined (WINDOWSNT)
+  initial_define_lispy_key (Vspecial_event_map, "language-change",
+                           "ignore");
+#endif
 }
 
 /* Mark the pointers in the kboard objects.