* lisp.h (Mouse_HLInfo): Move from here...
[bpt/emacs.git] / src / nsterm.m
index 1f09e03..f7f7b89 100644 (file)
@@ -60,6 +60,10 @@ GNUstep port and post-20 update by Adrian Robert (arobert@cogsci.ucsd.edu)
 #include "buffer.h"
 #include "font.h"
 
+#ifdef NS_IMPL_GNUSTEP
+#include "process.h"
+#endif
+
 /* call tracing */
 #if 0
 int term_trace_num = 0;
@@ -196,7 +200,9 @@ static BOOL gsaved = NO;
 static BOOL ns_fake_keydown = NO;
 int ns_tmp_flags; /* FIXME */
 struct nsfont_info *ns_tmp_font; /* FIXME */
+#ifdef NS_IMPL_COCOA
 static BOOL ns_menu_bar_is_hidden = NO;
+#endif
 /*static int debug_lock = 0; */
 
 /* event loop */
@@ -228,6 +234,24 @@ static struct {
   NULL, 0, 0
 };
 
+#ifdef NS_IMPL_COCOA
+/*
+ * State for pending menu activation:
+ * MENU_NONE     Normal state
+ * MENU_PENDING  A menu has been clicked on, but has been canceled so we can
+ *               run lisp to update the menu.
+ * MENU_OPENING  Menu is up to date, and the click event is redone so the menu
+ *               will open.
+ */
+#define MENU_NONE 0
+#define MENU_PENDING 1
+#define MENU_OPENING 2
+static int menu_will_open_state = MENU_NONE;
+
+/* Saved position for menu click.  */
+static CGPoint menu_mouse_point;
+#endif
+
 /* Convert modifiers in a NeXTstep event to emacs style modifiers.  */
 #define NS_FUNCTION_KEY_MASK 0x800000
 #define NSLeftControlKeyMask    (0x000001 | NSControlKeyMask)
@@ -298,8 +322,6 @@ static struct {
       ns_send_appdefined (-1);                                          \
     }
 
-void x_set_cursor_type (struct frame *, Lisp_Object, Lisp_Object);
-
 /* TODO: get rid of need for these forward declarations */
 static void ns_condemn_scroll_bars (struct frame *f);
 static void ns_judge_scroll_bars (struct frame *f);
@@ -319,8 +341,8 @@ hold_event (struct input_event *event)
     {
       if (hold_event_q.cap == 0) hold_event_q.cap = 10;
       else hold_event_q.cap *= 2;
-      hold_event_q.q = (struct input_event *)
-        xrealloc (hold_event_q.q, hold_event_q.cap * sizeof (*hold_event_q.q));
+      hold_event_q.q =
+        xrealloc (hold_event_q.q, hold_event_q.cap * sizeof *hold_event_q.q);
     }
 
   hold_event_q.q[hold_event_q.nr++] = *event;
@@ -337,7 +359,7 @@ append2 (Lisp_Object list, Lisp_Object item)
 {
   Lisp_Object array[2];
   array[0] = list;
-  array[1] = Fcons (item, Qnil);
+  array[1] = list1 (item);
   return Fnconc (2, &array[0]);
 }
 
@@ -651,8 +673,6 @@ ns_update_begin (struct frame *f)
    -------------------------------------------------------------------------- */
 {
   NSView *view = FRAME_NS_VIEW (f);
-  NSRect r = [view frame];
-  NSBezierPath *bp;
   NSTRACE (ns_update_begin);
 
   ns_update_auto_hide_menu_bar ();
@@ -664,9 +684,24 @@ ns_update_begin (struct frame *f)
      is for the minibuffer.  But the display engine may draw more because
      we have set the frame as garbaged.  So reset clip path to the whole
      view.  */
-  bp = [[NSBezierPath bezierPathWithRect: r] retain];
-  [bp setClip];
-  [bp release];
+#ifdef NS_IMPL_COCOA
+  {
+    NSBezierPath *bp;
+    NSRect r = [view frame];
+    NSRect cr = [[view window] frame];
+    /* If a large frame size is set, r may be larger than the window frame
+       before constrained.  In that case don't change the clip path, as we
+       will clear in to the tool bar and title bar.  */
+    if (r.size.height
+        + FRAME_NS_TITLEBAR_HEIGHT (f)
+        + FRAME_TOOLBAR_HEIGHT (f) <= cr.size.height)
+      {
+        bp = [[NSBezierPath bezierPathWithRect: r] retain];
+        [bp setClip];
+        [bp release];
+      }
+  }
+#endif
 
 #ifdef NS_IMPL_GNUSTEP
   uRect = NSMakeRect (0, 0, 0, 0);
@@ -682,9 +717,9 @@ ns_update_window_begin (struct window *w)
    -------------------------------------------------------------------------- */
 {
   struct frame *f = XFRAME (WINDOW_FRAME (w));
- Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
+  Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
+
   NSTRACE (ns_update_window_begin);
-  updated_window = w;
   set_output_cursor (&w->cursor);
 
   block_input ();
@@ -707,15 +742,13 @@ ns_update_window_begin (struct window *w)
 
 
 static void
-ns_update_window_end (struct window *w, int cursor_on_p,
-                      int mouse_face_overwritten_p)
+ns_update_window_end (struct window *w, bool cursor_on_p,
+                      bool mouse_face_overwritten_p)
 /* --------------------------------------------------------------------------
    Finished a grouped sequence of drawing calls
    external (RIF) call; for one window called before update_end
    -------------------------------------------------------------------------- */
 {
-  Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
-
   /* note: this fn is nearly identical in all terms */
   if (!w->pseudo_window_p)
     {
@@ -735,13 +768,8 @@ ns_update_window_end (struct window *w, int cursor_on_p,
   /* If a row with mouse-face was overwritten, arrange for
      frame_up_to_date to redisplay the mouse highlight.  */
   if (mouse_face_overwritten_p)
-    {
-      hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
-      hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
-      hlinfo->mouse_face_window = Qnil;
-    }
+    reset_mouse_highlight (MOUSE_HL_INFO (XFRAME (w->frame)));
 
-  updated_window = NULL;
   NSTRACE (update_window_end);
 }
 
@@ -753,19 +781,12 @@ ns_update_end (struct frame *f)
    external (RIF) call; for whole frame, called after update_window_end
    -------------------------------------------------------------------------- */
 {
-  NSView *view = FRAME_NS_VIEW (f);
+  EmacsView *view = FRAME_NS_VIEW (f);
 
 /*   if (f == MOUSE_HL_INFO (f)->mouse_face_mouse_frame) */
-    MOUSE_HL_INFO (f)->mouse_face_defer = 0;
-
-    block_input ();
+  MOUSE_HL_INFO (f)->mouse_face_defer = 0;
 
-#ifdef NS_IMPL_GNUSTEP
-  /* trigger flush only in the rectangle we tracked as being drawn */
-  [view unlockFocusNeedsFlush: NO];
-/*fprintf (stderr, " (%.0f, %.0f : %.0f x %.0f)", uRect.origin.x, uRect.origin.y, uRect.size.width, uRect.size.height); */
-  [view lockFocusInRect: uRect];
-#endif
+  block_input ();
 
   [view unlockFocus];
   [[view window] flushWindow];
@@ -799,13 +820,6 @@ ns_focus (struct frame *f, NSRect *r, int n)
    -------------------------------------------------------------------------- */
 {
 //  NSTRACE (ns_focus);
-#ifdef NS_IMPL_GNUSTEP
-  NSRect u;
-    if (n == 2)
-      u = NSUnionRect (r[0], r[1]);
-    else if (r)
-      u = *r;
-#endif
 /* static int c =0;
    fprintf (stderr, "focus: %d", c++);
    if (r) fprintf (stderr, " (%.0f, %.0f : %.0f x %.0f)", r->origin.x, r->origin.y, r->size.width, r->size.height);
@@ -824,33 +838,11 @@ ns_focus (struct frame *f, NSRect *r, int n)
             }
 
           if (view)
-#ifdef NS_IMPL_GNUSTEP
-            r ? [view lockFocusInRect: u] : [view lockFocus];
-#else
             [view lockFocus];
-#endif
           focus_view = view;
 /*if (view) debug_lock++; */
         }
-#ifdef NS_IMPL_GNUSTEP
-      else
-        {
-          /* more than one rect being drawn into */
-          if (view && r)
-            {
-              [view unlockFocus]; /* add prev rect to redraw list */
-              [view lockFocusInRect: u]; /* focus for draw in new rect */
-            }
-        }
-#endif
-    }
-#ifdef NS_IMPL_GNUSTEP
-  else
-    {
-      /* in batch mode, but in GNUstep must still track rectangles explicitly */
-      uRect = (r ? NSUnionRect (uRect, u) : [FRAME_NS_VIEW (f) visibleRect]);
     }
-#endif
 
   /* clipping */
   if (r)
@@ -993,8 +985,9 @@ ns_raise_frame (struct frame *f)
      Bring window to foreground and make it active
    -------------------------------------------------------------------------- */
 {
-  NSView *view = FRAME_NS_VIEW (f);
-  check_ns ();
+  NSView *view;
+  check_window_system (f);
+  view = FRAME_NS_VIEW (f);
   block_input ();
   if (FRAME_VISIBLE_P (f))
     [[view window] makeKeyAndOrderFront: NSApp];
@@ -1008,8 +1001,9 @@ ns_lower_frame (struct frame *f)
      Send window to back
    -------------------------------------------------------------------------- */
 {
-  NSView *view = FRAME_NS_VIEW (f);
-  check_ns ();
+  NSView *view;
+  check_window_system (f);
+  view = FRAME_NS_VIEW (f);
   block_input ();
   [[view window] orderBack: NSApp];
   unblock_input ();
@@ -1112,9 +1106,10 @@ x_make_frame_invisible (struct frame *f)
      External: Hide the window (X11 semantics)
    -------------------------------------------------------------------------- */
 {
-  NSView * view = FRAME_NS_VIEW (f);
+  NSView *view;
   NSTRACE (x_make_frame_invisible);
-  check_ns ();
+  check_window_system (f);
+  view = FRAME_NS_VIEW (f);
   [[view window] orderOut: NSApp];
   SET_FRAME_VISIBLE (f, 0);
   SET_FRAME_ICONIFIED (f, 0);
@@ -1127,10 +1122,13 @@ x_iconify_frame (struct frame *f)
      External: Iconify window
    -------------------------------------------------------------------------- */
 {
-  NSView * view = FRAME_NS_VIEW (f);
-  struct ns_display_info *dpyinfo = FRAME_NS_DISPLAY_INFO (f);
+  NSView *view;
+  struct ns_display_info *dpyinfo;
+
   NSTRACE (x_iconify_frame);
-  check_ns ();
+  check_window_system (f);
+  view = FRAME_NS_VIEW (f);
+  dpyinfo = FRAME_NS_DISPLAY_INFO (f);
 
   if (dpyinfo->x_highlight_frame == f)
     dpyinfo->x_highlight_frame = 0;
@@ -1155,11 +1153,15 @@ x_iconify_frame (struct frame *f)
 void
 x_free_frame_resources (struct frame *f)
 {
-  NSView *view = FRAME_NS_VIEW (f);
-  struct ns_display_info *dpyinfo = FRAME_NS_DISPLAY_INFO (f);
-  Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
+  NSView *view;
+  struct ns_display_info *dpyinfo;
+  Mouse_HLInfo *hlinfo;
+
   NSTRACE (x_free_frame_resources);
-  check_ns ();
+  check_window_system (f);
+  view = FRAME_NS_VIEW (f);
+  dpyinfo = FRAME_NS_DISPLAY_INFO (f);
+  hlinfo = MOUSE_HL_INFO (f);
 
   [(EmacsView *)view setWindowClosing: YES]; /* may not have been informed */
 
@@ -1175,12 +1177,7 @@ x_free_frame_resources (struct frame *f)
   if (f == dpyinfo->x_highlight_frame)
     dpyinfo->x_highlight_frame = 0;
   if (f == hlinfo->mouse_face_mouse_frame)
-    {
-      hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
-      hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
-      hlinfo->mouse_face_window = Qnil;
-      hlinfo->mouse_face_mouse_frame = 0;
-    }
+    reset_mouse_highlight (hlinfo);
 
   if (f->output_data.ns->miniimage != nil)
     [f->output_data.ns->miniimage release];
@@ -1200,7 +1197,7 @@ x_destroy_window (struct frame *f)
    -------------------------------------------------------------------------- */
 {
   NSTRACE (x_destroy_window);
-  check_ns ();
+  check_window_system (f);
   x_free_frame_resources (f);
   ns_window_num--;
 }
@@ -1288,12 +1285,17 @@ x_set_window_size (struct frame *f, int change_grav, int cols, int rows)
 
   /* If we have a toolbar, take its height into account. */
   if (tb && ! [view isFullscreen])
+    {
     /* NOTE: previously this would generate wrong result if toolbar not
              yet displayed and fixing toolbar_height=32 helped, but
              now (200903) seems no longer needed */
     FRAME_TOOLBAR_HEIGHT (f) =
       NSHeight ([window frameRectForContentRect: NSMakeRect (0, 0, 0, 0)])
         - FRAME_NS_TITLEBAR_HEIGHT (f);
+#ifdef NS_IMPL_GNUSTEP
+      FRAME_TOOLBAR_HEIGHT (f) -= 3;
+#endif
+    }
   else
     FRAME_TOOLBAR_HEIGHT (f) = 0;
 
@@ -1345,7 +1347,7 @@ x_set_window_size (struct frame *f, int change_grav, int cols, int rows)
 
 
 static void
-ns_fullscreen_hook (FRAME_PTR f)
+ns_fullscreen_hook (struct frame *f)
 {
   EmacsView *view = (EmacsView *)FRAME_NS_VIEW (f);
 
@@ -1519,7 +1521,7 @@ ns_get_color (const char *name, NSColor **col)
         }
     }
 
-  if (r >= 0.0)
+  if (r >= 0.0F)
     {
       *col = [NSColor colorWithCalibratedRed: r green: g blue: b alpha: 1.0];
       unblock_input ();
@@ -1580,7 +1582,7 @@ ns_color_to_lisp (NSColor *col)
      Convert a color to a lisp string with the RGB equivalent
    -------------------------------------------------------------------------- */
 {
-  CGFloat red, green, blue, alpha, gray;
+  EmacsCGFloat red, green, blue, alpha, gray;
   char buf[1024];
   const char *str;
   NSTRACE (ns_color_to_lisp);
@@ -1622,7 +1624,7 @@ ns_query_color(void *col, XColor *color_def, int setPixel)
          and set color_def pixel to the resulting index.
    -------------------------------------------------------------------------- */
 {
-  CGFloat r, g, b, a;
+  EmacsCGFloat r, g, b, a;
 
   [((NSColor *)col) getRed: &r green: &g blue: &b alpha: &a];
   color_def->red   = r * 65535;
@@ -1667,26 +1669,6 @@ ns_defined_color (struct frame *f,
 }
 
 
-unsigned long
-ns_get_rgb_color (struct frame *f, float r, float g, float b, float a)
-/* --------------------------------------------------------------------------
-    return an autoreleased RGB color
-   -------------------------------------------------------------------------- */
-{
-/*static int c = 1; fprintf (stderr, "color request %d\n", c++); */
-  if (r < 0.0) r = 0.0;
-  else if (r > 1.0) r = 1.0;
-  if (g < 0.0) g = 0.0;
-  else if (g > 1.0) g = 1.0;
-  if (b < 0.0) b = 0.0;
-  else if (b > 1.0) b = 1.0;
-  if (a < 0.0) a = 0.0;
-  else if (a > 1.0) a = 1.0;
-  return (unsigned long) ns_index_color(
-    [NSColor colorWithCalibratedRed: r green: g blue: b alpha: a], f);
-}
-
-
 void
 x_set_frame_alpha (struct frame *f)
 /* --------------------------------------------------------------------------
@@ -1694,7 +1676,6 @@ x_set_frame_alpha (struct frame *f)
    -------------------------------------------------------------------------- */
 {
   struct ns_display_info *dpyinfo = FRAME_NS_DISPLAY_INFO (f);
-  EmacsView *view = FRAME_NS_VIEW (f);
   double alpha = 1.0;
   double alpha_min = 1.0;
 
@@ -1716,7 +1697,10 @@ x_set_frame_alpha (struct frame *f)
     alpha = alpha_min;
 
 #ifdef NS_IMPL_COCOA
+  {
+    EmacsView *view = FRAME_NS_VIEW (f);
   [[view window] setAlphaValue: alpha];
+  }
 #endif
 }
 
@@ -1769,7 +1753,7 @@ x_set_mouse_position (struct frame *f, int h, int v)
 
 
 static int
-note_mouse_movement (struct frame *frame, float x, float y)
+note_mouse_movement (struct frame *frame, CGFloat x, CGFloat y)
 /*   ------------------------------------------------------------------------
      Called by EmacsView on mouseMovement events.  Passes on
      to emacs mainstream code if we moved off of a rect of interest
@@ -1854,7 +1838,7 @@ ns_mouse_position (struct frame **fp, int insist, Lisp_Object *bar_window,
         f = dpyinfo->x_focus_frame ? dpyinfo->x_focus_frame
                                     : SELECTED_FRAME ();
 
-      if (f && f->output_data.ns)  /* TODO: 2nd check no longer needed? */
+      if (f && FRAME_NS_P (f))
         {
           view = FRAME_NS_VIEW (*fp);
 
@@ -1893,10 +1877,9 @@ ns_frame_up_to_date (struct frame *f)
        {
          block_input ();
          ns_update_begin(f);
-         if (hlinfo->mouse_face_mouse_frame)
-           note_mouse_highlight (hlinfo->mouse_face_mouse_frame,
-                                 hlinfo->mouse_face_mouse_x,
-                                 hlinfo->mouse_face_mouse_y);
+         note_mouse_highlight (hlinfo->mouse_face_mouse_frame,
+                               hlinfo->mouse_face_mouse_x,
+                               hlinfo->mouse_face_mouse_y);
          ns_update_end(f);
          unblock_input ();
        }
@@ -2095,7 +2078,6 @@ ns_scroll_run (struct window *w, struct run *run)
 
   block_input ();
 
-  updated_window = w;
   x_clear_cursor (w);
 
   {
@@ -2113,12 +2095,11 @@ ns_scroll_run (struct window *w, struct run *run)
 
 
 static void
-ns_after_update_window_line (struct glyph_row *desired_row)
+ns_after_update_window_line (struct window *w, struct glyph_row *desired_row)
 /* --------------------------------------------------------------------------
     External (RIF): preparatory to fringe update after text was updated
    -------------------------------------------------------------------------- */
 {
-  struct window *w = updated_window;
   struct frame *f;
   int width, height;
 
@@ -2228,7 +2209,6 @@ ns_draw_fringe_bitmap (struct window *w, struct glyph_row *row,
 {
   struct frame *f = XFRAME (WINDOW_FRAME (w));
   struct face *face = p->face;
-  int rowY;
   static EmacsImage **bimgs = NULL;
   static int nBimgs = 0;
 
@@ -2242,7 +2222,6 @@ ns_draw_fringe_bitmap (struct window *w, struct glyph_row *row,
     }
 
   /* Must clip because of partially visible lines.  */
-  rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
   ns_clip_to_row (w, row, -1, YES);
 
   if (!p->overlay_p)
@@ -2330,7 +2309,7 @@ ns_draw_fringe_bitmap (struct window *w, struct glyph_row *row,
       [ns_lookup_indexed_color(face->background, f) set];
       NSRectFill (r);
       [img setXBMColor: ns_lookup_indexed_color(face->foreground, f)];
-#if !defined (NS_IMPL_COCOA) || MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
+#if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
       [img drawInRect: r
               fromRect: NSZeroRect
              operation: NSCompositeSourceOver
@@ -2351,8 +2330,8 @@ ns_draw_fringe_bitmap (struct window *w, struct glyph_row *row,
 
 static void
 ns_draw_window_cursor (struct window *w, struct glyph_row *glyph_row,
-                       int x, int y, int cursor_type, int cursor_width,
-                       int on_p, int active_p)
+                      int x, int y, enum text_cursor_kinds cursor_type,
+                      int cursor_width, bool on_p, bool active_p)
 /* --------------------------------------------------------------------------
      External call (RIF): draw cursor.
      Note that CURSOR_WIDTH is meaningful only for (h)bar cursors.
@@ -2362,7 +2341,6 @@ ns_draw_window_cursor (struct window *w, struct glyph_row *glyph_row,
   int fx, fy, h, cursor_height;
   struct frame *f = WINDOW_XFRAME (w);
   struct glyph *phys_cursor_glyph;
-  int overspill;
   struct glyph *cursor_glyph;
   struct face *face;
   NSColor *hollow_color = FRAME_BACKGROUND_COLOR (f);
@@ -2569,7 +2547,7 @@ ns_get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
   --------------------------------------------------------------------- */
 
 static void
-ns_draw_underwave (struct glyph_string *s, CGFloat width, CGFloat x)
+ns_draw_underwave (struct glyph_string *s, EmacsCGFloat width, EmacsCGFloat x)
 {
   int wave_height = 3, wave_length = 2;
   int y, dx, dy, odd, xmax;
@@ -2587,7 +2565,7 @@ ns_draw_underwave (struct glyph_string *s, CGFloat width, CGFloat x)
   NSRectClip (waveClip);
 
   /* Draw the waves */
-  a.x = x - ((int)(x) % dx) + 0.5;
+  a.x = x - ((int)(x) % dx) + (EmacsCGFloat) 0.5;
   b.x = a.x + dx;
   odd = (int)(a.x/dx) % 2;
   a.y = b.y = y + 0.5;
@@ -2727,7 +2705,8 @@ ns_draw_text_decoration (struct glyph_string *s, struct face *face,
 }
 
 static void
-ns_draw_box (NSRect r, float thickness, NSColor *col, char left_p, char right_p)
+ns_draw_box (NSRect r, CGFloat thickness, NSColor *col,
+             char left_p, char right_p)
 /* --------------------------------------------------------------------------
     Draw an unfilled rect inside r, optionally leaving left and/or right open.
     Note we can't just use an NSDrawRect command, because of the possibility
@@ -3004,7 +2983,7 @@ ns_dumpglyphs_image (struct glyph_string *s, NSRect r)
   /* Draw the image.. do we need to draw placeholder if img ==nil? */
   if (img != nil)
     {
-#if !defined (NS_IMPL_COCOA) || MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
+#if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
       NSRect dr = NSMakeRect (x, y, s->slice.width, s->slice.height);
       NSRect ir = NSMakeRect (s->slice.x, s->slice.y,
                               s->slice.width, s->slice.height);
@@ -3329,6 +3308,19 @@ ns_send_appdefined (int value)
 {
   /*NSTRACE (ns_send_appdefined); */
 
+#ifdef NS_IMPL_GNUSTEP
+  // GNUStep needs postEvent to happen on the main thread.
+  if (! [[NSThread currentThread] isMainThread])
+    {
+      EmacsApp *app = (EmacsApp *)NSApp;
+      app->nextappdefined = value;
+      [app performSelectorOnMainThread:@selector (sendFromMainThread:)
+                            withObject:nil
+                         waitUntilDone:YES];
+      return;
+    }
+#endif
+
   /* Only post this event if we haven't already posted one.  This will end
        the [NXApp run] main loop after having processed all events queued at
        this moment.  */
@@ -3388,6 +3380,73 @@ check_native_fs ()
 }
 #endif
 
+/* GNUStep and OSX <= 10.4 does not have cancelTracking.  */
+#if defined (NS_IMPL_COCOA) && \
+  MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
+/* Check if menu open should be cancelled or continued as normal.  */
+void
+ns_check_menu_open (NSMenu *menu)
+{
+  /* Click in menu bar? */
+  NSArray *a = [[NSApp mainMenu] itemArray];
+  int i;
+  BOOL found = NO;
+
+  if (menu == nil) // Menu tracking ended.
+    {
+      if (menu_will_open_state == MENU_OPENING)
+        menu_will_open_state = MENU_NONE;
+      return;
+    }
+
+  for (i = 0; ! found && i < [a count]; i++)
+    found = menu == [[a objectAtIndex:i] submenu];
+  if (found)
+    {
+      if (menu_will_open_state == MENU_NONE && emacs_event)
+        {
+          NSEvent *theEvent = [NSApp currentEvent];
+          struct frame *emacsframe = SELECTED_FRAME ();
+
+          [menu cancelTracking];
+          menu_will_open_state = MENU_PENDING;
+          emacs_event->kind = MENU_BAR_ACTIVATE_EVENT;
+          EV_TRAILER (theEvent);
+
+          CGEventRef ourEvent = CGEventCreate (NULL);
+          menu_mouse_point = CGEventGetLocation (ourEvent);
+          CFRelease (ourEvent);
+        }
+      else if (menu_will_open_state == MENU_OPENING)
+        {
+          menu_will_open_state = MENU_NONE;
+        }
+    }
+}
+
+/* Redo saved menu click if state is MENU_PENDING.  */
+void
+ns_check_pending_open_menu ()
+{
+  if (menu_will_open_state == MENU_PENDING)
+    {
+      CGEventSourceRef source
+        = CGEventSourceCreate (kCGEventSourceStateHIDSystemState);
+
+      CGEventRef event = CGEventCreateMouseEvent (source,
+                                                  kCGEventLeftMouseDown,
+                                                  menu_mouse_point,
+                                                  kCGMouseButtonLeft);
+      CGEventSetType (event, kCGEventLeftMouseDown);
+      CGEventPost (kCGHIDEventTap, event);
+      CFRelease (event);
+      CFRelease (source);
+
+      menu_will_open_state = MENU_OPENING;
+    }
+}
+#endif /* NS_IMPL_COCOA) && >= MAC_OS_X_VERSION_10_5 */
+
 static int
 ns_read_socket (struct terminal *terminal, struct input_event *hold_quit)
 /* --------------------------------------------------------------------------
@@ -3470,7 +3529,8 @@ ns_read_socket (struct terminal *terminal, struct input_event *hold_quit)
 
 int
 ns_select (int nfds, fd_set *readfds, fd_set *writefds,
-           fd_set *exceptfds, EMACS_TIME *timeout, sigset_t *sigmask)
+          fd_set *exceptfds, EMACS_TIME const *timeout,
+          sigset_t const *sigmask)
 /* --------------------------------------------------------------------------
      Replacement for select, checking for events
    -------------------------------------------------------------------------- */
@@ -3535,7 +3595,7 @@ ns_select (int nfds, fd_set *readfds, fd_set *writefds,
 
       /* Inform fd_handler that select should be called */
       c = 'g';
-      emacs_write (selfds[1], &c, 1);
+      emacs_write_sig (selfds[1], &c, 1);
     }
   else if (nr == 0 && timeout)
     {
@@ -3568,7 +3628,7 @@ ns_select (int nfds, fd_set *readfds, fd_set *writefds,
   if (nr > 0 && readfds)
     {
       c = 's';
-      emacs_write (selfds[1], &c, 1);
+      emacs_write_sig (selfds[1], &c, 1);
     }
   unblock_input ();
 
@@ -3596,11 +3656,15 @@ ns_select (int nfds, fd_set *readfds, fd_set *writefds,
           pthread_mutex_lock (&select_mutex);
           if (readfds) *readfds = select_readfds;
           if (writefds) *writefds = select_writefds;
-          if (timeout) *timeout = select_timeout;
           pthread_mutex_unlock (&select_mutex);
           result = t;
         }
     }
+  else
+    {
+      errno = EINTR;
+      result = -1;
+    }
 
   return result;
 }
@@ -3665,16 +3729,7 @@ ns_set_vertical_scroll_bar (struct window *window,
   v = [view frame];
   r.origin.y = (v.size.height - r.size.height - r.origin.y);
 
-  if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (window))
-    fringe_extended_p = (WINDOW_LEFTMOST_P (window)
-                        && WINDOW_LEFT_FRINGE_WIDTH (window)
-                        && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (window)
-                            || WINDOW_LEFT_MARGIN_COLS (window) == 0));
-  else
-    fringe_extended_p = (WINDOW_RIGHTMOST_P (window)
-                        && WINDOW_RIGHT_FRINGE_WIDTH (window)
-                        && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (window)
-                            || WINDOW_RIGHT_MARGIN_COLS (window) == 0));
+  fringe_extended_p = WINDOW_FRINGE_EXTENDED_P (window);
 
   XSETWINDOW (win, window);
   block_input ();
@@ -3704,7 +3759,7 @@ ns_set_vertical_scroll_bar (struct window *window,
         }
 
       bar = [[EmacsScroller alloc] initFrame: r window: win];
-      wset_vertical_scroll_bar (window, make_save_pointer (bar));
+      wset_vertical_scroll_bar (window, make_save_ptr (bar));
     }
   else
     {
@@ -3790,15 +3845,6 @@ ns_judge_scroll_bars (struct frame *f)
     [eview updateFrameSize: NO];
 }
 
-
-void
-x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
-{
-  /* XXX irrelevant under NS */
-}
-
-
-
 /* ==========================================================================
 
     Initialization
@@ -3808,15 +3854,31 @@ x_wm_set_icon_position (struct frame *f, int icon_x, int icon_y)
 int
 x_display_pixel_height (struct ns_display_info *dpyinfo)
 {
-  NSScreen *screen = [NSScreen mainScreen];
-  return [screen frame].size.height;
+  NSArray *screens = [NSScreen screens];
+  NSEnumerator *enumerator = [screens objectEnumerator];
+  NSScreen *screen;
+  NSRect frame;
+
+  frame = NSZeroRect;
+  while ((screen = [enumerator nextObject]) != nil)
+    frame = NSUnionRect (frame, [screen frame]);
+
+  return NSHeight (frame);
 }
 
 int
 x_display_pixel_width (struct ns_display_info *dpyinfo)
 {
-  NSScreen *screen = [NSScreen mainScreen];
-  return [screen frame].size.width;
+  NSArray *screens = [NSScreen screens];
+  NSEnumerator *enumerator = [screens objectEnumerator];
+  NSScreen *screen;
+  NSRect frame;
+
+  frame = NSZeroRect;
+  while ((screen = [enumerator nextObject]) != nil)
+    frame = NSUnionRect (frame, [screen frame]);
+
+  return NSWidth (frame);
 }
 
 
@@ -3878,7 +3940,6 @@ ns_initialize_display_info (struct ns_display_info *dpyinfo)
 {
     NSScreen *screen = [NSScreen mainScreen];
     NSWindowDepth depth = [screen depth];
-    Mouse_HLInfo *hlinfo = &dpyinfo->mouse_highlight;
 
     dpyinfo->resx = 72.27; /* used 75.0, but this makes pt == pixel, expected */
     dpyinfo->resy = 72.27;
@@ -3891,22 +3952,12 @@ ns_initialize_display_info (struct ns_display_info *dpyinfo)
     dpyinfo->color_table = xmalloc (sizeof *dpyinfo->color_table);
     dpyinfo->color_table->colors = NULL;
     dpyinfo->root_window = 42; /* a placeholder.. */
-
-    hlinfo->mouse_face_mouse_frame = NULL;
-    hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
-    hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
-    hlinfo->mouse_face_face_id = DEFAULT_FACE_ID;
-    hlinfo->mouse_face_window = hlinfo->mouse_face_overlay = Qnil;
-    hlinfo->mouse_face_hidden = 0;
-
-    hlinfo->mouse_face_mouse_x = hlinfo->mouse_face_mouse_y = 0;
-    hlinfo->mouse_face_defer = 0;
-
     dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame = NULL;
-
     dpyinfo->n_fonts = 0;
     dpyinfo->smallest_font_height = 1;
     dpyinfo->smallest_char_width = 1;
+
+    reset_mouse_highlight (&dpyinfo->mouse_highlight);
 }
 
 
@@ -4053,7 +4104,7 @@ ns_term_init (Lisp_Object display_name)
 
   if (selfds[0] == -1)
     {
-      if (pipe (selfds) == -1)
+      if (emacs_pipe (selfds) != 0)
         {
           fprintf (stderr, "Failed to create pipe: %s\n",
                    emacs_strerror (errno));
@@ -4271,6 +4322,12 @@ ns_term_init (Lisp_Object display_name)
 
   [NSApp run];
   ns_do_open_file = YES;
+
+#ifdef NS_IMPL_GNUSTEP
+  /* GNUstep steals SIGCHLD for use in NSTask, but we don't use NSTask.
+     We must re-catch it so subprocess works.  */
+  catch_child_signal ();
+#endif
   return dpyinfo;
 }
 
@@ -4321,19 +4378,37 @@ ns_term_shutdown (int sig)
 {
   int type = [theEvent type];
   NSWindow *window = [theEvent window];
+
 /*  NSTRACE (sendEvent); */
 /*fprintf (stderr, "received event of type %d\t%d\n", type);*/
 
-#ifdef NS_IMPL_COCOA
-  if (type == NSApplicationDefined
-      && [theEvent data2] == NSAPP_DATA2_RUNASSCRIPT)
+#ifdef NS_IMPL_GNUSTEP
+  // Keyboard events aren't propagated to file dialogs for some reason.
+  if ([NSApp modalWindow] != nil &&
+      (type == NSKeyDown || type == NSKeyUp || type == NSFlagsChanged))
     {
-      ns_run_ascript ();
-      [self stop: self];
+      [[NSApp modalWindow] sendEvent: theEvent];
       return;
     }
 #endif
 
+  if (type == NSApplicationDefined)
+    {
+      switch ([theEvent data2])
+        {
+#ifdef NS_IMPL_COCOA
+        case NSAPP_DATA2_RUNASSCRIPT:
+          ns_run_ascript ();
+          [self stop: self];
+          return;
+#endif
+        case NSAPP_DATA2_RUNFILEDIALOG:
+          ns_run_file_dialog ();
+          [self stop: self];
+          return;
+        }
+    }
+
   if (type == NSCursorUpdate && window == nil)
     {
       fprintf (stderr, "Dropping external cursor update event.\n");
@@ -4357,6 +4432,23 @@ ns_term_shutdown (int sig)
         }
     }
 
+
+#ifdef NS_IMPL_COCOA
+  /* If no dialog and none of our frames have focus and it is a move, skip it.
+     It is a mouse move in an auxiliary menu, i.e. on the top right on OSX,
+     such as Wifi, sound, date or similar.
+     This prevents "spooky" highlighting in the frame under the menu.  */
+  if (type == NSMouseMoved && [NSApp modalWindow] == nil)
+    {
+      struct ns_display_info *di;
+      BOOL has_focus = NO;
+      for (di = x_display_list; ! has_focus && di; di = di->next)
+        has_focus = di->x_focus_frame != 0;
+      if (! has_focus)
+        return;
+    }
+#endif
+
   [super sendEvent: theEvent];
 }
 
@@ -4578,6 +4670,13 @@ not_in_argv (NSString *arg)
   ns_send_appdefined (-2);
 }
 
+#ifdef NS_IMPL_GNUSTEP
+- (void)sendFromMainThread:(id)unused
+{
+  ns_send_appdefined (nextappdefined);
+}
+#endif
+
 - (void)fd_handler:(id)unused
 /* --------------------------------------------------------------------------
      Check data waiting on file descriptors and terminate if so
@@ -4742,7 +4841,7 @@ not_in_argv (NSString *arg)
   NSEvent *e =[[self window] currentEvent];
   struct face *face =FRAME_DEFAULT_FACE (emacsframe);
   id newFont;
-  float size;
+  CGFloat size;
 
   NSTRACE (changeFont);
   if (!emacs_event)
@@ -4861,7 +4960,16 @@ not_in_argv (NSString *arg)
         {
           /* COUNTERHACK: map 'Delete' on upper-right main KB to 'Backspace',
              because Emacs treats Delete and KP-Delete same (in simple.el). */
-          if (fnKeysym == 0xFFFF && [theEvent keyCode] == 0x33)
+          if ((fnKeysym == 0xFFFF && [theEvent keyCode] == 0x33)
+#ifdef NS_IMPL_GNUSTEP
+              /*  GNUstep uses incompatible keycodes, even for those that are
+                  supposed to be hardware independent.  Just check for delete.
+                  Keypad delete does not have keysym 0xFFFF.
+                  See http://savannah.gnu.org/bugs/?25395
+              */
+              || (fnKeysym == 0xFFFF && code == 127)
+#endif
+            )
             code = 0xFF08; /* backspace */
           else
             code = fnKeysym;
@@ -5014,10 +5122,14 @@ not_in_argv (NSString *arg)
 
 #if !defined (NS_IMPL_COCOA) || MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_6
   /* if we get here we should send the key for input manager processing */
+  /* Disable warning, there is nothing a user can do about it anyway, and
+     it does not seem to matter.  */
+#if 0
   if (firstTime && [[NSInputManager currentInputManager]
                      wantsToDelayTextChangeNotifications] == NO)
     fprintf (stderr,
           "Emacs: WARNING: TextInput mgr wants marked text to be permanent!\n");
+#endif
   firstTime = NO;
 #endif
   if (NS_KEYLOG && !processingCompose)
@@ -5225,7 +5337,12 @@ not_in_argv (NSString *arg)
   return NSMakeRange (NSNotFound, 0);
 }
 
+#if defined (NS_IMPL_COCOA) || GNUSTEP_GUI_MAJOR_VERSION > 0 || \
+    GNUSTEP_GUI_MINOR_VERSION > 22
 - (NSUInteger)characterIndexForPoint: (NSPoint)thePoint
+#else
+- (unsigned int)characterIndexForPoint: (NSPoint)thePoint
+#endif
 {
   if (NS_KEYLOG)
     NSLog (@"characterIndexForPoint request");
@@ -5264,7 +5381,7 @@ not_in_argv (NSString *arg)
 
   if ([theEvent type] == NSScrollWheel)
     {
-      float delta = [theEvent deltaY];
+      CGFloat delta = [theEvent deltaY];
       /* Mac notebooks send wheel events w/delta =0 when trackpad scrolling */
       if (delta == 0)
         return;
@@ -5449,11 +5566,24 @@ not_in_argv (NSString *arg)
   if (oldr != rows || oldc != cols || neww != oldw || newh != oldh)
     {
       NSView *view = FRAME_NS_VIEW (emacsframe);
+      NSWindow *win = [view window];
+      NSSize sz = [win resizeIncrements];
+
       FRAME_PIXEL_WIDTH (emacsframe) = neww;
       FRAME_PIXEL_HEIGHT (emacsframe) = newh;
       change_frame_size (emacsframe, rows, cols, 0, delay, 0);
       SET_FRAME_GARBAGED (emacsframe);
       cancel_mouse_face (emacsframe);
+
+      // Did resize increments change because of a font change?
+      if (sz.width != FRAME_COLUMN_WIDTH (emacsframe) ||
+          sz.height != FRAME_LINE_HEIGHT (emacsframe))
+        {
+          sz.width = FRAME_COLUMN_WIDTH (emacsframe);
+          sz.height = FRAME_LINE_HEIGHT (emacsframe);
+          [win setResizeIncrements: sz];
+        }
+
       [view setFrame: NSMakeRect (0, 0, neww, newh)];
       [self windowDidMove:nil];   // Update top/left.
     }
@@ -5467,7 +5597,7 @@ not_in_argv (NSString *arg)
 #ifdef NS_IMPL_GNUSTEP
   gsextra = 3;
 #endif
-  
+
   NSTRACE (windowWillResize);
 /*fprintf (stderr,"Window will resize: %.0f x %.0f\n",frameSize.width,frameSize.height); */
 
@@ -5505,17 +5635,17 @@ not_in_argv (NSString *arg)
             old_title = 0;
           }
       }
-    else
+    else if (fs_state == FULLSCREEN_NONE && ! maximizing_resize)
       {
         char *size_title;
         NSWindow *window = [self window];
         if (old_title == 0)
           {
-            const char *t = [[[self window] title] UTF8String];
+            char *t = strdup ([[[self window] title] UTF8String]);
             char *pos = strstr (t, "  —  ");
             if (pos)
               *pos = '\0';
-            old_title = xstrdup (t);
+            old_title = t;
           }
         size_title = xmalloc (strlen (old_title) + 40);
        esprintf (size_title, "%s  —  (%d x %d)", old_title, cols, rows);
@@ -5533,7 +5663,7 @@ not_in_argv (NSString *arg)
 
 - (void)windowDidResize: (NSNotification *)notification
 {
-  if (! [self fsIsNative]) 
+  if (! [self fsIsNative])
     {
       NSWindow *theWindow = [notification object];
       /* We can get notification on the non-FS window when in
@@ -5554,21 +5684,27 @@ not_in_argv (NSString *arg)
   NSTRACE (windowDidResize);
 /*fprintf (stderr,"windowDidResize: %.0f\n",[theWindow frame].size.height); */
 
+if (cols > 0 && rows > 0)
+    {
+      [self updateFrameSize: YES];
+    }
+
+  ns_send_appdefined (-1);
+}
+
 #ifdef NS_IMPL_COCOA
+- (void)viewDidEndLiveResize
+{
+  [super viewDidEndLiveResize];
   if (old_title != 0)
     {
+      [[self window] setTitle: [NSString stringWithUTF8String: old_title]];
       xfree (old_title);
       old_title = 0;
     }
-#endif /* NS_IMPL_COCOA */
-
-  if (cols > 0 && rows > 0)
-    {
-      [self updateFrameSize: YES];
-    }
-
-  ns_send_appdefined (-1);
+  maximizing_resize = NO;
 }
+#endif /* NS_IMPL_COCOA */
 
 
 - (void)windowDidBecomeKey: (NSNotification *)notification
@@ -5596,9 +5732,10 @@ not_in_argv (NSString *arg)
 /* cf. x_detect_focus_change(), x_focus_changed(), x_new_focus_frame() */
 {
   struct ns_display_info *dpyinfo = FRAME_NS_DISPLAY_INFO (emacsframe);
+  BOOL is_focus_frame = dpyinfo->x_focus_frame == emacsframe;
   NSTRACE (windowDidResignKey);
 
-  if (dpyinfo->x_focus_frame == emacsframe)
+  if (is_focus_frame)
     dpyinfo->x_focus_frame = 0;
 
   ns_frame_rehighlight (emacsframe);
@@ -5611,10 +5748,10 @@ not_in_argv (NSString *arg)
       x_set_frame_alpha (emacsframe);
     }
 
-  if (emacs_event)
+  if (emacs_event && is_focus_frame)
     {
       [self deleteWorkingText];
-      emacs_event->kind = FOCUS_IN_EVENT;
+      emacs_event->kind = FOCUS_OUT_EVENT;
       EV_TRAILER ((id)nil);
     }
 }
@@ -5643,7 +5780,6 @@ not_in_argv (NSString *arg)
   NSRect r, wr;
   Lisp_Object tem;
   NSWindow *win;
-  NSButton *toggleButton;
   NSSize sz;
   NSColor *col;
   NSString *name;
@@ -5673,7 +5809,10 @@ not_in_argv (NSString *arg)
 
   FRAME_NS_VIEW (f) = self;
   emacsframe = f;
+#ifdef NS_IMPL_COCOA
   old_title = 0;
+  maximizing_resize = NO;
+#endif
 
   win = [[EmacsWindow alloc]
             initWithContentRect: r
@@ -5719,9 +5858,12 @@ not_in_argv (NSString *arg)
   [win setToolbar: toolbar];
   [toolbar setVisible: NO];
 #ifdef NS_IMPL_COCOA
+  {
+    NSButton *toggleButton;
   toggleButton = [win standardWindowButton: NSWindowToolbarButton];
   [toggleButton setTarget: self];
   [toggleButton setAction: @selector (toggleToolbar: )];
+  }
 #endif
   FRAME_TOOLBAR_HEIGHT (f) = 0;
 
@@ -5745,7 +5887,7 @@ not_in_argv (NSString *arg)
   col = ns_lookup_indexed_color (NS_FACE_BACKGROUND
                                   (FRAME_DEFAULT_FACE (emacsframe)), emacsframe);
   [win setBackgroundColor: col];
-  if ([col alphaComponent] != 1.0)
+  if ([col alphaComponent] != (EmacsCGFloat) 1.0)
     [win setOpaque: NO];
 
   [self allocateGState];
@@ -5813,6 +5955,9 @@ not_in_argv (NSString *arg)
       maximized_width = -1;
       result.origin.y = defaultFrame.origin.y;
       [self setFSValue: FULLSCREEN_HEIGHT];
+#ifdef NS_IMPL_COCOA
+      maximizing_resize = YES;
+#endif
     }
   else if (next_maximized == FULLSCREEN_WIDTH)
     {
@@ -5831,14 +5976,20 @@ not_in_argv (NSString *arg)
       maximized_width = result.size.width;
       maximized_height = result.size.height;
       [self setFSValue: FULLSCREEN_MAXIMIZED];
+#ifdef NS_IMPL_COCOA
+      maximizing_resize = YES;
+#endif
     }
   else
     {
       /* restore */
       result = ns_userRect.size.height ? ns_userRect : result;
       ns_userRect = NSMakeRect (0, 0, 0, 0);
+#ifdef NS_IMPL_COCOA
+      maximizing_resize = fs_state != FULLSCREEN_NONE;
+#endif
       [self setFSValue: FULLSCREEN_NONE];
-      maximized_width = maximized_width = -1;
+      maximized_width = maximized_height = -1;
     }
 
   if (fs_before_fs == -1) next_maximized = -1;
@@ -5931,7 +6082,9 @@ not_in_argv (NSString *arg)
 {
   [self setFSValue: fs_before_fs];
   fs_before_fs = -1;
+#ifdef NS_IMPL_COCOA
   [self updateCollectionBehaviour];
+#endif
   if (FRAME_EXTERNAL_TOOL_BAR (emacsframe))
     {
       [toolbar setVisible:YES];
@@ -5978,7 +6131,7 @@ not_in_argv (NSString *arg)
     }
 }
 #endif
+
 - (void)toggleFullScreen: (id)sender
 {
   NSWindow *w, *fw;
@@ -5990,7 +6143,9 @@ not_in_argv (NSString *arg)
 
   if (fs_is_native)
     {
+#ifdef NS_IMPL_COCOA
       [[self window] toggleFullScreen:sender];
+#endif
       return;
     }
 
@@ -6036,7 +6191,7 @@ not_in_argv (NSString *arg)
       [fw useOptimizedDrawing: YES];
       [fw setResizeIncrements: sz];
       [fw setBackgroundColor: col];
-      if ([col alphaComponent] != 1.0)
+      if ([col alphaComponent] != (EmacsCGFloat) 1.0)
         [fw setOpaque: NO];
 
       f->border_width = 0;
@@ -6074,7 +6229,7 @@ not_in_argv (NSString *arg)
       [w setContentView:[fw contentView]];
       [w setResizeIncrements: sz];
       [w setBackgroundColor: col];
-      if ([col alphaComponent] != 1.0)
+      if ([col alphaComponent] != (EmacsCGFloat) 1.0)
         [w setOpaque: NO];
 
       f->border_width = bwidth;
@@ -6513,7 +6668,7 @@ not_in_argv (NSString *arg)
 {
   Lisp_Object str = Qnil;
   struct frame *f = SELECTED_FRAME ();
-  struct buffer *curbuf = XBUFFER (XWINDOW (f->selected_window)->buffer);
+  struct buffer *curbuf = XBUFFER (XWINDOW (f->selected_window)->contents);
 
   if ([attribute isEqualToString:NSAccessibilityRoleAttribute])
     return NSAccessibilityTextFieldRole;
@@ -6768,10 +6923,11 @@ not_in_argv (NSString *arg)
     }
   else
     {
-      float pos, por;
+      float pos;
+      CGFloat por;
       portion = max ((float)whole*min_portion/pixel_height, portion);
       pos = (float)position / (whole - portion);
-      por = (float)portion/whole;
+      por = (CGFloat)portion/whole;
 #if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
       [self setKnobProportion: por];
       [self setDoubleValue: pos];
@@ -6796,7 +6952,7 @@ not_in_argv (NSString *arg)
   *part = last_hit_part;
   *window = win;
   XSETINT (*y, pixel_height);
-  if ([self floatValue] > 0.999)
+  if ([self floatValue] > 0.999F)
     XSETINT (*x, pixel_height);
   else
     XSETINT (*x, pixel_height * [self floatValue]);
@@ -6870,7 +7026,7 @@ not_in_argv (NSString *arg)
   NSRect sr, kr;
   /* hitPart is only updated AFTER event is passed on */
   NSScrollerPart part = [self testPart: [e locationInWindow]];
-  double inc = 0.0, loc, kloc, pos;
+  CGFloat inc = 0.0, loc, kloc, pos;
   int edge = 0;
 
   NSTRACE (EmacsScroller_mouseDown);
@@ -6969,7 +7125,6 @@ not_in_argv (NSString *arg)
 {
     NSRect sr;
     double loc, pos;
-    int edge = 0;
 
     NSTRACE (EmacsScroller_mouseDragged);
 
@@ -6980,15 +7135,13 @@ not_in_argv (NSString *arg)
       if (loc <= 0.0)
         {
           loc = 0.0;
-          edge = -1;
         }
       else if (loc >= NSHeight (sr) + last_mouse_offset)
         {
           loc = NSHeight (sr) + last_mouse_offset;
-          edge = 1;
         }
 
-      pos = /*(edge ? loc :*/ (loc - last_mouse_offset) / NSHeight (sr);
+      pos = (loc - last_mouse_offset) / NSHeight (sr);
       [self sendScrollEventAtLoc: pos fromEvent: e];
 }
 
@@ -7015,6 +7168,12 @@ not_in_argv (NSString *arg)
 @end  /* EmacsScroller */
 
 
+#ifdef NS_IMPL_GNUSTEP
+/* Dummy class to get rid of startup warnings.  */
+@implementation EmacsDocument
+
+@end
+#endif
 
 
 /* ==========================================================================