(Ffunction, Fquote): Signal error if not 1 argument.
[bpt/emacs.git] / src / macfns.c
index 8e24120..34f8094 100644 (file)
@@ -1,12 +1,12 @@
 /* Graphical user interface functions for Mac OS.
    Copyright (C) 2000, 2001, 2002, 2003, 2004,
-                 2005, 2006 Free Software Foundation, Inc.
+                 2005, 2006, 2007 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
 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 2, or (at your option)
+the Free Software Foundation; either version 3, or (at your option)
 any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
@@ -51,6 +51,14 @@ Boston, MA 02110-1301, USA.  */
 
 extern void free_frame_menubar ();
 
+#if TARGET_API_MAC_CARBON
+
+/* Carbon version info */
+
+static Lisp_Object Vmac_carbon_version_string;
+
+#endif /* TARGET_API_MAC_CARBON */
+
 /* Non-zero means we're allowed to display an hourglass cursor.  */
 
 int display_hourglass_p;
@@ -210,9 +218,6 @@ void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
 void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
 void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
 void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
-
-extern void mac_get_window_bounds P_ ((struct frame *, Rect *, Rect *));
-
 \f
 
 /* Store the screen positions of frame F into XPTR and YPTR.
@@ -243,7 +248,7 @@ typedef struct colormap_t
   char *name;
 } colormap_t;
 
-colormap_t mac_color_map[] =
+static const colormap_t mac_color_map[] =
 {
   { RGB_TO_ULONG(255, 250, 250), "snow" },
   { RGB_TO_ULONG(248, 248, 255), "ghost white" },
@@ -1001,7 +1006,7 @@ colormap_t mac_color_map[] =
 
 Lisp_Object
 mac_color_map_lookup (colorname)
-     char *colorname;
+     const char *colorname;
 {
   Lisp_Object ret = Qnil;
   int i;
@@ -1024,7 +1029,7 @@ Lisp_Object
 x_to_mac_color (colorname)
      char * colorname;
 {
-  register Lisp_Object tail, ret = Qnil;
+  register Lisp_Object ret = Qnil;
 
   BLOCK_INPUT;
 
@@ -1091,7 +1096,7 @@ x_to_mac_color (colorname)
       char *color;
       unsigned long colorval;
       int i, pos;
-      pos = 0;
+      pos = 16;
 
       colorval = 0;
       color = colorname + 4;
@@ -1127,7 +1132,7 @@ x_to_mac_color (colorname)
          if (value == ULONG_MAX)
            break;
          colorval |= (value << pos);
-         pos += 0x8;
+         pos -= 0x8;
          if (i == 2)
            {
              if (*end != '\0')
@@ -1146,7 +1151,7 @@ x_to_mac_color (colorname)
       char *color;
       unsigned long colorval;
       int i, pos;
-      pos = 0;
+      pos = 16;
 
       colorval = 0;
       color = colorname + 5;
@@ -1168,7 +1173,7 @@ x_to_mac_color (colorname)
          if (val == 0x100)
            val = 0xFF;
          colorval |= (val << pos);
-         pos += 0x8;
+         pos -= 0x8;
          if (i == 2)
            {
              if (*end != '\0')
@@ -1359,7 +1364,6 @@ x_set_mouse_color (f, arg, oldval)
      Lisp_Object arg, oldval;
 {
   struct x_output *x = f->output_data.x;
-  Display *dpy = FRAME_MAC_DISPLAY (f);
   Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
   Cursor hourglass_cursor, horizontal_drag_cursor;
   unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
@@ -1593,7 +1597,7 @@ x_set_icon_name (f, arg, oldval)
       if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
        return;
     }
-  else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
+  else if (!NILP (arg) || NILP (oldval))
     return;
 
   f->icon_name = arg;
@@ -1678,6 +1682,25 @@ x_set_tool_bar_lines (f, value, oldval)
   /* Make sure we redisplay all windows in this frame.  */
   ++windows_or_buffers_changed;
 
+#if USE_MAC_TOOLBAR
+  FRAME_TOOL_BAR_LINES (f) = 0;
+  if (nlines)
+    {
+      FRAME_EXTERNAL_TOOL_BAR (f) = 1;
+      if (FRAME_MAC_P (f) && !IsWindowToolbarVisible (FRAME_MAC_WINDOW (f)))
+       /* Make sure next redisplay shows the tool bar.  */
+       XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = Qt;
+    }
+  else
+    {
+      if (FRAME_EXTERNAL_TOOL_BAR (f))
+        free_frame_tool_bar (f);
+      FRAME_EXTERNAL_TOOL_BAR (f) = 0;
+    }
+
+  return;
+#endif
+
   delta = nlines - FRAME_TOOL_BAR_LINES (f);
 
   /* Don't resize the tool-bar to more than we have room for.  */
@@ -1902,22 +1925,145 @@ x_set_scroll_bar_default_width (f)
 #endif /* not MAC_OSX */
 }
 
-void
-mac_set_scroll_bar_width (f, arg, oldval)
+static void
+mac_set_font (f, arg, oldval)
      struct frame *f;
      Lisp_Object arg, oldval;
 {
+  x_set_font (f, arg, oldval);
+#if USE_MAC_FONT_PANEL
+  {
+    Lisp_Object focus_frame = x_get_focus_frame (f);
+
+    if ((NILP (focus_frame) && f == SELECTED_FRAME ())
+       || XFRAME (focus_frame) == f)
+      {
+       BLOCK_INPUT;
+       mac_set_font_info_for_selection (f, DEFAULT_FACE_ID, 0);
+       UNBLOCK_INPUT;
+      }
+  }
+#endif
+}
+
+#if TARGET_API_MAC_CARBON
+static void
+mac_update_proxy_icon (f)
+     struct frame *f;
+{
+  OSStatus err;
+  Lisp_Object file_name =
+    XBUFFER (XWINDOW (FRAME_SELECTED_WINDOW (f))->buffer)->filename;
+  Window w = FRAME_MAC_WINDOW (f);
+  AliasHandle alias = NULL;
+
+  BLOCK_INPUT;
+
+  err = GetWindowProxyAlias (w, &alias);
+  if (err == errWindowDoesNotHaveProxy && !STRINGP (file_name))
+    goto out;
+
+  if (STRINGP (file_name))
+    {
+      AEDesc desc;
+#ifdef MAC_OSX
+      FSRef fref, fref_proxy;
+#else
+      FSSpec fss, fss_proxy;
+#endif
+      Boolean changed;
+      Lisp_Object encoded_file_name = ENCODE_FILE (file_name);
+
 #ifdef MAC_OSX
-  if (INTEGERP (arg) && XINT (arg) > 0)
+      err = AECoercePtr (TYPE_FILE_NAME, SDATA (encoded_file_name),
+                        SBYTES (encoded_file_name), typeFSRef, &desc);
+#else
+      SetPortWindowPort (w);
+      err = AECoercePtr (TYPE_FILE_NAME, SDATA (encoded_file_name),
+                        SBYTES (encoded_file_name), typeFSS, &desc);
+#endif
+      if (err == noErr)
+       {
+#ifdef MAC_OSX
+         err = AEGetDescData (&desc, &fref, sizeof (FSRef));
+#else
+         err = AEGetDescData (&desc, &fss, sizeof (FSSpec));
+#endif
+         AEDisposeDesc (&desc);
+       }
+      if (err == noErr)
+       {
+         if (alias)
+           {
+             /* (FS)ResolveAlias never sets `changed' to true if
+                `alias' is minimal.  */
+#ifdef MAC_OSX
+             err = FSResolveAlias (NULL, alias, &fref_proxy, &changed);
+             if (err == noErr)
+               err = FSCompareFSRefs (&fref, &fref_proxy);
+#else
+             err = ResolveAlias (NULL, alias, &fss_proxy, &changed);
+             if (err == noErr)
+               err = !(fss.vRefNum == fss_proxy.vRefNum
+                       && fss.parID == fss_proxy.parID
+                       && EqualString (fss.name, fss_proxy.name,
+                                       false, true));
+#endif
+           }
+         if (err != noErr || alias == NULL)
+           {
+             if (alias)
+               DisposeHandle ((Handle) alias);
+#ifdef MAC_OSX
+             err = FSNewAliasMinimal (&fref, &alias);
+#else
+             err = NewAliasMinimal (&fss, &alias);
+#endif
+             changed = true;
+           }
+       }
+      if (err == noErr)
+       if (changed)
+         err = SetWindowProxyAlias (w, alias);
+    }
+
+  if (alias)
+    DisposeHandle ((Handle) alias);
+
+  if (err != noErr || !STRINGP (file_name))
+    RemoveWindowProxy (w);
+
+ out:
+  UNBLOCK_INPUT;
+}
+#endif
+
+void
+mac_update_title_bar (f, save_match_data)
+     struct frame *f;
+     int save_match_data;
+{
+#if TARGET_API_MAC_CARBON
+  struct window *w;
+  int modified_p;
+
+  if (!FRAME_MAC_P (f))
+    return;
+
+  w = XWINDOW (FRAME_SELECTED_WINDOW (f));
+  modified_p = (BUF_SAVE_MODIFF (XBUFFER (w->buffer))
+               < BUF_MODIFF (XBUFFER (w->buffer)));
+  if (windows_or_buffers_changed
+      /* Minibuffer modification status shown in the close button is
+        confusing.  */
+      || (!MINI_WINDOW_P (w)
+         && (modified_p != !NILP (w->last_had_star))))
     {
-      if (XINT (arg) < (MAC_AQUA_SMALL_VERTICAL_SCROLL_BAR_WIDTH
-                       + MAC_AQUA_VERTICAL_SCROLL_BAR_WIDTH) / 2)
-       XSETINT (arg, MAC_AQUA_SMALL_VERTICAL_SCROLL_BAR_WIDTH);
-      else
-       XSETINT (arg, MAC_AQUA_VERTICAL_SCROLL_BAR_WIDTH);
+      SetWindowModified (FRAME_MAC_WINDOW (f),
+                        !MINI_WINDOW_P (w) && modified_p);
+      mac_update_proxy_icon (f);
     }
 #endif
-  x_set_scroll_bar_width (f, arg, oldval);
 }
 
 \f
@@ -2120,8 +2266,10 @@ mac_window (f)
 #if TARGET_API_MAC_CARBON
   CreateNewWindow (kDocumentWindowClass,
                   kWindowStandardDocumentAttributes
-                  /* | kWindowToolbarButtonAttribute */,
-                  &r, &FRAME_MAC_WINDOW (f));
+#ifdef MAC_OSX
+                  | kWindowToolbarButtonAttribute
+#endif
+                  , &r, &FRAME_MAC_WINDOW (f));
   if (FRAME_MAC_WINDOW (f))
     {
       SetWRefCon (FRAME_MAC_WINDOW (f), (long) f->output_data.mac);
@@ -2131,11 +2279,11 @@ mac_window (f)
          FRAME_MAC_WINDOW (f) = NULL;
        }
     }
-#else
+#else  /* !TARGET_API_MAC_CARBON */
   FRAME_MAC_WINDOW (f)
     = NewCWindow (NULL, &r, "\p", false, zoomDocProc,
-                 (WindowPtr) -1, 1, (long) f->output_data.mac);
-#endif
+                 (WindowRef) -1, 1, (long) f->output_data.mac);
+#endif  /* !TARGET_API_MAC_CARBON */
   /* so that update events can find this mac_output struct */
   f->output_data.mac->mFP = f;  /* point back to emacs frame */
 
@@ -2155,6 +2303,16 @@ mac_window (f)
     XSetWindowBackground (FRAME_MAC_DISPLAY(f), FRAME_MAC_WINDOW (f),
                          FRAME_BACKGROUND_PIXEL (f));
 
+#if USE_MAC_TOOLBAR
+  /* At the moment, the size of the tool bar is not yet known.  We
+     record the gravity value of the newly created window and use it
+     to adjust the position of the window (especially for a negative
+     specification of its vertical position) when the tool bar is
+     first redisplayed.  */
+  if (FRAME_EXTERNAL_TOOL_BAR (f))
+    f->output_data.mac->toolbar_win_gravity = f->win_gravity;
+#endif
+
   validate_x_resource_name ();
 
   /* x_set_name normally ignores requests to set the name if the
@@ -2387,7 +2545,6 @@ This function is an internal primitive--use `make-frame' instead.  */)
   struct mac_display_info *dpyinfo = NULL;
   Lisp_Object parent;
   struct kboard *kb;
-  static int x_frame_count = 2;  /* begins at 2 because terminal frame is F1 */
 
   check_mac ();
 
@@ -2451,7 +2608,6 @@ This function is an internal primitive--use `make-frame' instead.  */)
   f->output_data.mac = (struct mac_output *) xmalloc (sizeof (struct mac_output));
   bzero (f->output_data.mac, sizeof (struct mac_output));
   FRAME_FONTSET (f) = -1;
-  record_unwind_protect (unwind_create_frame, frame);
 
   f->icon_name
     = mac_get_arg (parms, Qicon_name, "iconName", "Title", RES_TYPE_STRING);
@@ -2459,6 +2615,9 @@ This function is an internal primitive--use `make-frame' instead.  */)
     f->icon_name = Qnil;
 
 /*   FRAME_MAC_DISPLAY_INFO (f) = dpyinfo; */
+
+  /* With FRAME_MAC_DISPLAY_INFO set up, this unwind-protect is safe.  */
+  record_unwind_protect (unwind_create_frame, frame);
 #if GLYPH_DEBUG
   image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
   dpyinfo_refcount = dpyinfo->reference_count;
@@ -2531,8 +2690,7 @@ This function is an internal primitive--use `make-frame' instead.  */)
       error ("Cannot find any usable font");
     UNBLOCK_INPUT;
 
-    x_default_parameter (f, parms, Qfont, font,
-                        "font", "Font", RES_TYPE_STRING);
+    x_set_frame_parameters (f, Fcons (Fcons (Qfont, font), Qnil));
   }
 
   x_default_parameter (f, parms, Qborder_width, make_number (0),
@@ -2713,27 +2871,45 @@ FRAME nil means use the selected frame.  */)
      (frame)
      Lisp_Object frame;
 {
+  OSErr err;
+  ProcessSerialNumber front_psn;
+  static const ProcessSerialNumber current_psn = {0, kCurrentProcess};
+  Boolean front_p;
   struct frame *f = check_x_frame (frame);
-  struct mac_display_info *dpyinfo = FRAME_MAC_DISPLAY_INFO (f);
 
-  if (dpyinfo->x_focus_frame != f)
-    {
-      BLOCK_INPUT;
+  BLOCK_INPUT;
+  /* Move the current process to the foreground if it is not.  Don't
+     call SetFrontProcess if the current process is already running in
+     the foreground so as not to change the z-order of windows.  */
+  err = GetFrontProcess (&front_psn);
+  if (err == noErr)
+    err = SameProcess (&front_psn, &current_psn, &front_p);
+  if (err == noErr)
+    if (!front_p)
+      {
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
+       if (FrontNonFloatingWindow () == FRAME_MAC_WINDOW (f))
+         SetFrontProcessWithOptions (&current_psn,
+                                     kSetFrontProcessFrontWindowOnly);
+       else
+#endif
+         SetFrontProcess (&current_psn);
+      }
+
 #ifdef MAC_OSX
-      ActivateWindow (ActiveNonFloatingWindow (), false);
-      ActivateWindow (FRAME_MAC_WINDOW (f), true);
+  ActivateWindow (ActiveNonFloatingWindow (), false);
+  ActivateWindow (FRAME_MAC_WINDOW (f), true);
 #else
 #if !TARGET_API_MAC_CARBON
-      /* SelectWindow (Non-Carbon) does not issue deactivate events if
-        the possibly inactive window that is to be selected is
-        already the frontmost one.  */
-      SendBehind (FRAME_MAC_WINDOW (f), NULL);
+  /* SelectWindow (Non-Carbon) does not issue deactivate events if the
+     possibly inactive window that is to be selected is already the
+     frontmost one.  */
+  SendBehind (FRAME_MAC_WINDOW (f), NULL);
 #endif
-      /* This brings the window to the front.  */
-      SelectWindow (FRAME_MAC_WINDOW (f));
+  /* This brings the window to the front.  */
+  SelectWindow (FRAME_MAC_WINDOW (f));
 #endif
-      UNBLOCK_INPUT;
-    }
+  UNBLOCK_INPUT;
 
   return Qnil;
 }
@@ -2766,14 +2942,9 @@ DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
   CHECK_STRING (color);
 
   if (mac_defined_color (f, SDATA (color), &foo, 0))
-    {
-      Lisp_Object rgb[3];
-
-      rgb[0] = make_number (foo.red);
-      rgb[1] = make_number (foo.green);
-      rgb[2] = make_number (foo.blue);
-      return Flist (3, rgb);
-    }
+    return list3 (make_number (foo.red),
+                 make_number (foo.green),
+                 make_number (foo.blue));
   else
     return Qnil;
 }
@@ -2889,7 +3060,7 @@ If omitted or nil, that stands for the selected frame's display.  */)
      (display)
      Lisp_Object display;
 {
-  return build_string ("Apple Computers");
+  return build_string ("Apple Inc.");
 }
 
 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
@@ -2910,23 +3081,25 @@ If omitted or nil, that stands for the selected frame's display.  */)
   BLOCK_INPUT;
   err = Gestalt (gestaltSystemVersion, &response);
   if (err == noErr)
-    if (response >= 0x00001040)
-      {
-       err = Gestalt ('sys1', &major); /* gestaltSystemVersionMajor */
-       if (err == noErr)
-         err = Gestalt ('sys2', &minor); /* gestaltSystemVersionMinor */
-       if (err == noErr)
-         err = Gestalt ('sys3', &bugfix); /* gestaltSystemVersionBugFix */
-      }
-    else
-      {
-       bugfix = response & 0xf;
-       response >>= 4;
-       minor = response & 0xf;
-       response >>= 4;
-       /* convert BCD to int */
-       major = response - (response >> 4) * 6;
-      }
+    {
+      if (response >= 0x00001040)
+       {
+         err = Gestalt (gestaltSystemVersionMajor, &major);
+         if (err == noErr)
+           err = Gestalt (gestaltSystemVersionMinor, &minor);
+         if (err == noErr)
+           err = Gestalt (gestaltSystemVersionBugFix, &bugfix);
+       }
+      else
+       {
+         bugfix = response & 0xf;
+         response >>= 4;
+         minor = response & 0xf;
+         response >>= 4;
+         /* convert BCD to int */
+         major = response - (response >> 4) * 6;
+       }
+    }
   UNBLOCK_INPUT;
 
   if (err != noErr)
@@ -2957,11 +3130,33 @@ If omitted or nil, that stands for the selected frame's display.  */)
      (display)
      Lisp_Object display;
 {
-  /* MAC_TODO: this is an approximation, and only of the main display */
-
   struct mac_display_info *dpyinfo = check_x_display_info (display);
+  float mm_per_pixel;
+
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1030
+#if MAC_OS_X_VERSION_MIN_REQUIRED == 1020
+  if (CGDisplayScreenSize != NULL)
+#endif
+    {
+      CGSize size;
+
+      BLOCK_INPUT;
+      size = CGDisplayScreenSize (kCGDirectMainDisplay);
+      mm_per_pixel = size.height / CGDisplayPixelsHigh (kCGDirectMainDisplay);
+      UNBLOCK_INPUT;
+    }
+#if MAC_OS_X_VERSION_MIN_REQUIRED == 1020
+  else                         /* CGDisplayScreenSize == NULL */
+#endif
+#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1030  */
+#if MAC_OS_X_VERSION_MAX_ALLOWED < 1030 || MAC_OS_X_VERSION_MIN_REQUIRED == 1020
+    {
+      /* This is an approximation.  */
+      mm_per_pixel = 25.4f / dpyinfo->resy;
+    }
+#endif
 
-  return make_number ((int) (dpyinfo->height * 25.4 / dpyinfo->resy));
+  return make_number ((int) (dpyinfo->height * mm_per_pixel + 0.5f));
 }
 
 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
@@ -2972,11 +3167,33 @@ If omitted or nil, that stands for the selected frame's display.  */)
      (display)
      Lisp_Object display;
 {
-  /* MAC_TODO: this is an approximation, and only of the main display */
-
   struct mac_display_info *dpyinfo = check_x_display_info (display);
+  float mm_per_pixel;
 
-  return make_number ((int) (dpyinfo->width * 25.4 / dpyinfo->resx));
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1030
+#if MAC_OS_X_VERSION_MIN_REQUIRED == 1020
+  if (CGDisplayScreenSize != NULL)
+#endif
+    {
+      CGSize size;
+
+      BLOCK_INPUT;
+      size = CGDisplayScreenSize (kCGDirectMainDisplay);
+      mm_per_pixel = size.width / CGDisplayPixelsWide (kCGDirectMainDisplay);
+      UNBLOCK_INPUT;
+    }
+#if MAC_OS_X_VERSION_MIN_REQUIRED == 1020
+  else                         /* CGDisplayScreenSize == NULL */
+#endif
+#endif /* MAC_OS_X_VERSION_MAX_ALLOWED >= 1030  */
+#if MAC_OS_X_VERSION_MAX_ALLOWED < 1030 || MAC_OS_X_VERSION_MIN_REQUIRED == 1020
+    {
+      /* This is an approximation.  */
+      mm_per_pixel = 25.4f / dpyinfo->resx;
+    }
+#endif
+
+  return make_number ((int) (dpyinfo->width * mm_per_pixel + 0.5f));
 }
 
 DEFUN ("x-display-backing-store", Fx_display_backing_store,
@@ -3210,6 +3427,14 @@ DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
   return Qnil;
 }
 
+/* x_sync is a no-op on Mac.  */
+
+void
+x_sync (f)
+     FRAME_PTR f;
+{
+}
+
 \f
 /***********************************************************************
                            Window properties
@@ -3462,6 +3687,9 @@ show_hourglass (timer)
          if (FRAME_LIVE_P (f) && FRAME_MAC_P (f)
              && FRAME_MAC_WINDOW (f) != tip_window)
            {
+#if USE_CG_DRAWING
+             mac_prepare_for_quickdraw (f);
+#endif
              if (!f->output_data.mac->hourglass_control)
                {
                  Window w = FRAME_MAC_WINDOW (f);
@@ -3506,7 +3734,12 @@ hide_hourglass ()
          if (FRAME_MAC_P (f)
              /* Watch out for newly created frames.  */
              && f->output_data.mac->hourglass_control)
-           HideControl (f->output_data.mac->hourglass_control);
+           {
+#if USE_CG_DRAWING
+             mac_prepare_for_quickdraw (f);
+#endif
+             HideControl (f->output_data.mac->hourglass_control);
+           }
        }
 
       hourglass_shown_p = 0;
@@ -3869,8 +4102,12 @@ compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
       Point mouse_pos;
 
       BLOCK_INPUT;
+#if TARGET_API_MAC_CARBON
+      GetGlobalMouse (&mouse_pos);
+#else
       GetMouse (&mouse_pos);
       LocalToGlobal (&mouse_pos);
+#endif
       *root_x = mouse_pos.h;
       *root_y = mouse_pos.v;
       UNBLOCK_INPUT;
@@ -3910,6 +4147,8 @@ DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
        doc: /* Show STRING in a "tooltip" window on frame FRAME.
 A tooltip window is a small window displaying a string.
 
+This is an internal function; Lisp code should call `tooltip-show'.
+
 FRAME nil or omitted means use the selected frame.
 
 PARMS is an optional list of frame parameters which can be used to
@@ -4182,14 +4421,14 @@ If ONLY-DIR-P is non-nil, the user can only select directories.  */)
      (prompt, dir, default_filename, mustmatch, only_dir_p)
      Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p;
 {
-  struct frame *f = SELECTED_FRAME ();
   Lisp_Object file = Qnil;
   int count = SPECPDL_INDEX ();
   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
   char filename[MAXPATHLEN];
-  int default_filter_index = 1; /* 1: All Files, 2: Directories only  */
   static NavEventUPP mac_nav_event_callbackUPP = NULL;
 
+  check_mac ();
+
   GCPRO6 (prompt, dir, default_filename, mustmatch, file, only_dir_p);
   CHECK_STRING (prompt);
   CHECK_STRING (dir);
@@ -4312,7 +4551,7 @@ If ONLY-DIR-P is non-nil, the user can only select directories.  */)
                      filename[len++] = '/';
                    CFStringGetCString(reply.saveFileName, filename+len,
                                       sizeof (filename) - len,
-#if MAC_OSX
+#ifdef MAC_OSX
                                       kCFStringEncodingUTF8
 #else
                                       CFStringGetSystemEncoding ()
@@ -4359,6 +4598,77 @@ mac_nav_event_callback (selector, parms, data)
 }
 #endif
 \f
+/***********************************************************************
+                               Fonts
+ ***********************************************************************/
+
+DEFUN ("mac-clear-font-name-table", Fmac_clear_font_name_table,
+       Smac_clear_font_name_table, 0, 0, 0,
+       doc: /* Clear the font name table.  */)
+     ()
+{
+  check_mac ();
+  mac_clear_font_name_table ();
+  return Qnil;
+}
+
+#if USE_MAC_FONT_PANEL
+DEFUN ("mac-set-font-panel-visible-p", Fmac_set_font_panel_visible_p,
+       Smac_set_font_panel_visible_p, 1, 1, 0,
+  doc: /* Make the font panel visible if and only if FLAG is non-nil.
+This is for internal use only.  Use `mac-font-panel-mode' instead.  */)
+     (flag)
+     Lisp_Object flag;
+{
+  OSStatus err = noErr;
+
+  check_mac ();
+
+  BLOCK_INPUT;
+  if (NILP (flag) != !mac_font_panel_visible_p ())
+    {
+      err = mac_show_hide_font_panel ();
+      if (err == noErr && !NILP (flag))
+       {
+         Lisp_Object focus_frame = x_get_focus_frame (SELECTED_FRAME ());
+         struct frame *f = (NILP (focus_frame) ? SELECTED_FRAME ()
+                            : XFRAME (focus_frame));
+
+         mac_set_font_info_for_selection (f, DEFAULT_FACE_ID, 0);
+       }
+    }
+  UNBLOCK_INPUT;
+
+  if (err != noErr)
+    error ("Cannot change visibility of the font panel");
+  return Qnil;
+}
+#endif
+
+#if USE_ATSUI
+extern Lisp_Object mac_atsu_font_face_attributes P_ ((ATSUFontID));
+
+DEFUN ("mac-atsu-font-face-attributes", Fmac_atsu_font_face_attributes,
+       Smac_atsu_font_face_attributes, 1, 1, 0,
+  doc: /* Return plist of face attributes and values for ATSU font ID.
+ID is specified by either an integer or a float.  */)
+     (id)
+     Lisp_Object id;
+{
+  ATSUFontID font_id;
+  Lisp_Object result;
+
+  check_mac ();
+  CHECK_NUMBER_OR_FLOAT(id);
+  font_id = NUMBERP (id) ? XINT (id) : (ATSUFontID) XFLOAT (id);
+  BLOCK_INPUT;
+  result = mac_atsu_font_face_attributes (font_id);
+  UNBLOCK_INPUT;
+  return result;
+}
+#endif
+
+\f
 /***********************************************************************
                            Initialization
  ***********************************************************************/
@@ -4375,7 +4685,7 @@ frame_parm_handler mac_frame_parm_handlers[] =
   x_set_border_width,
   x_set_cursor_color,
   x_set_cursor_type,
-  x_set_font,
+  mac_set_font,
   x_set_foreground_color,
   x_set_icon_name,
   0, /* MAC_TODO: x_set_icon_type, */
@@ -4383,7 +4693,7 @@ frame_parm_handler mac_frame_parm_handlers[] =
   x_set_menu_bar_lines,
   x_set_mouse_color,
   x_explicitly_set_name,
-  mac_set_scroll_bar_width,
+  x_set_scroll_bar_width,
   x_set_title,
   x_set_unsplittable,
   x_set_vertical_scroll_bars,
@@ -4511,6 +4821,22 @@ such a font.  This is especially effective for such large fonts as
 Chinese, Japanese, and Korean.  */);
   Vx_pixel_size_width_font_regexp = Qnil;
 
+#if TARGET_API_MAC_CARBON
+  DEFVAR_LISP ("mac-carbon-version-string", &Vmac_carbon_version_string,
+    doc: /* Version info for Carbon API.  */);
+  {
+    OSErr err;
+    UInt32 response;
+    char carbon_version[16] = "Unknown";
+
+    err = Gestalt (gestaltCarbonVersion, &response);
+    if (err == noErr)
+      sprintf (carbon_version, "%u.%u.%u",
+              (response >> 8) & 0xf, (response >> 4) & 0xf, response & 0xf);
+    Vmac_carbon_version_string = build_string (carbon_version);
+  }
+#endif /* TARGET_API_MAC_CARBON */
+
   /* X window properties.  */
   defsubr (&Sx_change_window_property);
   defsubr (&Sx_delete_window_property);
@@ -4551,7 +4877,7 @@ Chinese, Japanese, and Korean.  */);
   load_font_func = x_load_font;
   find_ccl_program_func = x_find_ccl_program;
   query_font_func = x_query_font;
-  set_frame_fontset_func = x_set_font;
+  set_frame_fontset_func = mac_set_font;
   check_window_system_func = check_mac;
 
   hourglass_atimer = NULL;
@@ -4569,6 +4895,13 @@ Chinese, Japanese, and Korean.  */);
 
 #if TARGET_API_MAC_CARBON
   defsubr (&Sx_file_dialog);
+#endif
+  defsubr (&Smac_clear_font_name_table);
+#if USE_MAC_FONT_PANEL
+  defsubr (&Smac_set_font_panel_visible_p);
+#endif
+#if USE_ATSUI
+  defsubr (&Smac_atsu_font_face_attributes);
 #endif
 }