* w32term.c (pfnGetFontUnicodeRanges): Remove unused function pointer.
[bpt/emacs.git] / src / macterm.c
index 842e184..b208573 100644 (file)
@@ -1,13 +1,13 @@
 /* Implementation of GUI terminal on the Mac OS.
-   Copyright (C) 2000, 2001, 2002, 2003, 2004,
-                 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
+   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
+                 200 Free Software Foundation, Inc.
 
 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
@@ -15,9 +15,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/>.  */
 
 /* Contributed by Andrew Choi (akochoi@mac.com).  */
 
@@ -335,7 +333,7 @@ static struct terminal *mac_create_terminal P_ ((struct mac_display_info *dpyinf
 static int max_fringe_bmp = 0;
 static CGImageRef *fringe_bmp = 0;
 
-static CGColorSpaceRef mac_cg_color_space_rgb;
+CGColorSpaceRef mac_cg_color_space_rgb;
 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1030
 static CGColorRef mac_cg_color_black;
 #endif
@@ -351,7 +349,7 @@ init_cg_color ()
   if (CGColorGetTypeID != NULL)
 #endif
     {
-      float rgba[] = {0.0f, 0.0f, 0.0f, 1.0f};
+      CGFloat rgba[] = {0.0f, 0.0f, 0.0f, 1.0f};
 
       mac_cg_color_black = CGColorCreate (mac_cg_color_space_rgb, rgba);
     }
@@ -460,7 +458,7 @@ mac_draw_line (f, gc, x1, y1, x2, y2)
 {
 #if USE_CG_DRAWING
   CGContextRef context;
-  float gx1 = x1, gy1 = y1, gx2 = x2, gy2 = y2;
+  CGFloat gx1 = x1, gy1 = y1, gx2 = x2, gy2 = y2;
 
   if (y1 != y2)
     gx1 += 0.5f, gx2 += 0.5f;
@@ -508,6 +506,48 @@ XDrawLine (display, p, gc, x1, y1, x2, y2)
      GC gc;
      int x1, y1, x2, y2;
 {
+#if USE_MAC_IMAGE_IO
+  CGContextRef context;
+  XImagePtr ximg = p;
+  CGColorSpaceRef color_space;
+  CGImageAlphaInfo alpha_info;
+  CGFloat gx1 = x1, gy1 = y1, gx2 = x2, gy2 = y2;
+
+  if (y1 != y2)
+    gx1 += 0.5f, gx2 += 0.5f;
+  if (x1 != x2)
+    gy1 += 0.5f, gy2 += 0.5f;
+
+  if (ximg->bits_per_pixel == 32)
+    {
+      color_space = mac_cg_color_space_rgb;
+      alpha_info = (kCGImageAlphaNoneSkipFirst
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1040
+                   | kCGBitmapByteOrder32Host
+#endif
+                   );
+    }
+  else
+    {
+      color_space = NULL;
+      alpha_info = kCGImageAlphaOnly;
+    }
+  if (color_space == NULL)
+    return;
+  context = CGBitmapContextCreate (ximg->data, ximg->width,
+                                  ximg->height, 8,
+                                  ximg->bytes_per_line, color_space,
+                                  alpha_info);
+  if (ximg->bits_per_pixel == 32)
+    CG_SET_STROKE_COLOR_WITH_GC_FOREGROUND (context, gc);
+  else
+    CGContextSetGrayStrokeColor (context, gc->xgcv.foreground / 255.0f, 1.0);
+  CGContextMoveToPoint (context, gx1, gy1);
+  CGContextAddLineToPoint (context, gx2, gy2);
+  CGContextClosePath (context);
+  CGContextStrokePath (context);
+  CGContextRelease (context);
+#else
   CGrafPtr old_port;
   GDHandle old_gdh;
 
@@ -537,6 +577,7 @@ XDrawLine (display, p, gc, x1, y1, x2, y2)
   UnlockPixels (GetGWorldPixMap (p));
 
   SetGWorld (old_port, old_gdh);
+#endif
 }
 
 
@@ -553,7 +594,7 @@ mac_erase_rectangle (f, gc, x, y, width, height)
 
       context = mac_begin_cg_clip (f, gc);
       CG_SET_FILL_COLOR_WITH_GC_BACKGROUND (context, gc);
-      CGContextFillRect (context, CGRectMake (x, y, width, height));
+      CGContextFillRect (context, mac_rect_make (f, x, y, width, height));
       mac_end_cg_clip (f);
     }
 #else
@@ -632,8 +673,8 @@ mac_draw_cg_image (image, f, gc, src_x, src_y, width, height,
      int dest_x, dest_y, overlay_p;
 {
   CGContextRef context;
-  float port_height = FRAME_PIXEL_HEIGHT (f);
-  CGRect dest_rect = CGRectMake (dest_x, dest_y, width, height);
+  CGFloat port_height = FRAME_PIXEL_HEIGHT (f);
+  CGRect dest_rect = mac_rect_make (f, dest_x, dest_y, width, height);
 
   context = mac_begin_cg_clip (f, gc);
   if (!overlay_p)
@@ -647,11 +688,11 @@ mac_draw_cg_image (image, f, gc, src_x, src_y, width, height,
   if (CGImageIsMask (image))
     CG_SET_FILL_COLOR_WITH_GC_FOREGROUND (context, gc);
   CGContextDrawImage (context,
-                     CGRectMake (dest_x - src_x,
-                                 port_height - (dest_y - src_y
-                                                + CGImageGetHeight (image)),
-                                 CGImageGetWidth (image),
-                                 CGImageGetHeight (image)),
+                     mac_rect_make (f, dest_x - src_x,
+                                    port_height - (dest_y - src_y
+                                                   + CGImageGetHeight (image)),
+                                    CGImageGetWidth (image),
+                                    CGImageGetHeight (image)),
                      image);
   mac_end_cg_clip (f);
 }
@@ -744,16 +785,28 @@ mac_free_bitmap (bitmap)
 Pixmap
 XCreatePixmap (display, w, width, height, depth)
      Display *display;
-     WindowRef w;
+     Window w;
      unsigned int width, height;
      unsigned int depth;
 {
+#if USE_MAC_IMAGE_IO
+  XImagePtr ximg;
+
+  ximg = xmalloc (sizeof (*ximg));
+  ximg->width = width;
+  ximg->height = height;
+  ximg->bits_per_pixel = depth == 1 ? 8 : 32;
+  ximg->bytes_per_line = width * (ximg->bits_per_pixel / 8);
+  ximg->data = xmalloc (ximg->bytes_per_line * height);
+  return ximg;
+#else
   Pixmap pixmap;
   Rect r;
   QDErr err;
 
+#ifdef MAC_OS8
   SetPortWindowPort (w);
-
+#endif
   SetRect (&r, 0, 0, width, height);
 #if !defined (WORDS_BIG_ENDIAN) && USE_CG_DRAWING
   if (depth == 1)
@@ -767,13 +820,14 @@ XCreatePixmap (display, w, width, height, depth)
   if (err != noErr)
     return NULL;
   return pixmap;
+#endif
 }
 
 
 Pixmap
 XCreatePixmapFromBitmapData (display, w, data, width, height, fg, bg, depth)
      Display *display;
-     WindowRef w;
+     Window w;
      char *data;
      unsigned int width, height;
      unsigned long fg, bg;
@@ -781,6 +835,38 @@ XCreatePixmapFromBitmapData (display, w, data, width, height, fg, bg, depth)
 {
   Pixmap pixmap;
   BitMap bitmap;
+#if USE_MAC_IMAGE_IO
+  CGDataProviderRef provider;
+  CGImageRef image_mask;
+  CGContextRef context;
+
+  pixmap = XCreatePixmap (display, w, width, height, depth);
+  if (pixmap == NULL)
+    return NULL;
+
+  mac_create_bitmap_from_bitmap_data (&bitmap, data, width, height);
+  provider = CGDataProviderCreateWithData (NULL, bitmap.baseAddr,
+                                          bitmap.rowBytes * height, NULL);
+  image_mask = CGImageMaskCreate (width, height, 1, 1, bitmap.rowBytes,
+                                 provider, NULL, 0);
+  CGDataProviderRelease (provider);
+
+  context = CGBitmapContextCreate (pixmap->data, width, height, 8,
+                                  pixmap->bytes_per_line,
+                                  mac_cg_color_space_rgb,
+                                  kCGImageAlphaNoneSkipFirst
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1040
+                                  | kCGBitmapByteOrder32Host
+#endif
+                                  );
+
+  CG_SET_FILL_COLOR (context, fg);
+  CGContextFillRect (context, CGRectMake (0, 0, width, height));
+  CG_SET_FILL_COLOR (context, bg);
+  CGContextDrawImage (context, CGRectMake (0, 0, width, height), image_mask);
+  CGContextRelease (context);
+  CGImageRelease (image_mask);
+#else
   CGrafPtr old_port;
   GDHandle old_gdh;
   static GC gc = NULL;
@@ -809,6 +895,7 @@ XCreatePixmapFromBitmapData (display, w, data, width, height, fg, bg, depth)
 #endif /* not TARGET_API_MAC_CARBON */
   UnlockPixels (GetGWorldPixMap (pixmap));
   SetGWorld (old_port, old_gdh);
+#endif
   mac_free_bitmap (&bitmap);
 
   return pixmap;
@@ -820,7 +907,15 @@ XFreePixmap (display, pixmap)
      Display *display;
      Pixmap pixmap;
 {
+#if USE_MAC_IMAGE_IO
+  if (pixmap)
+    {
+      xfree (pixmap->data);
+      xfree (pixmap);
+    }
+#else
   DisposeGWorld (pixmap);
+#endif
 }
 
 
@@ -838,7 +933,7 @@ mac_fill_rectangle (f, gc, x, y, width, height)
 
   context = mac_begin_cg_clip (f, gc);
   CG_SET_FILL_COLOR_WITH_GC_FOREGROUND (context, gc);
-  CGContextFillRect (context, CGRectMake (x, y, width, height));
+  CGContextFillRect (context, mac_rect_make (f, x, y, width, height));
   mac_end_cg_clip (f);
 #else
   Rect r;
@@ -887,7 +982,26 @@ mac_invert_rectangle (f, x, y, width, height)
      int x, y;
      unsigned int width, height;
 {
-  Rect r;
+#if USE_CG_DRAWING && MAC_OS_X_VERSION_MAX_ALLOWED >= 1040
+#if MAC_OS_X_VERSION_MIN_REQUIRED < 1040 && MAC_OS_X_VERSION_MIN_REQUIRED >= 1020
+  if (CGContextSetBlendMode != NULL)
+#endif
+    {
+      CGContextRef context;
+
+      context = mac_begin_cg_clip (f, NULL);
+      CGContextSetRGBFillColor (context, 1.0f, 1.0f, 1.0f, 1.0f);
+      CGContextSetBlendMode (context, kCGBlendModeDifference);
+      CGContextFillRect (context, mac_rect_make (f, x, y, width, height));
+      mac_end_cg_clip (f);
+    }
+#if MAC_OS_X_VERSION_MIN_REQUIRED < 1040 && MAC_OS_X_VERSION_MIN_REQUIRED >= 1020
+  else                         /* CGContextSetBlendMode == NULL */
+#endif
+#endif /* USE_CG_DRAWING && MAC_OS_X_VERSION_MAX_ALLOWED >= 1040 */
+#if !USE_CG_DRAWING || MAC_OS_X_VERSION_MAX_ALLOWED < 1040 || (MAC_OS_X_VERSION_MIN_REQUIRED < 1040 && MAC_OS_X_VERSION_MIN_REQUIRED >= 1020)
+    {
+      Rect r;
 
 #if USE_CG_DRAWING
   mac_prepare_for_quickdraw (f);
@@ -1018,7 +1132,7 @@ mac_draw_image_string_atsui (f, gc, x, y, buf, nchars, bg_width,
   else
     {
       static CGContextRef context;
-      float port_height = FRAME_PIXEL_HEIGHT (f);
+      CGFloat port_height = FRAME_PIXEL_HEIGHT (f);
       static const ATSUAttributeTag tags[] = {kATSUCGContextTag};
       static const ByteCount sizes[] = {sizeof (CGContextRef)};
       static const ATSUAttributeValuePtr values[] = {&context};
@@ -1042,9 +1156,10 @@ mac_draw_image_string_atsui (f, gc, x, y, buf, nchars, bg_width,
            {
              CG_SET_FILL_COLOR_WITH_GC_BACKGROUND (context, gc);
              CGContextFillRect (context,
-                                CGRectMake (x, y - FONT_BASE (GC_FONT (gc)),
-                                            bg_width,
-                                            FONT_HEIGHT (GC_FONT (gc))));
+                                mac_rect_make (f,
+                                               x, y - FONT_BASE (GC_FONT (gc)),
+                                               bg_width,
+                                               FONT_HEIGHT (GC_FONT (gc))));
            }
          CGContextScaleCTM (context, 1, -1);
          CGContextTranslateCTM (context, 0, -port_height);
@@ -1388,7 +1503,7 @@ mac_draw_image_string_cg (f, gc, x, y, buf, nchars, bg_width, overstrike_p)
      XChar2b *buf;
      int nchars, bg_width, overstrike_p;
 {
-  float port_height, gx, gy;
+  CGFloat port_height, gx, gy;
   int i;
   CGContextRef context;
   CGGlyph *glyphs;
@@ -1430,8 +1545,8 @@ mac_draw_image_string_cg (f, gc, x, y, buf, nchars, bg_width, overstrike_p)
          CG_SET_FILL_COLOR_WITH_GC_BACKGROUND (context, gc);
          CGContextFillRect
            (context,
-            CGRectMake (gx, y - FONT_BASE (GC_FONT (gc)),
-                        bg_width, FONT_HEIGHT (GC_FONT (gc))));
+            mac_rect_make (f, gx, y - FONT_BASE (GC_FONT (gc)),
+                           bg_width, FONT_HEIGHT (GC_FONT (gc))));
        }
       CGContextScaleCTM (context, 1, -1);
       CGContextTranslateCTM (context, 0, -port_height);
@@ -1733,7 +1848,7 @@ XSetForeground (display, gc, color)
            }
          else
            {
-             float rgba[4];
+             CGFloat rgba[4];
 
              rgba[0] = gc->fore_color.red / 65535.0f;
              rgba[1] = gc->fore_color.green / 65535.0f;
@@ -1774,7 +1889,7 @@ XSetBackground (display, gc, color)
            }
          else
            {
-             float rgba[4];
+             CGFloat rgba[4];
 
              rgba[0] = gc->back_color.red / 65535.0f;
              rgba[1] = gc->back_color.green / 65535.0f;
@@ -1803,8 +1918,8 @@ XSetFont (display, gc, font)
 /* Mac replacement for XSetClipRectangles.  */
 
 static void
-mac_set_clip_rectangles (display, gc, rectangles, n)
-     Display *display;
+mac_set_clip_rectangles (f, gc, rectangles, n)
+     struct frame *f;
      GC gc;
      Rect *rectangles;
      int n;
@@ -1836,9 +1951,9 @@ mac_set_clip_rectangles (display, gc, rectangles, n)
     {
       Rect *rect = rectangles + i;
 
-      gc->clip_rects[i] = CGRectMake (rect->left, rect->top,
-                                     rect->right - rect->left,
-                                     rect->bottom - rect->top);
+      gc->clip_rects[i] = mac_rect_make (f, rect->left, rect->top,
+                                        rect->right - rect->left,
+                                        rect->bottom - rect->top);
     }
 #endif
 }
@@ -1847,8 +1962,8 @@ mac_set_clip_rectangles (display, gc, rectangles, n)
 /* Mac replacement for XSetClipMask.  */
 
 static INLINE void
-mac_reset_clip_rectangles (display, gc)
-     Display *display;
+mac_reset_clip_rectangles (f, gc)
+     struct frame *f;
      GC gc;
 {
   gc->n_clip_rects = 0;
@@ -2338,7 +2453,7 @@ x_draw_fringe_bitmap (w, row, p)
       XSetForeground (display, face->gc, gcv.foreground);
     }
 
-  mac_reset_clip_rectangles (display, face->gc);
+  mac_reset_clip_rectangles (f, face->gc);
 }
 
 #if USE_CG_DRAWING
@@ -2852,7 +2967,7 @@ x_set_glyph_string_clipping (s)
   int n;
 
   n = get_glyph_string_clip_rects (s, rects, MAX_CLIP_RECTS);
-  mac_set_clip_rectangles (s->display, s->gc, rects, n);
+  mac_set_clip_rectangles (s->f, s->gc, rects, n);
 }
 
 
@@ -3434,7 +3549,6 @@ x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
      int top_p, bot_p, left_p, right_p, raised_p;
      Rect *clip_rect;
 {
-  Display *dpy = FRAME_MAC_DISPLAY (f);
   int i;
   GC gc;
 
@@ -3442,7 +3556,7 @@ x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
     gc = f->output_data.mac->white_relief.gc;
   else
     gc = f->output_data.mac->black_relief.gc;
-  mac_set_clip_rectangles (dpy, gc, clip_rect, 1);
+  mac_set_clip_rectangles (f, gc, clip_rect, 1);
 
   /* Top.  */
   if (top_p)
@@ -3457,12 +3571,12 @@ x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
       mac_draw_line (f, gc,
                     left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
 
-  mac_reset_clip_rectangles (dpy, gc);
+  mac_reset_clip_rectangles (f, gc);
   if (raised_p)
     gc = f->output_data.mac->black_relief.gc;
   else
     gc = f->output_data.mac->white_relief.gc;
-  mac_set_clip_rectangles (dpy, gc, clip_rect, 1);
+  mac_set_clip_rectangles (f, gc, clip_rect, 1);
 
   /* Bottom.  */
   if (bot_p)
@@ -3477,7 +3591,7 @@ x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
       mac_draw_line (f, gc,
                     right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
 
-  mac_reset_clip_rectangles (dpy, gc);
+  mac_reset_clip_rectangles (f, gc);
 }
 
 
@@ -3499,7 +3613,7 @@ x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
 
   XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
   XSetForeground (s->display, s->gc, s->face->box_color);
-  mac_set_clip_rectangles (s->display, s->gc, clip_rect, 1);
+  mac_set_clip_rectangles (s->f, s->gc, clip_rect, 1);
 
   /* Top.  */
   mac_fill_rectangle (s->f, s->gc, left_x, top_y,
@@ -3520,7 +3634,7 @@ x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
                        top_y, width, bottom_y - top_y + 1);
 
   XSetForeground (s->display, s->gc, xgcv.foreground);
-  mac_reset_clip_rectangles (s->display, s->gc);
+  mac_reset_clip_rectangles (s->f, s->gc);
 }
 
 
@@ -3829,7 +3943,7 @@ x_draw_stretch_glyph_string (s)
            gc = s->face->gc;
 
          get_glyph_string_clip_rect (s, &r);
-         mac_set_clip_rectangles (s->display, gc, &r, 1);
+         mac_set_clip_rectangles (s->f, gc, &r, 1);
 
 #if 0 /* MAC_TODO: stipple */
          if (s->face->stipple)
@@ -3969,7 +4083,7 @@ x_draw_glyph_string (s)
              if (s->face->font)
                 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
             }
-
+         /* FIXME: Obey underline_minimum_offset.  */
          if (s->face->underline_defaulted_p)
            mac_fill_rectangle (s->f, s->gc, s->x, y,
                                s->background_width, h);
@@ -4029,7 +4143,7 @@ x_draw_glyph_string (s)
     }
 
   /* Reset clipping.  */
-  mac_reset_clip_rectangles (s->display, s->gc);
+  mac_reset_clip_rectangles (s->f, s->gc);
 }
 
 /* Shift display to make room for inserted glyphs.   */
@@ -6409,7 +6523,7 @@ x_draw_bar_cursor (w, row, width, kind)
                            cursor_glyph->pixel_width,
                            width);
 
-      mac_reset_clip_rectangles (dpy, gc);
+      mac_reset_clip_rectangles (f, gc);
     }
 }
 
@@ -6739,7 +6853,6 @@ mac_handle_size_change (f, pixelwidth, pixelheight)
       change_frame_size (f, rows, cols, 0, 1, 0);
       FRAME_PIXEL_WIDTH (f) = pixelwidth;
       FRAME_PIXEL_HEIGHT (f) = pixelheight;
-      SET_FRAME_GARBAGED (f);
 
       /* If cursor was outside the new size, mark it as off.  */
       mark_window_cursors_off (XWINDOW (f->root_window));
@@ -6772,7 +6885,6 @@ void
 x_calc_absolute_position (f)
      struct frame *f;
 {
-  int width_diff = 0, height_diff = 0;
   int flags = f->size_hint_flags;
   Rect inner, outer;
 
@@ -6787,22 +6899,15 @@ x_calc_absolute_position (f)
   mac_get_window_bounds (f, &inner, &outer);
   UNBLOCK_INPUT;
 
-  width_diff = (outer.right - outer.left) - (inner.right - inner.left);
-  height_diff = (outer.bottom - outer.top) - (inner.bottom - inner.top);
-
   /* Treat negative positions as relative to the leftmost bottommost
      position that fits on the screen.  */
   if (flags & XNegative)
-    f->left_pos = (FRAME_MAC_DISPLAY_INFO (f)->width
-                   - width_diff
-                  - FRAME_PIXEL_WIDTH (f)
-                  + f->left_pos);
+    f->left_pos += (FRAME_MAC_DISPLAY_INFO (f)->width
+                   - (outer.right - outer.left));
 
   if (flags & YNegative)
-    f->top_pos = (FRAME_MAC_DISPLAY_INFO (f)->height
-                 - height_diff
-                 - FRAME_PIXEL_HEIGHT (f)
-                 + f->top_pos);
+    f->top_pos += (FRAME_MAC_DISPLAY_INFO (f)->height
+                  - (outer.bottom - outer.top));
 
   /* The left_pos and top_pos
      are now relative to the top and left screen edges,
@@ -7284,8 +7389,7 @@ x_free_frame_resources (f)
 
   x_free_gcs (f);
 
-  if (FRAME_SIZE_HINTS (f))
-    xfree (FRAME_SIZE_HINTS (f));
+  xfree (FRAME_SIZE_HINTS (f));
 
   xfree (f->output_data.mac);
   f->output_data.mac = NULL;
@@ -7534,8 +7638,7 @@ xlfdpat_destroy (pat)
     {
       if (pat->buf)
        {
-         if (pat->blocks)
-           xfree (pat->blocks);
+         xfree (pat->blocks);
          xfree (pat->buf);
        }
       xfree (pat);
@@ -8258,8 +8361,7 @@ init_font_name_table ()
                                         HASH_VALUE (h, j));
            prev_family = family;
          }
-      if (font_ids)
-       xfree (font_ids);
+      xfree (font_ids);
     }
 #endif
 
@@ -9136,20 +9238,17 @@ mac_unload_font (dpyinfo, font)
       int i;
 
       for (i = font->min_byte1; i <= font->max_byte1; i++)
-       if (font->bounds.rows[i])
-         xfree (font->bounds.rows[i]);
+       xfree (font->bounds.rows[i]);
       xfree (font->bounds.rows);
       ATSUDisposeStyle (font->mac_style);
     }
   else
 #endif
-    if (font->bounds.per_char)
-      xfree (font->bounds.per_char);
+    xfree (font->bounds.per_char);
 #if USE_CG_TEXT_DRAWING
   if (font->cg_font)
     CGFontRelease (font->cg_font);
-  if (font->cg_glyphs)
-    xfree (font->cg_glyphs);
+  xfree (font->cg_glyphs);
 #endif
   xfree (font);
 }
@@ -9354,8 +9453,8 @@ x_query_font (f, fontname)
 
   for (i = 0; i < dpyinfo->n_fonts; i++)
     if (dpyinfo->font_table[i].name
-       && (!xstricmp (dpyinfo->font_table[i].name, fontname)
-           || !xstricmp (dpyinfo->font_table[i].full_name, fontname)))
+       && (!xstrcasecmp (dpyinfo->font_table[i].name, fontname)
+           || !xstrcasecmp (dpyinfo->font_table[i].full_name, fontname)))
       return (dpyinfo->font_table + i);
   return NULL;
 }
@@ -9635,9 +9734,9 @@ static Lisp_Object Qtoolbar_switch_mode;
 #endif
 #if USE_MAC_TSM
 static TSMDocumentID tsm_document_id;
-static Lisp_Object Qtext_input;
-static Lisp_Object Qupdate_active_input_area, Qunicode_for_key_event;
-static Lisp_Object Vmac_ts_active_input_overlay;
+Lisp_Object Qtext_input;
+Lisp_Object Qupdate_active_input_area, Qunicode_for_key_event;
+Lisp_Object Vmac_ts_active_input_overlay, Vmac_ts_active_input_buf;
 extern Lisp_Object Qbefore_string;
 static Lisp_Object Vmac_ts_script_language_on_focus;
 static Lisp_Object saved_ts_script_language_on_focus;
@@ -9663,7 +9762,8 @@ extern OSStatus install_menu_target_item_handler P_ ((void));
 
 #ifdef MAC_OSX
 extern OSStatus install_service_handler ();
-static Lisp_Object Qservice, Qpaste, Qperform;
+Lisp_Object Qservice, Qpaste, Qperform;
+Lisp_Object Qmouse_drag_overlay;
 #endif
 #endif
 
@@ -9826,6 +9926,216 @@ mac_get_emulated_btn ( UInt32 modifiers )
   return result;
 }
 
+#ifdef MAC_OSX
+void
+mac_get_selected_range (w, range)
+     struct window *w;
+     CFRange *range;
+{
+  Lisp_Object overlay = find_symbol_value (Qmouse_drag_overlay);
+  struct buffer *b = XBUFFER (w->buffer);
+  int begv = BUF_BEGV (b), zv = BUF_ZV (b);
+  int start, end;
+
+  if (OVERLAYP (overlay)
+      && EQ (Foverlay_buffer (overlay), w->buffer)
+      && (start = XINT (Foverlay_start (overlay)),
+         end = XINT (Foverlay_end (overlay)),
+         start != end))
+    ;
+  else
+    {
+      if (w == XWINDOW (selected_window) && b == current_buffer)
+       start = PT;
+      else
+       start = marker_position (w->pointm);
+
+      if (NILP (Vtransient_mark_mode) || NILP (b->mark_active))
+       end = start;
+      else
+       {
+         int mark_pos = marker_position (b->mark);
+
+         if (start <= mark_pos)
+           end = mark_pos;
+         else
+           {
+             end = start;
+             start = mark_pos;
+           }
+       }
+    }
+
+  if (start != end)
+    {
+      if (start < begv)
+       start = begv;
+      else if (start > zv)
+       start = zv;
+
+      if (end < begv)
+       end = begv;
+      else if (end > zv)
+       end = zv;
+    }
+
+  range->location = start - begv;
+  range->length = end - start;
+}
+
+/* Store the text of the buffer BUF from START to END as Unicode
+   characters in CHARACTERS.  Return non-zero if successful.  */
+
+int
+mac_store_buffer_text_to_unicode_chars (buf, start, end, characters)
+     struct buffer *buf;
+     int start, end;
+     UniChar *characters;
+{
+  int start_byte, end_byte, char_count, byte_count;
+  struct coding_system coding;
+  unsigned char *dst = (unsigned char *) characters;
+
+  start_byte = buf_charpos_to_bytepos (buf, start);
+  end_byte = buf_charpos_to_bytepos (buf, end);
+  char_count = end - start;
+  byte_count = end_byte - start_byte;
+
+  if (setup_coding_system (
+#ifdef WORDS_BIG_ENDIAN
+                          intern ("utf-16be")
+#else
+                          intern ("utf-16le")
+#endif
+                          , &coding) < 0)
+    return 0;
+
+  coding.src_multibyte = !NILP (buf->enable_multibyte_characters);
+  coding.dst_multibyte = 0;
+  coding.mode |= CODING_MODE_LAST_BLOCK;
+  coding.composing = COMPOSITION_DISABLED;
+
+  if (BUF_GPT_BYTE (buf) <= start_byte || end_byte <= BUF_GPT_BYTE (buf))
+    encode_coding (&coding, BUF_BYTE_ADDRESS (buf, start_byte), dst,
+                  byte_count, char_count * sizeof (UniChar));
+  else
+    {
+      int first_byte_count = BUF_GPT_BYTE (buf) - start_byte;
+
+      encode_coding (&coding, BUF_BYTE_ADDRESS (buf, start_byte), dst,
+                    first_byte_count, char_count * sizeof (UniChar));
+      if (coding.result == CODING_FINISH_NORMAL)
+       encode_coding (&coding,
+                      BUF_BYTE_ADDRESS (buf, start_byte + first_byte_count),
+                      dst + coding.produced,
+                      byte_count - first_byte_count,
+                      char_count * sizeof (UniChar) - coding.produced);
+    }
+
+  if (coding.result != CODING_FINISH_NORMAL)
+    return 0;
+
+  return 1;
+}
+
+void
+mac_ax_selected_text_range (f, range)
+     struct frame *f;
+     CFRange *range;
+{
+  mac_get_selected_range (XWINDOW (f->selected_window), range);
+}
+
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1030
+unsigned int
+mac_ax_number_of_characters (f)
+     struct frame *f;
+{
+  struct buffer *b = XBUFFER (XWINDOW (f->selected_window)->buffer);
+
+  return BUF_ZV (b) - BUF_BEGV (b);
+}
+#endif
+#endif
+
+#if USE_MAC_TSM
+OSStatus
+mac_restore_keyboard_input_source ()
+{
+  OSStatus err = noErr;
+  ScriptLanguageRecord slrec, *slptr = NULL;
+
+  if (EQ (Vmac_ts_script_language_on_focus, Qt)
+      && EQ (saved_ts_script_language_on_focus, Qt))
+    slptr = &saved_ts_language;
+  else if (CONSP (Vmac_ts_script_language_on_focus)
+          && INTEGERP (XCAR (Vmac_ts_script_language_on_focus))
+          && INTEGERP (XCDR (Vmac_ts_script_language_on_focus))
+          && CONSP (saved_ts_script_language_on_focus)
+          && EQ (XCAR (saved_ts_script_language_on_focus),
+                 XCAR (Vmac_ts_script_language_on_focus))
+          && EQ (XCDR (saved_ts_script_language_on_focus),
+                 XCDR (Vmac_ts_script_language_on_focus)))
+    {
+      slrec.fScript = XINT (XCAR (Vmac_ts_script_language_on_focus));
+      slrec.fLanguage = XINT (XCDR (Vmac_ts_script_language_on_focus));
+      slptr = &slrec;
+    }
+
+  if (slptr)
+    {
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
+      err = SetDefaultInputMethodOfClass (saved_ts_component, slptr,
+                                         kKeyboardInputMethodClass);
+#else
+      err = SetDefaultInputMethod (saved_ts_component, slptr);
+#endif
+      if (err == noErr)
+       err = SetTextServiceLanguage (slptr);
+
+      /* Seems to be needed on Mac OS X 10.2.  */
+      if (err == noErr)
+       KeyScript (slptr->fScript | smKeyForceKeyScriptMask);
+    }
+
+  return err;
+}
+
+void
+mac_save_keyboard_input_source ()
+{
+  OSStatus err;
+  ScriptLanguageRecord slrec, *slptr = NULL;
+
+  saved_ts_script_language_on_focus = Vmac_ts_script_language_on_focus;
+
+  if (EQ (Vmac_ts_script_language_on_focus, Qt))
+    {
+      err = GetTextServiceLanguage (&saved_ts_language);
+      if (err == noErr)
+       slptr = &saved_ts_language;
+    }
+  else if (CONSP (Vmac_ts_script_language_on_focus)
+          && INTEGERP (XCAR (Vmac_ts_script_language_on_focus))
+          && INTEGERP (XCDR (Vmac_ts_script_language_on_focus)))
+    {
+      slrec.fScript = XINT (XCAR (Vmac_ts_script_language_on_focus));
+      slrec.fLanguage = XINT (XCDR (Vmac_ts_script_language_on_focus));
+      slptr = &slrec;
+    }
+
+  if (slptr)
+    {
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1020
+      GetDefaultInputMethodOfClass (&saved_ts_component, slptr,
+                                   kKeyboardInputMethodClass);
+#else
+      GetDefaultInputMethod (&saved_ts_component, slptr);
+#endif
+    }
+}
+#endif
+
 #if TARGET_API_MAC_CARBON
 /***** Code to handle C-g testing  *****/
 extern int quit_char;
@@ -12709,12 +13019,10 @@ x_delete_display (dpyinfo)
 
   if (dpyinfo->font_table)
     {
-      if (dpyinfo->font_table->font_encoder)
-       xfree (dpyinfo->font_table->font_encoder);
+      xfree (dpyinfo->font_table->font_encoder);
       xfree (dpyinfo->font_table);
     }
-  if (dpyinfo->mac_id_name)
-    xfree (dpyinfo->mac_id_name);
+  xfree (dpyinfo->mac_id_name);
 
   if (x_display_list == 0)
     {
@@ -12970,6 +13278,9 @@ syms_of_macterm ()
   Qservice     = intern ("service");     staticpro (&Qservice);
   Qpaste       = intern ("paste");       staticpro (&Qpaste);
   Qperform     = intern ("perform");     staticpro (&Qperform);
+
+  Qmouse_drag_overlay = intern ("mouse-drag-overlay");
+  staticpro (&Qmouse_drag_overlay);
 #endif
 #if USE_MAC_TSM
   Qtext_input = intern ("text-input"); staticpro (&Qtext_input);
@@ -13130,6 +13441,11 @@ CODING_SYSTEM is a coding system corresponding to TEXT-ENCODING.  */);
     doc: /* Overlay used to display Mac TSM active input area.  */);
   Vmac_ts_active_input_overlay = Qnil;
 
+  DEFVAR_LISP ("mac-ts-active-input-buf", &Vmac_ts_active_input_buf,
+    doc: /* Byte sequence of the current Mac TSM active input area.  */);
+  /* `empty_string' is not ready yet on Mac OS Classic.  */
+  Vmac_ts_active_input_buf = build_string ("");
+
   DEFVAR_LISP ("mac-ts-script-language-on-focus", &Vmac_ts_script_language_on_focus,
     doc: /* *How to change Mac TSM script/language when a frame gets focus.
 If the value is t, the input script and language are restored to those