merging Emacs.app (NeXTstep port)
[bpt/emacs.git] / src / keyboard.c
index 867a8d2..b908b8a 100644 (file)
@@ -5,10 +5,10 @@
 
 This file is part of GNU Emacs.
 
-GNU Emacs is free software; you can redistribute it and/or modify
+GNU Emacs is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 3, or (at your option)
-any later version.
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -16,9 +16,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-Boston, MA 02110-1301, USA.  */
+along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <config.h>
 #include <signal.h>
@@ -82,6 +80,11 @@ Boston, MA 02110-1301, USA.  */
 #include "macterm.h"
 #endif
 
+#ifdef HAVE_NS
+#include "nsterm.h"
+extern Lisp_Object Qsuper;
+#endif
+
 #ifndef USE_CRT_DLL
 extern int errno;
 #endif
@@ -132,6 +135,9 @@ int this_command_key_count_reset;
 Lisp_Object raw_keybuf;
 int raw_keybuf_count;
 
+/* Non-nil if the present key sequence was obtained by shift translation.  */
+Lisp_Object Vthis_command_keys_shift_translated;
+
 #define GROW_RAW_KEYBUF                                                        \
  if (raw_keybuf_count == XVECTOR (raw_keybuf)->size)                   \
    raw_keybuf = larger_vector (raw_keybuf, raw_keybuf_count * 2, Qnil)  \
@@ -154,11 +160,6 @@ extern int message_enable_multibyte;
 
 Lisp_Object Vshow_help_function;
 
-/* If a string, the message displayed before displaying a help-echo
-   in the echo area.  */
-
-Lisp_Object Vpre_help_message;
-
 /* Nonzero means do menu prompting.  */
 
 static int menu_prompting;
@@ -394,6 +395,7 @@ Lisp_Object Vinput_method_previous_message;
 
 /* Non-nil means deactivate the mark at end of this command.  */
 Lisp_Object Vdeactivate_mark;
+Lisp_Object Qdeactivate_mark;
 
 /* Menu bar specified in Lucid Emacs fashion.  */
 
@@ -918,7 +920,7 @@ add_command_key (key)
                                       2 * ASIZE (this_command_keys),
                                       Qnil);
 
-  AREF (this_command_keys, this_command_key_count) = key;
+  ASET (this_command_keys, this_command_key_count, key);
   ++this_command_key_count;
 }
 
@@ -935,7 +937,7 @@ recursive_edit_1 ()
       specbind (Qstandard_input, Qt);
     }
 
-#ifdef HAVE_X_WINDOWS
+#ifdef HAVE_WINDOW_SYSTEM
   /* The command loop has started an hourglass timer, so we have to
      cancel it here, otherwise it will fire because the recursive edit
      can take some time.  Do not check for display_hourglass_p here,
@@ -1220,7 +1222,7 @@ cmd_error (data)
   Lisp_Object old_level, old_length;
   char macroerror[50];
 
-#ifdef HAVE_X_WINDOWS
+#ifdef HAVE_WINDOW_SYSTEM
   if (display_hourglass_p)
     cancel_hourglass ();
 #endif
@@ -1396,7 +1398,7 @@ DEFUN ("top-level", Ftop_level, Stop_level, 0, 0, "",
        doc: /* Exit all recursive editing levels.  */)
      ()
 {
-#ifdef HAVE_X_WINDOWS
+#ifdef HAVE_WINDOW_SYSTEM
   if (display_hourglass_p)
     cancel_hourglass ();
 #endif
@@ -1519,7 +1521,7 @@ static void adjust_point_for_property P_ ((int, int));
 
 /* Cancel hourglass from protect_unwind.
    ARG is not used.  */
-#ifdef HAVE_X_WINDOWS
+#ifdef HAVE_WINDOW_SYSTEM
 static Lisp_Object
 cancel_hourglass_unwind (arg)
      Lisp_Object arg;
@@ -1603,17 +1605,15 @@ command_loop_1 ()
 
       if (minibuf_level
          && !NILP (echo_area_buffer[0])
-         && EQ (minibuf_window, echo_area_window))
+         && EQ (minibuf_window, echo_area_window)
+         && NUMBERP (Vminibuffer_message_timeout))
        {
          /* Bind inhibit-quit to t so that C-g gets read in
             rather than quitting back to the minibuffer.  */
          int count = SPECPDL_INDEX ();
          specbind (Qinhibit_quit, Qt);
 
-         if (NUMBERP (Vminibuffer_message_timeout))
-           sit_for (Vminibuffer_message_timeout, 0, 2);
-         else
-           sit_for (Qt, 0, 2);
+         sit_for (Vminibuffer_message_timeout, 0, 2);
 
          /* Clear the echo area.  */
          message2 (0, 0, 0);
@@ -1650,6 +1650,7 @@ command_loop_1 ()
       Vthis_command = Qnil;
       real_this_command = Qnil;
       Vthis_original_command = Qnil;
+      Vthis_command_keys_shift_translated = Qnil;
 
       /* Read next key sequence; i gets its length.  */
       i = read_key_sequence (keybuf, sizeof keybuf / sizeof keybuf[0],
@@ -1763,7 +1764,9 @@ command_loop_1 ()
 
              /* Recognize some common commands in common situations and
                 do them directly.  */
-             if (EQ (Vthis_command, Qforward_char) && PT < ZV)
+             if (EQ (Vthis_command, Qforward_char) && PT < ZV
+                 && NILP (Vthis_command_keys_shift_translated)
+                 && !CONSP (Vtransient_mark_mode))
                {
                   struct Lisp_Char_Table *dp
                    = window_display_table (XWINDOW (selected_window));
@@ -1803,7 +1806,9 @@ command_loop_1 ()
                    direct_output_forward_char (1);
                  goto directly_done;
                }
-             else if (EQ (Vthis_command, Qbackward_char) && PT > BEGV)
+             else if (EQ (Vthis_command, Qbackward_char) && PT > BEGV
+                      && NILP (Vthis_command_keys_shift_translated)
+                      && !CONSP (Vtransient_mark_mode))
                {
                   struct Lisp_Char_Table *dp
                    = window_display_table (XWINDOW (selected_window));
@@ -1893,7 +1898,7 @@ command_loop_1 ()
          /* Here for a command that isn't executed directly */
 
           {
-#ifdef HAVE_X_WINDOWS
+#ifdef HAVE_WINDOW_SYSTEM
             int scount = SPECPDL_INDEX ();
 
             if (display_hourglass_p
@@ -1909,7 +1914,7 @@ command_loop_1 ()
               Fundo_boundary ();
             Fcommand_execute (Vthis_command, Qnil, Qnil, Qnil);
 
-#ifdef HAVE_X_WINDOWS
+#ifdef HAVE_WINDOW_SYSTEM
          /* Do not check display_hourglass_p here, because
             Fcommand_execute could change it, but we should cancel
             hourglass cursor anyway.
@@ -1963,25 +1968,16 @@ command_loop_1 ()
 
       if (!NILP (current_buffer->mark_active) && !NILP (Vrun_hooks))
        {
-         /* Setting transient-mark-mode to `only' is a way of
-            turning it on for just one command.  */
-
+         /* In Emacs 22, setting transient-mark-mode to `only' was a
+            way of turning it on for just one command.  This usage is
+            obsolete, but support it anyway.  */
          if (EQ (Vtransient_mark_mode, Qidentity))
            Vtransient_mark_mode = Qnil;
-         if (EQ (Vtransient_mark_mode, Qonly))
+         else if (EQ (Vtransient_mark_mode, Qonly))
            Vtransient_mark_mode = Qidentity;
 
-         if (!NILP (Vdeactivate_mark) && !NILP (Vtransient_mark_mode))
-           {
-             /* We could also call `deactivate'mark'.  */
-             if (EQ (Vtransient_mark_mode, Qlambda))
-               Vtransient_mark_mode = Qnil;
-             else
-               {
-                 current_buffer->mark_active = Qnil;
-                 call1 (Vrun_hooks, intern ("deactivate-mark-hook"));
-               }
-           }
+         if (!NILP (Vdeactivate_mark))
+           call0 (Qdeactivate_mark);
          else if (current_buffer != prev_buffer || MODIFF != prev_modiff)
            call1 (Vrun_hooks, intern ("activate-mark-hook"));
        }
@@ -2033,6 +2029,7 @@ adjust_point_for_property (last_pt, modified)
      can't be usefully combined anyway.  */
   while (check_composition || check_display || check_invisible)
     {
+      /* FIXME: check `intangible'.  */
       if (check_composition
          && PT > BEGV && PT < ZV
          && get_property_and_range (PT, Qcomposition, &val, &beg, &end, Qnil)
@@ -2459,37 +2456,6 @@ show_help_echo (help, window, object, pos, ok_to_overwrite_keystroke_echo)
     {
       if (!NILP (Vshow_help_function))
        call1 (Vshow_help_function, help);
-      else if (/* Don't overwrite minibuffer contents.  */
-              !MINI_WINDOW_P (XWINDOW (selected_window))
-              /* Don't overwrite a keystroke echo.  */
-              && (NILP (echo_message_buffer)
-                  || ok_to_overwrite_keystroke_echo)
-              /* Don't overwrite a prompt.  */
-              && !cursor_in_echo_area)
-       {
-         if (STRINGP (help))
-           {
-             int count = SPECPDL_INDEX ();
-
-             if (!help_echo_showing_p)
-               Vpre_help_message = current_message ();
-
-             specbind (Qmessage_truncate_lines, Qt);
-             message3_nolog (help, SBYTES (help),
-                             STRING_MULTIBYTE (help));
-             unbind_to (count, Qnil);
-           }
-         else if (STRINGP (Vpre_help_message))
-           {
-             message3_nolog (Vpre_help_message,
-                             SBYTES (Vpre_help_message),
-                             STRING_MULTIBYTE (Vpre_help_message));
-             Vpre_help_message = Qnil;
-           }
-         else
-           message (0);
-       }
-
       help_echo_showing_p = STRINGP (help);
     }
 }
@@ -4186,7 +4152,8 @@ kbd_buffer_get_event (kbp, used_mouse_menu, end_time)
 #endif
        }
 
-#if defined (HAVE_X11) || defined (HAVE_NTGUI) || defined (MAC_OS)
+#if defined (HAVE_X11) || defined (HAVE_NTGUI) || defined (MAC_OS) \
+    || defined (HAVE_NS)
       else if (event->kind == DELETE_WINDOW_EVENT)
        {
          /* Make an event (delete-frame (FRAME)).  */
@@ -4195,7 +4162,8 @@ kbd_buffer_get_event (kbp, used_mouse_menu, end_time)
          kbd_fetch_ptr = event + 1;
        }
 #endif
-#if defined (HAVE_X11) || defined (HAVE_NTGUI) || defined (MAC_OS)
+#if defined (HAVE_X11) || defined (HAVE_NTGUI) || defined (MAC_OS) \
+    || defined (HAVE_NS)
       else if (event->kind == ICONIFY_EVENT)
        {
          /* Make an event (iconify-frame (FRAME)).  */
@@ -4218,7 +4186,7 @@ kbd_buffer_get_event (kbp, used_mouse_menu, end_time)
          kbd_fetch_ptr = event + 1;
        }
 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
-    || defined (USE_GTK)
+    || defined(HAVE_NS) || defined (USE_GTK)
       else if (event->kind == MENU_BAR_ACTIVATE_EVENT)
        {
          kbd_fetch_ptr = event + 1;
@@ -4328,7 +4296,7 @@ kbd_buffer_get_event (kbp, used_mouse_menu, end_time)
              obj = make_lispy_event (event);
 
 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined(MAC_OS) \
-    || defined (USE_GTK)
+    || defined(HAVE_NS) || defined (USE_GTK)
              /* If this was a menu selection, then set the flag to inhibit
                 writing to last_nonmenu_event.  Don't do this if the event
                 we're returning is (menu-bar), though; that indicates the
@@ -4507,6 +4475,13 @@ timer_resume_idle ()
 /* This is only for debugging.  */
 struct input_event last_timer_event;
 
+/* List of elisp functions to call, delayed because they were generated in
+   a context where Elisp could not be safely run (e.g. redisplay, signal,
+   ...).  Each lement has the form (FUN . ARGS).  */
+Lisp_Object pending_funcalls;
+
+extern Lisp_Object Qapply;
+
 /* Check whether a timer has fired.  To prevent larger problems we simply
    disregard elements that are not proper timers.  Do not make a circular
    timer list for the time being.
@@ -4543,6 +4518,14 @@ timer_check (do_it_now)
   chosen_timer = Qnil;
   GCPRO3 (timers, idle_timers, chosen_timer);
 
+  /* First run the code that was delayed.  */
+  while (CONSP (pending_funcalls))
+    {
+      Lisp_Object funcall = XCAR (pending_funcalls);
+      pending_funcalls = XCDR (pending_funcalls);
+      safe_call2 (Qapply, XCAR (funcall), XCDR (funcall));
+    }
+
   if (CONSP (timers) || CONSP (idle_timers))
     {
       EMACS_GET_TIME (now);
@@ -5667,7 +5650,7 @@ make_lispy_event (event)
        if (event->kind == MOUSE_CLICK_EVENT)
          {
            struct frame *f = XFRAME (event->frame_or_window);
-#if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
+#if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK) && ! defined (HAVE_NS)
            int row, column;
 #endif
 
@@ -5676,7 +5659,7 @@ make_lispy_event (event)
            if (! FRAME_LIVE_P (f))
              return Qnil;
 
-#if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
+#if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK) && ! defined (HAVE_NS)
            /* EVENT->x and EVENT->y are frame-relative pixel
               coordinates at this place.  Under old redisplay, COLUMN
               and ROW are set to frame relative glyph coordinates
@@ -5736,7 +5719,7 @@ make_lispy_event (event)
 
                return Fcons (item, Fcons (position, Qnil));
              }
-#endif /* not USE_X_TOOLKIT && not USE_GTK */
+#endif /* not USE_X_TOOLKIT && not USE_GTK && not HAVE_NS */
 
            position = make_lispy_position (f, &event->x, &event->y,
                                            event->timestamp);
@@ -6116,7 +6099,7 @@ make_lispy_event (event)
 #endif /* HAVE_MOUSE */
 
 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
-    || defined (USE_GTK)
+    || defined(HAVE_NS) || defined (USE_GTK)
     case MENU_BAR_EVENT:
       if (EQ (event->arg, event->frame_or_window))
        /* This is the prefix key.  We translate this to
@@ -7134,10 +7117,11 @@ read_avail_input (expected)
                 kill (getpid (), SIGHUP);
 
               /* XXX Is calling delete_terminal safe here?  It calls Fdelete_frame. */
-              if (t->delete_terminal_hook)
-                (*t->delete_terminal_hook) (t);
-              else
-                delete_terminal (t);
+             {
+               Lisp_Object tmp;
+               XSETTERMINAL (tmp, t);
+               Fdelete_terminal (tmp, Qnoelisp);
+             }
             }
 
           if (hold_quit.kind != NO_EVENT)
@@ -7255,7 +7239,7 @@ tty_read_avail_input (struct terminal *terminal,
          Jeffrey Honig <jch@bsdi.com> says this is generally safe. */
       if (nread == -1 && errno == EIO)
         return -2;          /* Close this terminal. */
-#if defined (AIX) && (! defined (aix386) && defined (_BSD))
+#if defined (AIX) && defined (_BSD)
       /* The kernel sometimes fails to deliver SIGHUP for ptys.
          This looks incorrect, but it isn't, because _BSD causes
          O_NDELAY to be defined in fcntl.h as O_NONBLOCK,
@@ -7826,11 +7810,11 @@ parse_menu_item (item, notreal, inmenubar)
 
   /* Initialize optional entries.  */
   for (i = ITEM_PROPERTY_DEF; i < ITEM_PROPERTY_ENABLE; i++)
-    AREF (item_properties, i) = Qnil;
-  AREF (item_properties, ITEM_PROPERTY_ENABLE) = Qt;
+    ASET (item_properties, i, Qnil);
+  ASET (item_properties, ITEM_PROPERTY_ENABLE, Qt);
 
   /* Save the item here to protect it from GC.  */
-  AREF (item_properties, ITEM_PROPERTY_ITEM) = item;
+  ASET (item_properties, ITEM_PROPERTY_ITEM, item);
 
   item_string = XCAR (item);
 
@@ -7839,12 +7823,12 @@ parse_menu_item (item, notreal, inmenubar)
   if (STRINGP (item_string))
     {
       /* Old format menu item.  */
-      AREF (item_properties, ITEM_PROPERTY_NAME) = item_string;
+      ASET (item_properties, ITEM_PROPERTY_NAME, item_string);
 
       /* Maybe help string.  */
       if (CONSP (item) && STRINGP (XCAR (item)))
        {
-         AREF (item_properties, ITEM_PROPERTY_HELP) = XCAR (item);
+         ASET (item_properties, ITEM_PROPERTY_HELP, XCAR (item));
          start = item;
          item = XCDR (item);
        }
@@ -7859,27 +7843,27 @@ parse_menu_item (item, notreal, inmenubar)
        }
 
       /* This is the real definition--the function to run.  */
-      AREF (item_properties, ITEM_PROPERTY_DEF) = item;
+      ASET (item_properties, ITEM_PROPERTY_DEF, item);
 
       /* Get enable property, if any.  */
       if (SYMBOLP (item))
        {
          tem = Fget (item, Qmenu_enable);
          if (!NILP (Venable_disabled_menus_and_buttons))
-           AREF (item_properties, ITEM_PROPERTY_ENABLE) = Qt;
+           ASET (item_properties, ITEM_PROPERTY_ENABLE, Qt);
          else if (!NILP (tem))
-           AREF (item_properties, ITEM_PROPERTY_ENABLE) = tem;
+           ASET (item_properties, ITEM_PROPERTY_ENABLE, tem);
        }
     }
   else if (EQ (item_string, Qmenu_item) && CONSP (item))
     {
       /* New format menu item.  */
-      AREF (item_properties, ITEM_PROPERTY_NAME) = XCAR (item);
+      ASET (item_properties, ITEM_PROPERTY_NAME, XCAR (item));
       start = XCDR (item);
       if (CONSP (start))
        {
          /* We have a real binding.  */
-         AREF (item_properties, ITEM_PROPERTY_DEF) = XCAR (start);
+         ASET (item_properties, ITEM_PROPERTY_DEF, XCAR (start));
 
          item = XCDR (start);
          /* Is there a cache list with key equivalences. */
@@ -7898,9 +7882,9 @@ parse_menu_item (item, notreal, inmenubar)
              if (EQ (tem, QCenable))
                {
                  if (!NILP (Venable_disabled_menus_and_buttons))
-                   AREF (item_properties, ITEM_PROPERTY_ENABLE) = Qt;
+                   ASET (item_properties, ITEM_PROPERTY_ENABLE, Qt);
                  else
-                   AREF (item_properties, ITEM_PROPERTY_ENABLE) = XCAR (item);
+                   ASET (item_properties, ITEM_PROPERTY_ENABLE, XCAR (item));
                }
              else if (EQ (tem, QCvisible) && !notreal)
                {
@@ -7911,7 +7895,7 @@ parse_menu_item (item, notreal, inmenubar)
                    return 0;
                }
              else if (EQ (tem, QChelp))
-               AREF (item_properties, ITEM_PROPERTY_HELP) = XCAR (item);
+               ASET (item_properties, ITEM_PROPERTY_HELP, XCAR (item));
              else if (EQ (tem, QCfilter))
                filter = item;
              else if (EQ (tem, QCkey_sequence))
@@ -7926,7 +7910,7 @@ parse_menu_item (item, notreal, inmenubar)
                {
                  tem = XCAR (item);
                  if (CONSP (tem) || (STRINGP (tem) && NILP (cachelist)))
-                   AREF (item_properties, ITEM_PROPERTY_KEYEQ) = tem;
+                   ASET (item_properties, ITEM_PROPERTY_KEYEQ, tem);
                }
              else if (EQ (tem, QCbutton) && CONSP (XCAR (item)))
                {
@@ -7935,10 +7919,9 @@ parse_menu_item (item, notreal, inmenubar)
                  type = XCAR (tem);
                  if (EQ (type, QCtoggle) || EQ (type, QCradio))
                    {
-                     AREF (item_properties, ITEM_PROPERTY_SELECTED)
-                       = XCDR (tem);
-                     AREF (item_properties, ITEM_PROPERTY_TYPE)
-                       = type;
+                     ASET (item_properties, ITEM_PROPERTY_SELECTED,
+                           XCDR (tem));
+                     ASET (item_properties, ITEM_PROPERTY_TYPE, type);
                    }
                }
              item = XCDR (item);
@@ -7958,7 +7941,7 @@ parse_menu_item (item, notreal, inmenubar)
       item_string = menu_item_eval_property (item_string);
       if (!STRINGP (item_string))
        return 0;
-      AREF (item_properties, ITEM_PROPERTY_NAME) = item_string;
+      ASET (item_properties, ITEM_PROPERTY_NAME, item_string);
     }
 
   /* If got a filter apply it on definition.  */
@@ -7968,7 +7951,7 @@ parse_menu_item (item, notreal, inmenubar)
       def = menu_item_eval_property (list2 (XCAR (filter),
                                            list2 (Qquote, def)));
 
-      AREF (item_properties, ITEM_PROPERTY_DEF) = def;
+      ASET (item_properties, ITEM_PROPERTY_DEF, def);
     }
 
   /* Enable or disable selection of item.  */
@@ -7981,7 +7964,7 @@ parse_menu_item (item, notreal, inmenubar)
        tem = menu_item_eval_property (tem);
       if (inmenubar && NILP (tem))
        return 0;               /* Ignore disabled items in menu bar.  */
-      AREF (item_properties, ITEM_PROPERTY_ENABLE) = tem;
+      ASET (item_properties, ITEM_PROPERTY_ENABLE, tem);
     }
 
   /* If we got no definition, this item is just unselectable text which
@@ -7995,8 +7978,8 @@ parse_menu_item (item, notreal, inmenubar)
   /* For a subkeymap, just record its details and exit.  */
   if (CONSP (tem))
     {
-      AREF (item_properties, ITEM_PROPERTY_MAP) = tem;
-      AREF (item_properties, ITEM_PROPERTY_DEF) = tem;
+      ASET (item_properties, ITEM_PROPERTY_MAP, tem);
+      ASET (item_properties, ITEM_PROPERTY_DEF, tem);
       return 1;
     }
 
@@ -8010,9 +7993,18 @@ parse_menu_item (item, notreal, inmenubar)
   if (NILP (cachelist))
     {
       /* We have to create a cachelist.  */
-      CHECK_IMPURE (start);
-      XSETCDR (start, Fcons (Fcons (Qnil, Qnil), XCDR (start)));
-      cachelist = XCAR (XCDR (start));
+      /* With the introduction of where_is_cache, the computation
+         of equivalent key bindings is sufficiently fast that we
+         do not need to cache it here any more. */
+/*PENDING: under NS this effect does not hold, perhaps due to the
+           modifier-preference changes to where-is-internal.. */
+#ifdef HAVE_NS
+       CHECK_IMPURE (start);
+       XSETCDR (start, Fcons (Fcons (Qnil, Qnil), XCDR (start)));
+       cachelist = XCAR (XCDR (start));
+#else
+      cachelist = Fcons (Qnil, Qnil);
+#endif
       newcache = 1;
       tem = AREF (item_properties, ITEM_PROPERTY_KEYEQ);
       if (!NILP (keyhint))
@@ -8078,7 +8070,12 @@ parse_menu_item (item, notreal, inmenubar)
              && SYMBOLP (XSYMBOL (def)->function)
              && ! NILP (Fget (def, Qmenu_alias)))
            def = XSYMBOL (def)->function;
+#ifdef HAVE_NS
+          /* prefer 'super' bindings */
+         tem = Fwhere_is_internal (def, Qnil, Qsuper, Qt, Qt);
+#else
          tem = Fwhere_is_internal (def, Qnil, Qt, Qnil, Qt);
+#endif
          XSETCAR (cachelist, tem);
          if (NILP (tem))
            {
@@ -8111,7 +8108,7 @@ parse_menu_item (item, notreal, inmenubar)
   if (newcache && !NILP (tem))
     {
       tem = concat2 (build_string ("  "), tem);
-      // tem = concat3 (build_string ("  ("), tem, build_string (")"));
+      /* tem = concat3 (build_string ("  ("), tem, build_string (")")); */
       XSETCDR (cachelist, tem);
     }
 
@@ -8120,7 +8117,7 @@ parse_menu_item (item, notreal, inmenubar)
     return 1;
 
   /* If we have an equivalent key binding, use that.  */
-  AREF (item_properties, ITEM_PROPERTY_KEYEQ) = tem;
+  ASET (item_properties, ITEM_PROPERTY_KEYEQ, tem);
 
   /* Include this when menu help is implemented.
   tem = XVECTOR (item_properties)->contents[ITEM_PROPERTY_HELP];
@@ -8136,8 +8133,8 @@ parse_menu_item (item, notreal, inmenubar)
   /* Handle radio buttons or toggle boxes.  */
   tem = AREF (item_properties, ITEM_PROPERTY_SELECTED);
   if (!NILP (tem))
-    AREF (item_properties, ITEM_PROPERTY_SELECTED)
-      = menu_item_eval_property (tem);
+    ASET (item_properties, ITEM_PROPERTY_SELECTED,
+         menu_item_eval_property (tem));
 
   return 1;
 }
@@ -9196,6 +9193,11 @@ read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
   /* Likewise, for key_translation_map and input-decode-map.  */
   volatile keyremap keytran, indec;
 
+  /* Non-zero if we are trying to map a key by changing an upper-case
+     letter to lower case, or a shifted function key to an unshifted
+     one. */
+  volatile int shift_translated = 0;
+
   /* If we receive a `switch-frame' or `select-window' event in the middle of
      a key sequence, we put it off for later.
      While we're reading, we keep the event here.  */
@@ -9304,8 +9306,7 @@ read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
          defs    = (Lisp_Object *) alloca (2 * sizeof (defs[0]));
          nmaps_allocated = 2;
        }
-      if (!NILP (current_kboard->Voverriding_terminal_local_map))
-       submaps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
+      submaps[nmaps++] = current_kboard->Voverriding_terminal_local_map;
     }
   else if (!NILP (Voverriding_local_map))
     {
@@ -9315,8 +9316,7 @@ read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
          defs    = (Lisp_Object *) alloca (2 * sizeof (defs[0]));
          nmaps_allocated = 2;
        }
-      if (!NILP (Voverriding_local_map))
-       submaps[nmaps++] = Voverriding_local_map;
+      submaps[nmaps++] = Voverriding_local_map;
     }
   else
     {
@@ -9591,7 +9591,8 @@ read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
            }
 
          GROW_RAW_KEYBUF;
-         ASET (raw_keybuf, raw_keybuf_count++, key);
+         ASET (raw_keybuf, raw_keybuf_count, key);
+         raw_keybuf_count++;
        }
 
       /* Clicks in non-text areas get prefixed by the symbol
@@ -9618,7 +9619,7 @@ read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
            {
              Lisp_Object window, posn;
 
-             window = POSN_WINDOW      (EVENT_START (key));
+             window = POSN_WINDOW (EVENT_START (key));
              posn   = POSN_POSN (EVENT_START (key));
 
              if (CONSP (posn)
@@ -10106,13 +10107,14 @@ read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
            XSETINT (new_key, XINT (key) & ~shift_modifier);
          else
            XSETINT (new_key, (DOWNCASE (XINT (key) & ~CHAR_MODIFIER_MASK)
-                              | (XINT (key) & ~CHAR_MODIFIER_MASK)));
+                              | (XINT (key) & CHAR_MODIFIER_MASK)));
 
          /* We have to do this unconditionally, regardless of whether
             the lower-case char is defined in the keymaps, because they
             might get translated through function-key-map.  */
          keybuf[t - 1] = new_key;
          mock_input = max (t, mock_input);
+         shift_translated = 1;
 
          goto replay_sequence;
        }
@@ -10154,6 +10156,7 @@ read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
                 input-decode-map doesn't need to go through it again.  */
              fkey.start = fkey.end = 0;
              keytran.start = keytran.end = 0;
+             shift_translated = 1;
 
              goto replay_sequence;
            }
@@ -10172,7 +10175,13 @@ read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
   if ((dont_downcase_last || first_binding >= nmaps)
       && t > 0
       && t - 1 == original_uppercase_position)
-    keybuf[t - 1] = original_uppercase;
+    {
+      keybuf[t - 1] = original_uppercase;
+      shift_translated = 0;
+    }
+
+  if (shift_translated)
+    Vthis_command_keys_shift_translated = Qt;
 
   /* Occasionally we fabricate events, perhaps by expanding something
      according to function-key-map, or by adding a prefix symbol to a
@@ -10191,8 +10200,6 @@ read_key_sequence (keybuf, bufsize, prompt, dont_downcase_last,
       add_command_key (keybuf[t]);
     }
 
-
-
   UNGCPRO;
   return t;
 }
@@ -10274,7 +10281,7 @@ will read just one key sequence.  */)
       this_single_command_key_start = 0;
     }
 
-#ifdef HAVE_X_WINDOWS
+#ifdef HAVE_WINDOW_SYSTEM
   if (display_hourglass_p)
     cancel_hourglass ();
 #endif
@@ -10286,7 +10293,7 @@ will read just one key sequence.  */)
 #if 0  /* The following is fine for code reading a key sequence and
          then proceeding with a lenghty computation, but it's not good
          for code reading keys in a loop, like an input method.  */
-#ifdef HAVE_X_WINDOWS
+#ifdef HAVE_WINDOW_SYSTEM
   if (display_hourglass_p)
     start_hourglass ();
 #endif
@@ -10334,7 +10341,7 @@ DEFUN ("read-key-sequence-vector", Fread_key_sequence_vector,
       this_single_command_key_start = 0;
     }
 
-#ifdef HAVE_X_WINDOWS
+#ifdef HAVE_WINDOW_SYSTEM
   if (display_hourglass_p)
     cancel_hourglass ();
 #endif
@@ -10343,7 +10350,7 @@ DEFUN ("read-key-sequence-vector", Fread_key_sequence_vector,
                         prompt, ! NILP (dont_downcase_last),
                         ! NILP (can_return_switch_frame), 0);
 
-#ifdef HAVE_X_WINDOWS
+#ifdef HAVE_WINDOW_SYSTEM
   if (display_hourglass_p)
     start_hourglass ();
 #endif
@@ -10466,7 +10473,7 @@ give to the command you invoke, if it asks for an argument.  */)
   Lisp_Object saved_keys, saved_last_point_position_buffer;
   Lisp_Object bindings, value;
   struct gcpro gcpro1, gcpro2, gcpro3;
-#ifdef HAVE_X_WINDOWS
+#ifdef HAVE_WINDOW_SYSTEM
   /* The call to Fcompleting_read wil start and cancel the hourglass,
      but if the hourglass was already scheduled, this means that no
      hourglass will be shown for the actual M-x command itself.
@@ -10506,7 +10513,7 @@ give to the command you invoke, if it asks for an argument.  */)
                               Qt, Qnil, Qextended_command_history, Qnil,
                               Qnil);
 
-#ifdef HAVE_X_WINDOWS
+#ifdef HAVE_WINDOW_SYSTEM
   if (hstarted) start_hourglass ();
 #endif
 
@@ -11585,8 +11592,7 @@ static void
 wipe_kboard (kb)
      KBOARD *kb;
 {
-  if (kb->kbd_macro_buffer)
-    xfree (kb->kbd_macro_buffer);
+  xfree (kb->kbd_macro_buffer);
 }
 
 #ifdef MULTI_KBOARD
@@ -11727,8 +11733,7 @@ struct event_head head_table[] = {
 void
 syms_of_keyboard ()
 {
-  Vpre_help_message = Qnil;
-  staticpro (&Vpre_help_message);
+  pending_funcalls = Qnil;
 
   Vlispy_mouse_stem = build_string ("mouse");
   staticpro (&Vlispy_mouse_stem);
@@ -12084,6 +12089,14 @@ The command can set this variable; whatever is put here
 will be in `last-command' during the following command.  */);
   Vthis_command = Qnil;
 
+  DEFVAR_LISP ("this-command-keys-shift-translated",
+              &Vthis_command_keys_shift_translated,
+              doc: /* Non-nil if the key sequence activating this command was shift-translated.
+Shift-translation occurs when there is no binding for the key sequence
+as entered, but a binding was found by changing an upper-case letter
+to lower-case, or a shifted function key to an unshifted one.  */);
+  Vthis_command_keys_shift_translated = Qnil;
+
   DEFVAR_LISP ("this-original-command", &Vthis_original_command,
               doc: /* The command bound to the current key sequence before remapping.
 It equals `this-command' if the original command was not remapped through
@@ -12240,6 +12253,8 @@ The command loop sets this to nil before each command,
 and tests the value when the command returns.
 Buffer modification stores t in this variable.  */);
   Vdeactivate_mark = Qnil;
+  Qdeactivate_mark = intern ("deactivate-mark");
+  staticpro (&Qdeactivate_mark);
 
   DEFVAR_LISP ("command-hook-internal", &Vcommand_hook_internal,
               doc: /* Temporary storage of pre-command-hook or post-command-hook.  */);